Jogging in the Park
Time limit2sMemory limit512 MB
Extend k walks that start at glade 1 so each ends at glade n with the same total length, or report -1 when that is impossible.
- Level
Hard8 of 10
- Topics
- Graph, Shortest path, Number theory, Math
- Solved
- No attempts yet
Problem
The Outer Park is very well suited for jogging. Inside the park, there are glades, numbered from 1 to . The glades are connected by trails. The -th trail connects glades and and has length meters. All trails can be walked in both directions. The main entrance is at glade 1.
Your friends each prepared a route that starts at the entrance and moves from glade to glade along trails. At the end of the run, all friends want to reach glade at the same time, where a cafe is. Some routes do not end at glade , and the routes may have different lengths. All friends run at the same speed.
Extend each route so that it starts with the same glades as the original, ends at glade , and has exactly the same length as every other extended route. An original or extended route may use the same trail more than once.
Input
The first line contains two integers and (; ), the number of glades and trails. Each of the next lines contains three integers , , and (; ; ), the glades connected by the -th trail and its length.
The next line contains a single integer (), the number of friends. Each of the next lines contains (), the number of glades in the route of the -th friend, followed by integers (), the glades of that route.
All unordered pairs are distinct. For every , . For every between 1 and , there is a trail between and .
Output
If it is impossible to extend all the routes, print a single integer -1. Otherwise, print lines. The -th line contains () followed by integers (), the glades of the -th extended route.
For every , . For every between 1 and , there is a trail between and . For every between 1 and , . The total length of all extended routes must be the same.
The sum of all must not exceed . If a valid extension exists, one exists with the sum of all at most .