Roads and Planes
Time limit1sMemory limit128 MB
Find shortest paths from town S in a graph mixing bidirectional non-negative roads with one-way planes that may have negative costs and never form a return cycle.
- Level
Hard8 of 10
- Topics
- Shortest path, Graph, Topological sort, Union-find
- Solved
- No attempts yet
Problem
Farmer John is researching a new milk-delivery contract in a fresh territory. He must deliver milk to towns numbered , connected by up to roads and airplane flights.
Each road or plane connects town to town with traversal cost .
- Roads are bidirectional and may be traversed from or from for the same cost. A road's cost is always non-negative: .
- Planes may be flown only in the given direction, from . A plane's cost may be negative: .
It is guaranteed that whenever a plane goes from to , there is no way to return from to using any sequence of roads and planes. (In other words, the flights never form a cycle, so the whole network contains no negative cycle.)
Farmer John's distribution center is in town . For every town, find the cheapest total cost to deliver from town to that town, or report that no route exists.
Constraints:
- ,
Input
The first line contains four space-separated integers , , , and .
Each of the next lines contains three integers , , describing a road.
Each of the following lines contains three integers , , describing a plane.
Output
Print lines. Line contains the minimum cost to travel from town to town , or NO PATH if town cannot be reached from town .
Notes
Because a plane can be flown in only one direction and can never be undone, some towns may be impossible to reach; those must print NO PATH. Roads have non-negative cost, so within any group of towns connected only by roads the usual shortest-path rules apply, while the one-way planes impose an acyclic ordering on those groups. The guarantee that no plane can be reversed means the whole network has no negative cycle, so every reachable town has a uniquely determined minimum cost.