forked from vedant781999/Array_operation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBellman-ford-Algorithm-Graph.cpp
More file actions
51 lines (45 loc) · 1.07 KB
/
Bellman-ford-Algorithm-Graph.cpp
File metadata and controls
51 lines (45 loc) · 1.07 KB
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
// single source shortest path finding algorithm
// works on negative weight also
#include<bits/stdc++.h>
using namespace std;
const int INF=1e9;
int main()
{
int n,m;
cin>>n>>m;
vector<vector<int>> edges;
for(int i=0;i<m;i++)
{
int u,v,w;
cin>>u>>v>>w;
edges.push_back({u,v,w});
}
int src;
cin>>src;
vector<int> dist(n,INF);
dist[src]=0;
// because agar cycle nhi hui at max n-1 edges me hi ans aa jata he if not than cycle is present
for(int iter=0;iter<n-1;iter++)
{
bool cycle = false;
for(auto e : edges)
{
int u = e[0];
int v = e[1];
int w = e[2];
dist[v] = min(dist[v],w+dist[u]);
if(dist[v]>w+dist[u])
{
cycle = true;
cout<<"Contains a negative cycle";
return 0;
}
}
}
for(auto i : dist)
{
cout<<i<<" ";
}
cout<<"\n";
return 0;
}