Movable Type Graph
Time limit1.5sMemory limit1024 MB
Find the shortest path from vertex 1 to vertex 2 in graph T, where each graph can embed earlier graphs as sub-blocks with chosen entry and exit vertices.
- Level
Hard8 of 10
- Topics
- Graph, Shortest path, Dynamic programming
- Solved
- No attempts yet
Problem
In the Goryeo period, the scholar Ha Uibi made graphs by printing them with metal movable type. While doing this, Ha Uibi became curious about the shortest path in a type graph, the name for a graph printed with type.
Ha Uibi currently has type graphs, numbered from 1 to in the order they were made.
Type graph has numbered vertices, from 1 to . Type graph is then completed by repeatedly doing one of the following.
- Add a directed edge with weight from vertex to vertex .
- Print the -th type graph so that its start and end points are vertices and of graph . Its vertices and edges are added to graph . The vertices of the printed -th graph are not numbered. ()
The start vertex of every type graph is vertex 1, and its end vertex is vertex 2.
Find the shortest path from vertex 1 to vertex 2 in type graph .
Input
The first line contains the number of type graphs . ()
The information for each type graph is given as follows.
Suppose you are reading type graph .
- The first line contains the number of numbered vertices and the number of edge or sub-graph operations , separated by a space. (, )
- The next lines each contain three integers , , and , separated by spaces. (, , )
- If , a directed edge with weight from vertex to vertex was added.
- If , the type graph was printed with its start point at vertex and its end point at vertex .
The sum of the numbered vertex counts does not exceed . The sum of the edge and sub-graph operation counts does not exceed .
Output
Print the length of the shortest path from vertex 1 to vertex 2 in type graph . If there is no path from vertex 1 to vertex 2, or if this value is greater than , print -1.