Rabbit vs Turtle
Time limit1sMemory limit512 MB
Count the nodes on Stan's path where he can switch to the shortest route to node N and still beat George, who may be sleeping on his own path.
- Level
Medium7 of 10
- Topics
- Shortest path, Prefix sum, Graph
- Solved
- No attempts yet
Problem
A rabbit named Stan and a turtle named George race on a graph with nodes and edges. The race starts at node 1 and ends at node . Each racer picks a path before the race. Both know the graph, both paths, and the time each racer needs to cross each edge.
George chooses some nodes on his path where he sleeps for a while. If George realizes that Stan is cheating, he stops sleeping and runs straight to the finish.
Stan does not intend to keep his promise. At some node he plans to switch from his assigned path to the shortest path to node . He can change paths only while he is at a node, never while he is on an edge. If Stan cheats while George is awake, George notices at once. If George is asleep, he notices when he wakes up.
Count the moments when Stan can switch paths and still win the race.
Input
The first line contains and . Each of the next lines has four values , , , , meaning there is an edge from node to node . George needs time units to cross it, and Stan needs time units. The -th edge has index .
The next line contains , the number of nodes on George's path. The next lines each contain an edge index and a sleep time: the edge George uses next, and how long he sleeps after using it. The sleep value on the last edge does not matter, since George reaches the finish.
The next line contains , the number of edges on Stan's initial path. The last line contains edge indices, the edges Stan uses in order.
Output
On the first line, print , the number of moments when Stan can cheat. On the second line, print the node indices in ascending order.
Constraints
- The edges in the path descriptions are given in the correct order.
- The nodes on George's path do not repeat.
- The nodes on Stan's path do not repeat.
- If Stan and George reach the finish at the same time, Stan wins.
- If Stan cheats at the exact moment George starts to sleep, George falls asleep first and notices the cheating only after waking up.
- Stan cheats only if he leaves his original path and the new path is strictly faster than the rest of the original path.
- The cheating path may share nodes with the original path. At the moment of cheating, Stan must take a different edge from his original one. He may return to the previous node if an edge allows it.