#P3385. 【模板】负环
【模板】负环
Description
Given a directed graph with vertices, determine whether there exists a negative cycle that is reachable from vertex .
A negative cycle is defined as a cycle whose sum of edge weights is negative.
Input Format
This problem contains multiple groups of testdata in a single test point.
The first line contains an integer , representing the number of groups of testdata. For each group, the format is as follows:
The first line contains two integers, the number of vertices and the number of lines of edge information that follow.
Then follow lines, each with three integers .
- If , then there exists an edge from to with weight , and also an edge from to with weight .
- If , then there exists only an edge from to with weight .
Output Format
For each group of testdata, output one line containing a string. Print YES if such a negative cycle exists, otherwise print NO.
2
3 4
1 2 2
1 3 4
2 3 1
3 1 -3
3 3
1 2 3
2 3 4
3 1 -8
NO
YES
Hint
Constraints and Conventions
For all test points, it is guaranteed that:
- , .
- , .
- .
Hint
Please note that is not the number of edges of the graph.
Translated by ChatGPT 5
京公网安备 11011102002149号