Tilted Towers

Time limit: 2s Memory limit: 256MB Input: Output:

Task

You are given an undirected weighted graph with NN nodes and MM edges. You are also given two integers: QQ and PP.

Your task is to determine the minimum cost path from node 11 to node NN, with the following conditions:

  • The path is not required to be simple, meaning you may traverse the same edge multiple times.
  • At each step along the path, you have two options:
    1. You can traverse a single edge, paying its original cost.
    2. You can choose to traverse 2X2^X consecutive edges (for some 0XQ0 \le X \le Q), where the cost of each edge is replaced with PP instead of its original cost.

Important constraint:

The values of XX you choose must be pairwise distinct, meaning that each shortcut 2X2^X can be used at most once in the entire path.
For example, if you use a shortcut of length 212^1, you cannot use length 212^1 again — but you may still use 22=42^2 = 4 or 23=82^3 = 8, as long as each XQX \le Q is unique.

Input data

On the first line, there will be the four numbers NN, MM, QQ, and PP with the meaning from the statement, separated by a single space.
On the next MM lines, the edges of the graph will be described.
On each such line, there will be 33 numbers: AA, BB, CC (separated by a single space), meaning that there is an edge between nodes AA and BB with cost CC.

Output data

The first line will contain a single number, representing the minimum cost of a path from node 11 to node NN.

Constraints and clarifications

  • 1A,BN5001 \le A, B \le N \le 500
  • 1MN(N1)21 \le M \le \frac{N*(N-1)}{2}
  • 1Q91 \le Q \le 9
  • 1C,P1091 \le C, P \le 10^9
  • The given graph is connected.

Example 1

stdin

8 12 4 3
1 2 2
1 3 2
1 4 3
2 3 3
2 4 5
3 4 5
3 5 4
5 6 3
5 7 5
7 8 2
6 7 3
6 8 5

stdout

10

Example 2

stdin

4 5 2 1
1 2 4
2 3 1
2 4 2
1 3 3
3 4 3

stdout

2

Log in or sign up to be able to send submissions!