-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bellman-Ford.cpp
62 lines (55 loc) · 1.15 KB
/
Bellman-Ford.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
/*
In the name of Allah
Code by jahanaraco
Single Source Shortest Path
Bellman-Ford algorithm for Directed Weighted Graphs
O(N * E)
*/
#include <iostream>
#include <vector>
using namespace std;
const int MAXN = 1000;
const int INF = 1000 * 1000 * 1000;
vector<pair<int, int> > adj[MAXN];
int n, m, source, dist[MAXN];
bool relax(int v, int u, int w){
if(dist[v] == INF)
return false;
if(dist[u] > dist[v] + w){
dist[u] = dist[v] + w;
return true;
}
return false;
}
int main(){
cin >> n >> m >> source;
for(int i = 0; i < m; i++){
int v, u, w;
cin >> v >> u >> w;
adj[v].push_back(make_pair(u, w));
}
for(int i = 1; i <= n; i++)
dist[i] = INF;
dist[source] = 0;
bool negative_cycle = false
for(int i = 0; i <= n; i++)
for(int v = 1; v <= n; i++)
for(int j = 0; j < adj[v].size(); j++){
if(relax(v, adj[v][j].first, adj[v][j].second))
if(i == n){
negative_cycle = true;
break;
}
}
if(negative_cycle)
cout << "Negative Cycle Found!" << endl;
else{
for(int i = 1; i <= n; i++)
if(dist[i] != INF)
cout << dist[i] << ' ';
else
cout << "not find ";
cout << endl;
}
return 0;
}