#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 NN nodes and MM 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 00.

If a negative weight cycle exists, output only one line with 1-1; otherwise, compute the shortest path from a given source node SS to every other node. The distance from SS to itself is defined as 00. If SS is not connected to a node, output NoPath.

Input Format

The first line contains three positive integers: the number of nodes NN, the number of edges MM, and the source node SS.

The following MM lines each contain three integers a,b,ca, b, c, indicating an edge from node aa to node bb with weight cc.

Output Format

If a negative weight cycle exists, output only one line with 1-1. Otherwise, output the following:

A total of NN lines, where the ii-th line describes the shortest path from SS to node ii:

  • If SS is not connected to ii, output NoPath;
  • If i=Si = S, output 00.
  • Otherwise, output the length of the shortest path from SS to ii.

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$.