Negative weight cycle
Medium Accuracy: 40.39% Submissions: 6546 Points: 4

Given a graph, the task is to check if it contains a negative weight cycle or not.

Input:
The first line of input contains an integer T denoting the no of test cases. Then T test cases follow. Each test case contains 2 lines the first line contains two space separated integers v and e denoting the no of vertices and no of edges respectively. Then in the next line are 3*e space separated values x,y,z denoting an edge from x to y with a weight of z.

Output:
For each test case in a new line print 0 if there is no negative weight cycle else print 1.

Constraints:
1 <= T <= 50
1 <= v <= 100
1 <= e <= 50

Example:
Input:
2
5 8
0 1 -1 0 2 4 1 2 3 1 3 2 1 4 2 3 2 5 3 1 1 4 3 -3
3 3
0 1 -5 1 2 -6 2 0 -9

Output:
0
1

to report an issue on this page.

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?

Yes

All Submissions

My Submissions:

Login to access your submissions.

Output Window