Test Case Tweaking

Time limit1sMemory limit128 MB

Summary
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 cc and a directed graph GG. Each edge of GG has a non-negative integer cost. For a path from one node of GG 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 11 and the destination node nn — you must adjust the edge costs so that the minimum-cost path from node 11 to node nn becomes exactly the target cost cc. You may assume that cc 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 nn, mm, and cc are the number of nodes, the number of edges, and the target cost, respectively, separated by single spaces, where 2≤n≤1002 \le n \le 100, 1≤m≤10001 \le m \le 1000, and 0≤c≤1000000 \le c \le 100000.

Each node of the graph is represented by an integer from 11 to nn.

The following mm lines describe the edges: the integers fif_i, tit_i, and cic_i (1≤i≤m1 \le i \le m) are the originating node, the destination node, and the cost of the ii-th edge, separated by single spaces. They satisfy 1≤fi,ti≤n1 \le f_i, t_i \le n and 0≤ci≤100000 \le c_i \le 10000. You may assume that fi≠tif_i \ne t_i, and that (fi,ti)≠(fj,tj)(f_i, t_i) \ne (f_j, t_j) when i=ji = j.

For each dataset you may assume that there is at least one path from node 11 to node nn, and that the cost of the minimum-cost path from node 11 to node nn in the given graph is greater than cc.

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 11 to node nn equals the target cost cc. Edge costs cannot be made negative. The output must not contain any other extra characters.

Examples1

  1. Example 1

    Input
    3 3 3
    1 2 3
    2 3 3
    1 3 8
    12 12 2010
    1 2 0
    2 3 3000
    3 4 0
    4 5 3000
    5 6 3000
    6 12 2010
    2 7 100
    7 8 200
    8 9 300
    9 10 400
    10 11 500
    11 6 512
    10 18 1
    1 2 9
    1 3 2
    1 4 6
    2 5 0
    2 6 10
    2 7 2
    3 5 10
    3 6 3
    3 7 10
    4 7 6
    5 8 10
    6 8 2
    6 9 11
    7 9 3
    8 9 9
    8 10 8
    9 10 1
    8 2 1
    0 0 0
    
    Expected output
    1
    2
    3