luogu#P5122. [USACO18DEC] Fine Dining G

[USACO18DEC] Fine Dining G

Problem Description

After a long day, the hungry and tired cows are ready to return to the barn.

The farm has NN pastures (2N5×1042\le N\le 5\times 10^4), numbered 1N1\dots N for convenience. All cows want to go to the barn located at pasture NN. Each of the other N1N-1 pastures has one cow. The cows can move between pastures using MM undirected paths (1M1051\le M\le 10^5). The ii-th path connects pastures aia_i and bib_i, and it takes time tit_i to travel along it. Each cow can take some sequence of paths to get back to the barn.

Because they are hungry, the cows are happy to stop and eat along the way home. There are KK pastures with tasty hay bales (1KN1 \le K \le N). The ii-th hay bale has tastiness value yiy_i. Each cow wants to stop at exactly one hay bale on her way to the barn, but she will do so only if going through that hay bale increases her travel time to the barn by no more than the hay bale’s tastiness value. Note that a cow only “officially” stops to eat at one hay bale, even if her path passes through other pastures that also have hay bales; she will simply ignore the other hay bales.

Input Format

The first line contains three space-separated integers NN, MM, and KK. The next MM lines each contain three integers aia_i, bib_i, and tit_i, describing a path between pastures aia_i and bib_i that takes time tit_i to traverse (aia_i is not equal to bib_i, and tit_i is a positive integer not exceeding 10410^4).

The next KK lines each describe a hay bale with two integers: the index of the pasture where the hay bale is located, and its tastiness value (a positive integer not exceeding 10910^9). There may be multiple hay bales in the same pasture.

Output Format

Output N1N-1 lines. If the cow in pasture ii can visit some hay bale and eat there on her way back to the barn, output an integer 11 on line ii; otherwise output an integer 00.

4 5 1
1 4 10
2 1 20
4 2 3
2 3 5
4 3 2
2 7
1
1
1

Hint

In this example, the cow in pasture 33 can stop to eat, because her time to get back increases by only 66 (from 22 to 88), and this increase does not exceed the hay bale’s tastiness value 77. The cow in pasture 22 can clearly eat the hay in pasture 22, since this does not cause her best path to change. The cow in pasture 11 is an interesting case, because it looks like her best path (1010) might increase too much if she goes to eat. However, there is indeed a path that lets her stop at a hay bale: first go to pasture 44, then go to pasture 22 (eat hay), and then return to pasture 44.

Translated by ChatGPT 5