-
Notifications
You must be signed in to change notification settings - Fork 0
/
bellmanFord.cpp
45 lines (42 loc) · 843 Bytes
/
bellmanFord.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
#include<bits/stdc++.h>
using namespace std;
struct edge {
int a,b,w;
};
void print(auto &v){
for(auto i:v)
cout<<i<<" ";
cout<<"\n";
}
bool bf(vector<edge> &G,vector<int> &d,vector<int> &p,int n,int m,int src){
d[src] = 0;
bool flag = 0;
for(int i=0;i<n;i++){
flag = 0;
for(int j = 0;j < m;j++){
if(d[G[j].a]<INT_MAX && d[G[j].b]>d[G[j].a]+G[j].w){
d[G[j].b] = min(d[G[j].b],d[G[j].a]+G[j].w);
p[G[j].b] = G[j].a;
flag = 1;
}
}
if(!flag) break;
}
return flag;
}
int main(){
int noEdge,noVer,src;
cin>>noVer>>noEdge;
vector<edge> G(noEdge);
vector<int> dist(noVer,INT_MAX),par(noVer,-1);
for(int i=0;i<noEdge;i++)
cin>>G[i].a>>G[i].b>>G[i].w;
cin>>src;
bool isNegCycle = bf(G,dist,par,noVer,noEdge,src);
if(isNegCycle)
cout<<"Negative Cycle Found\n";
else{
print(dist);
print(par);
}
}