Drifting
InterviewTime limit2sMemory limit1024 MB
Find the minimum weight path from vertex 1 to vertex N in a DAG where certain consecutive edge pairs (a, b, c) are forbidden.
- Level
Medium5 of 10
- Topics
- Graph, Shortest path, Hash map
- Solved
- No attempts yet
Problem
You are given a weighted directed graph with vertices and edges. The -th edge goes from vertex to vertex with weight . Every edge satisfies .
You are also given triples of integers. The -th triple is with .
You start at vertex and move along edges until you reach vertex . If you move directly from to , your next move must go to a vertex other than .
Determine whether vertex can be reached. If it can, find the minimum total weight of the edges you pass through.
Input
The first line contains and . Each of the next lines contains , , and . The next line contains . Each of the next lines contains , , and .
Output
If vertex cannot be reached, print . Otherwise, print the minimum total weight of the edges you pass through.
Constraints
- for all
- No two edges have the same
- for all
Hint
In the first sample, the best route is . In the second sample, the best route is .