This page is still under construction.

Parts of this page are still being built. What you see may change.

Roads and Planes

Time limit1sMemory limit128 MB

Summary
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 TT towns numbered 1…T1 \dots T, connected by up to RR roads and PP airplane flights.

Each road or plane connects town AiA_i to town BiB_i with traversal cost CiC_i.

  • Roads are bidirectional and may be traversed from Ai→BiA_i \to B_i or from Bi→AiB_i \to A_i for the same cost. A road's cost is always non-negative: 0≤Ci≤100000 \le C_i \le 10000.
  • Planes may be flown only in the given direction, from Ai→BiA_i \to B_i. A plane's cost may be negative: −10000≤Ci≤10000-10000 \le C_i \le 10000.

It is guaranteed that whenever a plane goes from AiA_i to BiB_i, there is no way to return from BiB_i to AiA_i 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 SS. For every town, find the cheapest total cost to deliver from town SS to that town, or report that no route exists.

Constraints:

  • 1≤T≤250001 \le T \le 25000
  • 1≤R≤500001 \le R \le 50000, 1≤P≤500001 \le P \le 50000
  • 1≤Ai,Bi,S≤T1 \le A_i, B_i, S \le T

Input

The first line contains four space-separated integers TT, RR, PP, and SS.

Each of the next RR lines contains three integers AiA_i, BiB_i, CiC_i describing a road.

Each of the following PP lines contains three integers AiA_i, BiB_i, CiC_i describing a plane.

Output

Print TT lines. Line ii contains the minimum cost to travel from town SS to town ii, or NO PATH if town ii cannot be reached from town SS.

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.

Examples3

  1. Example 1

    Input
    6 3 3 4
    1 2 5
    3 4 5
    5 6 10
    3 5 -100
    4 6 -100
    1 3 -10
    
    Expected output
    NO PATH
    NO PATH
    5
    0
    -95
    -100
    
  2. Example 2

    Input
    3 1 1 1
    1 2 5
    2 3 -4
    
    Expected output
    0
    5
    1
    
  3. Example 3

    Input
    6 6 1 1
    1 2 2
    1 3 5
    2 3 1
    2 4 7
    3 5 3
    4 5 1
    5 6 2
    
    Expected output
    0
    2
    3
    7
    6
    8