#Lutece0563. Improving roads
Improving roads
Migrated from Lutece 563 Improving roads
All parts of this problem, including description, images, samples, data and checker, might be broken. If you find bugs in this problem, please contact the admins.
Description
Building the roads in the shortest time restored the transport facilities of Country X. However, the economy is still under a downturn. After investigating for a long time, Qiqi found that the key point is the high cost of traffic between capital and the other cities.
Now Qiqi wants to find the best reconstruction plan. Best plan must satisfy the following conditions:
- for each non-capital city, there is exactly one path from capital to it, and the length of this very path should be minimal.
- after satisfying the first condition, the total length of all roads needed to build is minimal.
Given many optional roads Qiqi can build, please tell him the total length of roads in the best plan. Note that all roads in this problem are one-way.
Input
There are multiple test cases. The first line of the input will be an integer () indicating the number of test cases.
The first line of each test case contains two integers: , representing the number of cities of Country X, and number of optional roads. The cities are numbered from to and the capital is numbered .
Then comes lines, each gives a road: , representing that Qiqi could build a road from to and the length of this road is .
We guarantee there are no multiple roads and self-loops.
, .
, .
Output
For each test case, print Case #t:
first, in which is the number of the test case starting from . Then output the total length. If Qiqi cannot find a plan, output -1
.
Samples
2
3 3
1 2 1
2 3 1
1 3 2
3 2
1 2 1
3 2 1
Case #1: 2
Case #2: -1
Note
Huge input/output. Please use scanf/printf
for C/C++
.
Resources
10th UESTC Programming Contest Preliminary