-
Notifications
You must be signed in to change notification settings - Fork 56
/
Copy pathBellmanFord.cpp
56 lines (50 loc) · 1.02 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
#include "bits/stdc++.h"
using namespace std;
const int INF = 1e9;
int main(){
int n,m;cin>>n>>m;
vector<vector<int>> edges;
cout<<"u v w"<<endl;
for (int i = 0; i < m; i++)
{
int u,v,w;
cin>>u>>v>>w;
edges.push_back({u,v,w});
}
cout<<endl;
cout<<"SOURCE : "<<endl;
int src; cin>>src;
vector<int> dist(n,INF);
dist[src] = 0;
for (int iter = 0; iter < n-1; iter++)
{
bool change = false;
for(auto e : edges){
int u = e[0];
int v = e[1];
int w = e[2];
//for detection of cycle
if(dist[v] > w + dist[u]){
change = true;
}
dist[v] = min(dist[v], w + dist[u]);
}
}
cout<<"VERTEX DISTANCE FROM SOURCE"<<endl;
for(int i=0;i<n;i++){
cout<<i<<" "<<dist[i]<<endl;
}
cout<<endl;
return 0;
}
// 5 8
// 1 2 3
// 3 2 5
// 1 3 2
// 3 1 1
// 1 4 2
// 0 2 4
// 4 3 -3
// 0 1 -1
// 0
// 0 -1 2 -2 1