forked from imsushant12/GFG-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Negative_weight_cycle.cpp
65 lines (53 loc) · 1.65 KB
/
Negative_weight_cycle.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/*
Problem Statement:
------------------
Given a weighted directed graph with n nodes and m edges. Nodes are labeled from 0 to n-1, the task is to check if it contains a negative weight cycle or not.
Note: edges[i] is defined as u, v and weight.
Example 1:
Input: n = 3, edges = {{0,1,-1},{1,2,-2},
{2,0,-3}}
Output: 1
Explanation: The graph contains negative weight
cycle as 0->1->2->0 with weight -1,-2,-3,-1.
Example 2:
Input: n = 3, edges = {{0,1,-1},{1,2,-2},
{2,0,3}}
Output: 0
Explanation: The graph does not contain any
negative weight cycle.
Your Task:
You don't need to read or print anyhting. Your task is to complete the function isNegativeWeightCycle() which takes n and edges as input paramater and returns 1 if graph
contains negative weight cycle otherwise returns 0.
Expected Time Complexity: O(n*m)
Expected Space Compelxity: O(n)
*/
// Link --> https://practice.geeksforgeeks.org/problems/negative-weight-cycle3504/1
// Code:
class Solution
{
public int isNegativeWeightCycle(int n, int[][] edges)
{
int[] d = new int[n];
int[] p = new int[n];
Arrays.fill(d, 0);
Arrays.fill(p, -1);
int x = -1;
for(int i=0; i<n; i++)
{
x = -1;
for(int j = 0; j<edges.length; j++)
{
if(d[edges[j][0]] + edges[j][2] < d[edges[j][1]])
{
d[edges[j][1]] = d[edges[j][0]] + edges[j][2];
p[edges[j][1]] = edges[j][0];
x = edges[j][1];
}
}
}
if(x == -1)
return 0;
else
return 1;
}
}