Beware the Geoducks
Time limit1sMemory limit128 MB
Given two fixed walking routes on a weighted graph, decide whether the two travelers ever occupy the same point within t seconds, accounting for nodes with geoducks that make a traveler vanish.
- Level
Hard8 of 10
- Topics
- Implementation, Simulation, Geometry, Math
- Solved
- No attempts yet
Problem
Stan Velikiy is once again chasing his arch-nemesis, Mario the Wabbit, this time around a circuit. As the amused observer, you have been asked to predict the outcome.
The circuit is a set of nodes connected by wires of given lengths. Stan and Mario each start at one node and follow a predetermined route: a list of nodes visited in order, moving along the wires at a speed of one meter per second. Consecutive nodes in a route are always joined directly by a wire. When a traveler's route runs out, they stay at that final node forever.
If Stan and Mario are ever at the exact same location at the same instant — the same node, or the same point along a wire — Stan apprehends Mario. If more than seconds pass without a capture, Stan gives up.
Unknown to both, geoducks sit at some of the nodes. Anyone who reaches a node holding a geoduck vanishes instantly, and once either Stan or Mario vanishes, Stan can never catch Mario. In particular, if the two meet exactly on a node that holds a geoduck, they both vanish and it does not count as a capture.
Input
The first line contains six integers: (), the number of nodes; (), the number of wires; and (), the number of nodes in Stan's and Mario's routes, respectively; (), the number of geoducks; and (), the time limit in seconds.
Each of the next lines contains three integers: the two nodes that a wire connects and its length (). No wire connects a node to itself, and there is at most one wire between any two nodes.
The next lines each contain one integer, giving the nodes of Stan's route in the order they are visited.
The next lines each contain one integer, giving the nodes of Mario's route in the order they are visited.
The final lines each contain one integer, giving a node that holds a geoduck.
Output
Print YES if Stan catches Mario no later than seconds after the start, and NO otherwise.
Note
As an illustration, suppose Stan walks from node 1 toward node 2 while Mario walks from node 2 toward node 1 along the same wire, with a geoduck resting on an unrelated node 3 that neither traveler ever reaches. The two meet in the middle of that wire; even if they arrive there exactly when the time limit is reached, Stan still catches Mario.