#Q88. 「一本通 3.3 练习 3」Easy SSSP
「一本通 3.3 练习 3」Easy SSSP
Description
Original source: Vijos P1053
The input data provides a weighted directed graph with nodes and edges. Your task is to write a program that determines whether this directed graph contains a negative weight cycle. A negative weight cycle is defined as a path that starts and ends at the same node, with the sum of the weights along the edges of the path being less than .
If a negative weight cycle exists, output only one line with ; otherwise, compute the shortest path from a given source node to every other node. The distance from to itself is defined as . If is not connected to a node, output NoPath.
Input Format
The first line contains three positive integers: the number of nodes , the number of edges , and the source node .
The following lines each contain three integers , indicating an edge from node to node with weight .
Output Format
If a negative weight cycle exists, output only one line with . Otherwise, output the following:
A total of lines, where the -th line describes the shortest path from to node :
- If is not connected to , output
NoPath; - If , output .
- Otherwise, output the length of the shortest path from to .
Sample 1
6 8 1
1 3 4
1 2 6
3 4 -7
6 4 2
2 4 5
3 6 3
4 5 1
3 5 4
0
6
4
-3
-2
7
Data Range and Hints
For all data, $2\le N\le 1000,1\le M\le 10^5,1\le a,b,S\le N,|c|\le 10^6$.