-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbellmanFord.cpp
69 lines (54 loc) · 1.58 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
#include <iostream>
#include <vector>
#define INF 987654321
using namespace std;
int N, M;
vector<pair<int, pair<int, int>>> edge;
vector<long long> dist;
// 음수 사이클이 있을 경우 텅 빈 배열을 반환한다.
bool bellmanFord(int start) {
dist.assign(N + 1, INF);
dist[start] = 0;
// 모든 간선을 살펴본다.
for (int i = 1; i < N; i++) {
for (int j = 0; j < edge.size(); j++) {
int here = edge[j].first;
int next = edge[j].second.first;
int distance = edge[j].second.second;
//(here, next)간선을 따라 완화를 시도한다.
if(dist[here] == INF) continue;
if (dist[next] > dist[here] + distance) {
dist[next] = dist[here] + distance;
}
}
}
for (int i = 0; i < edge.size(); i++) {
int here = edge[i].first;
int next = edge[i].second.first;
int distance = edge[i].second.second;
//(here, next)간선을 따라 완화를 시도한다.
if(dist[here] == INF) continue;
if (dist[next] > dist[here] + distance) {
return false;
}
}
return true;
}
int main() {
int A, B, C;
cin >> N >> M;
edge.assign(M, pair<int, pair<int, int>>());
for(int i = 0; i < M; i++) {
cin >> A >> B >> C;
edge[i] = {A, {B, C}};
}
if(bellmanFord(1)) {
for(int i = 2; i <= N; i++) {
if(dist[i] == INF) cout << -1 << '\n';
else cout << dist[i] << '\n';
}
} else {
cout << -1;
}
return 0;
}