#P7831. [CCO 2021] Travelling Merchant
[CCO 2021] Travelling Merchant
Problem Description
A country has cities and directed roads, and a travelling merchant travels among these cities.
The -th road goes from city to city . He can use this road only when his assets are at least yuan. After taking this road, his assets increase by yuan.
He wants to be able to keep travelling forever without stopping, so he wants to know, starting from each city, the minimum initial assets required.
Input Format
The first line contains two integers .
The next lines each contain four integers on the -th line.
Output Format
Output one line with integers. If the answer for the -th city does not exist, then the -th integer is . Otherwise, it is the minimum initial assets required (in yuan).
5 5
3 1 4 0
2 1 3 0
1 3 1 1
3 2 3 1
4 2 0 2
2 3 3 1 -1
Hint
Sample #1 Explanation
Take city as an example: starting from city with initial assets of yuan, he can loop forever among cities .
Constraints
For of the testdata, .
For another of the testdata, .
For of the testdata, , , , , it is guaranteed that there are no self-loops but there may be multiple edges.
Source
CCO2021 D2T1
Translated by ChatGPT 5