Exact-Trail Relay
Time limit2sMemory limit128 MB
Compute the minimum total length of a walk between two intersections that uses exactly N trails, where N can be as large as one million.
- Level
Hard8 of 10
- Topics
- Shortest path, Matrix, Graph, Math
- Solved
- No attempts yet
Problem
For a fitness program, N (2 <= N <= 1,000,000) cows will run a relay using the T (2 <= T <= 100) trails in a pasture.
Each trail connects two different intersections. Intersection labels are integers between 1 and 1,000, and every listed intersection is incident to at least two trails. For each trail, the cows know its length (1 <= length_i <= 1,000) and the two intersections I1_i and I2_i that it connects. No two intersections are directly connected by more than one trail.
The cows may stand at intersections, and more than one cow may stand at the same intersection. They need to pass the baton from cow to cow so that the route starts at intersection S, ends at intersection E, and uses exactly N trails.
Find the minimum possible total distance of such a route.
Input
- Line 1: four space-separated integers
N,T,S, andE. - Lines 2 through
T+1: linei+1describes trailiwith three space-separated integerslength_i,I1_i, andI2_i.
Output
Print one integer: the shortest distance from intersection S to intersection E using exactly N trails.