Negative weight cycle
Submissions: 1056   Accuracy:

38.41%

  Difficulty: Medium   Marks: 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

** For More Input/Output Examples Use 'Expected Output' option **

Contributor: Vikraman
Author: vikikkdi


If you have purchased any course from GeeksforGeeks then please ask your doubt on course discussion forum. You will get quick replies from GFG Moderators there.



Need help with your code? Please use ide.geeksforgeeks.org, generate link and share the link here.

to report an issue on this page.