Two Shortest Paths
Time limit5sMemory limit1024 MB
Given a directed weighted graph, find for each vertex i the minimum total cost of two edge-disjoint paths from vertex 1 to i.
- Level
Hard8 of 10
- Topics
- Graph, Shortest path
- Solved
- No attempts yet
Problem
The graph has vertices and edges. Each edge is directed and has a non-negative integer weight.
For every , find the minimum total cost of two non-overlapping paths from vertex 1 to vertex . Two paths do not overlap if they share no edge. The cost of a path is the sum of the weights of the edges on it.
Input
The first line contains the number of test cases . Each of the following test cases is given on separate lines.
Each test case has the following format.
- The first line contains two integers and .
- Each of the next lines contains three integers , , . This means there is an edge from vertex to vertex with weight .
Output
For each test case, print one line with integers. The -th integer is the minimum total cost of two non-overlapping paths from vertex 1 to vertex . If two non-overlapping paths do not exist, print -1.
Constraints
- Vertex 1 can reach every vertex.
- In one input file, the sum of is at most .
- In one input file, the sum of is at most .