-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathbellmanFord.cpp
74 lines (70 loc) · 1.84 KB
/
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <list>
#include <climits>
#include <unordered_map>
using namespace std;
class Graph {
int V;
list<pair<pair<int, int>,int>> l;
public:
Graph(int V){
this->V=V;
}
void addEdge(int x,int y,int wt){
l.push_back({{x,y},wt});
}
void bellmanFord(int src){
int* parent=new int[V];
int* dist=new int[V];
for(int i=0;i<V;i++){
dist[i]=INT_MAX;
parent[i]=i;
}
dist[src]=0;
bool flag;
for(int i=0;i<V-1;i++){
flag=false;
for(auto edge:l){
int a=edge.first.first;
int b=edge.first.second;
int d=edge.second;
if(dist[b]>dist[a]+d){
flag=true;
dist[b]=dist[a]+d;
parent[b]=a;
}
}
if(!flag){
break;
}
}
if(flag){
for(auto edge:l){
int a=edge.first.first;
int b=edge.first.second;
int d=edge.second;
if(dist[b]>dist[a]+d){
cout<<"Negative weighted cycle is present."<<endl;
return;
}
}
}
for(int i=0;i<V;i++){
cout<<parent[i]<<" ->"<<i<<"-"<<dist[i]<<",";
}
}
};
int main(){
Graph g(5);
g.addEdge(0,1,6);
g.addEdge(0,2,7);
g.addEdge(1,2,8);
g.addEdge(1,3,-4);
g.addEdge(1,4,5);
g.addEdge(2,3,9);
g.addEdge(2,4,-3);
g.addEdge(3,4,7);
g.addEdge(3,0,2);
g.addEdge(4,1,-2);
g.bellmanFord(0);
}