-
Notifications
You must be signed in to change notification settings - Fork 0
/
bellmanFord.cpp
77 lines (66 loc) · 1.59 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
75
76
77
#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define yes cout << "YES\n"
#define no cout << "NO\n"
#define endl "\n"
#define ll long long
#define inf 1000000009
ll mod = INT_MAX;
queue<pair<pair<int,int>,int>>q;
int dis[1001];
int main(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin); //freopen("output.txt", "w", stdout);
#endif
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int nodes,edges; cin >> nodes >> edges;
for(int i=0;i<edges;i++){
int u,v,c; cin >> u >> v >> c;
q.push({{u,v},c});
}
int source = 0, dest = nodes;
for(int i=0;i<=nodes;i++)dis[i] = inf;
dis[source] = 0;
for(int i=0;i<nodes;i++){
for(int j=0;j<edges;j++){
int u = q.front().first.first;
int v = q.front().first.second;
int c = q.front().second;
q.pop();
q.push({{u,v},c});
if(dis[u]!=inf && dis[u]+c<dis[v]){
dis[v] = dis[u] + c;
}
}
}
cout << "N-1 th relaxation:\n";
for(int i=0;i<=nodes;i++)cout << dis[i] << " ";
cout << endl;
for(int j=0;j<edges;j++){
int u = q.front().first.first;
int v = q.front().first.second;
int c = q.front().second;
q.pop();
q.push({{u,v},c});
if(dis[u]!=inf && dis[u]+c<dis[v]){
dis[v] = dis[u] + c;
}
}
cout << "N th relaxation:\n";
for(int i=0;i<=nodes;i++)cout << dis[i] << " ";
cout << endl;
}
/*
4 8
0 1 -1
0 2 5
1 2 3
1 3 2
1 4 2
3 2 5
3 1 1
2 0 -2
*/