Negative cycle detection

Write a program that, given a directed graph with positive and/or negative costs at the arcs, detects if there is a negative cycle in the graph.

Input

Input consists of several cases. Every case begins with the number of vertices nn and the number of arcs mm. Follow mm triples u,v,cu, v, c, indicating that there is an arc uvu \to v of cost cc, where uvu \ne v, 106c106-10^6 \le c \le 10^6. Assume 1n1041 \le n \le 10^4, 0m5n0 \le m \le 5n, and that for every pair of vertices uu and vv there is at most one arc of the kind uvu \to v. All numbers are integers. Vertices are numbered from 0 to n1n-1.

Output

For every case, print "YES" if there is a negative cycle in the graph, and "NO" otherwise.

Problem information

Author: Jordi Petit

Generation: 2026-01-25T10:35:31.051Z

© Jutge.org, 2006–2026.
https://jutge.org