Module: Floyd's algorithm


Problem

7 /10


Negative Cycle-2

Theory Click to read/hide

you can read more about finding a negative cycle here: http://e-maxx.ru/algo/export_ford_bellman

Problem

Given a directed graph. Determine if it has a negative weight cycle, and if so, output it.
 
Input
The first line contains the number N (1 <= N <= 100) – the number of graph vertices. The next N lines contain N numbers each – graph adjacency matrix. Edge weights modulo less than 100000. If there is no edge, the corresponding value is 100000.
 
Output
On the first line print "YES" if the cycle exists, or "NO" otherwise. If there is a loop, in the second line print the number of vertices in it (counting the same – first and last), and in the third line – the vertices included in this cycle are in traversal order. If there are several cycles, then  print any of them.

Examples
# Input Output
1
3
100000 100000 -51
100  100000 100000
100000 -50  100000
YES
4
3 2 1 3