Test Case Tweaking
Time limit1sMemory limit128 MB
Given a directed graph and target cost c smaller than its current shortest 1-to-n path cost, find the minimum number of edge costs to change so the new shortest path equals exactly c.
- Level
Hard8 of 10
- Topics
- Shortest path, Graph, Binary search, Greedy
- Solved
- No attempts yet
Problem
You are a judge for a programming contest. You are preparing a dataset for a graph problem that asks for the cost of the minimum-cost path. You have generated some random cases, but they are not interesting. You want the answer to be a specific desired value — for instance, the number 2010 representing this year. To do this, you will tweak (adjust) the cost of the minimum-cost path to a given value by changing the costs of some edges, using as few changes as possible.
You are given a non-negative integer and a directed graph . Each edge of has a non-negative integer cost. For a path from one node of to another, the cost of the path is the sum of the costs of the edges on it. For a pair of nodes, the minimum cost between them is the minimum over the costs of all paths connecting them.
Given the graph and two of its nodes — the start node and the destination node — you must adjust the edge costs so that the minimum-cost path from node to node becomes exactly the target cost . You may assume that is smaller than the cost of the minimum-cost path between these two nodes in the original graph.
For example, in Figure G.1 the minimum cost of a path from node 1 to node 3 in the given graph is 6. To adjust this minimum cost to 2, we can change the cost of the edge from node 1 to node 3 to 2; after the change that direct edge becomes the minimum-cost path.
For another example, in Figure G.2 the minimum cost of a path from node 1 to node 12 is 4022. To adjust this minimum cost to 2010, we can change the cost of the edge from node 6 to node 12 together with one of the six edges in the right half of the graph. There are many possible edge modifications, but the minimum number of modified edges is 2.

Figure G.1: Example 1 of graph

Figure G.2: Example 2 of graph
Input
The input is a sequence of datasets. Each dataset has the following format.
n m c
f1 t1 c1
f2 t2 c2
.
.
.
fm tm cm
The integers , , and are the number of nodes, the number of edges, and the target cost, respectively, separated by single spaces, where , , and .
Each node of the graph is represented by an integer from to .
The following lines describe the edges: the integers , , and () are the originating node, the destination node, and the cost of the -th edge, separated by single spaces. They satisfy and . You may assume that , and that when .
For each dataset you may assume that there is at least one path from node to node , and that the cost of the minimum-cost path from node to node in the given graph is greater than .
The end of the input is indicated by a line containing three zeros separated by single spaces.
Output
For each dataset, output on one line the minimum number of edges whose costs must be changed so that the cost of the minimum-cost path from node to node equals the target cost . Edge costs cannot be made negative. The output must not contain any other extra characters.