#SDNU1505. C.Proxy
C.Proxy
Description
Because of the GFW (Great Firewall), we cannot directly visit many websites, such as Facebook, Twitter, YouTube, etc. But with the help of proxy and proxy server, we can easily get to these website.
You have a list of several proxy servers, some of them can be connected directly but others can’t. But you can visit proxy servers through other proxy server by a one-way connection.
As we all know, the lag of internet visit will decide our feelings of the visit. You have a very smart proxy software which will find the least lag way to reach the website once you choose a directly reachable proxy server.
You know the lag of every connection. The lag of your visit is the all the lags in your whole connection. You want to minimize the lag of visit, which proxy server you will choose?
Format
Input
Multiple test cases, the first line is an integer , indicating the number of test cases.
The first line of each test case is two integers . is the number of proxy servers (labeled from 1 to ).
0 is the label of your computer and () is the label of the server of target website.
Then lines follows, each line contains three integers $u, v, w (0 \leq u, v \leq N + 1, 1 \leq w \leq 1000)$, means can directly connect to and the lag is .
Output
An integer in one line for each test case, which proxy server you will choose to connect directly. You can only choose the proxy server which can be connected directly from your computer.
If there are multiple choices, you should output the proxy server with the least label.
If you can’t visit the target website by any means, output “-1” (without quotes).
If you can directly visit the website and the lag is the least, output “0” (without quotes).
Samples
4
3 6
0 1 10
1 2 1
2 4 4
0 3 2
3 2 1
3 4 7
2 4
0 2 10
0 1 5
1 2 4
2 1 7
1 3
0 2 1
0 1 2
1 2 1
1 3
0 2 10
0 1 2
1 2 1
3
-1
0
1