-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFord-Bellman.cpp
More file actions
43 lines (39 loc) · 870 Bytes
/
Ford-Bellman.cpp
File metadata and controls
43 lines (39 loc) · 870 Bytes
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
#include <iostream>
#include <cstdio>
#include <vector>
#include <limits.h>
#define INF INT_MAX
typedef struct edge{
int u,v,w;
} edge;
using namespace std;
int main() {
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
int n,m,s;
cin >> n >> m >> s;
s--;
vector <edge> g;
for (int i = 0; i < m; ++i){
int tu, tv, tw;
cin >> tu >> tv >> tw;
g.push_back({tu - 1, tv - 1, tw});
}
vector <int> d(n, INF);
d[s] = 0;
for (int i = 0; i < n - 1; ++i){
for (int j = 0; j < m; ++j){
if (d[g[j].u] < INF){
d[g[j].v] = min (d[g[j].v], d[g[j].u] + g[j].w);
}
}
}
for (int i = 0; i < n; ++i){
if (d[i] != INF){
cout << d[i];
} else {
cout << ' ';
}
cout << ' ';
}
}