-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathboj_11657.cpp
More file actions
66 lines (56 loc) · 1.14 KB
/
boj_11657.cpp
File metadata and controls
66 lines (56 loc) · 1.14 KB
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
//<11657>번 : <타임머신>
#include <bits/stdc++.h>
#define INF 100000000
using namespace std;
int n, m;
long long d[501];
vector<pair<pair<int,int>, int>> edges;
int bellmanFord()
{
fill(d, d+n+1, INF);
d[1] = 0;
for(int i = 0; i < n-1; i++)
{
for(auto e : edges)
{
int u = e.first.first;
int v = e.first.second;
int w = e.second;
if(d[u] == INF) continue;
d[v] = min(d[v], d[u] + w);
}
}
for(auto e : edges)
{
int u = e.first.first;
int v = e.first.second;
int w = e.second;
if(d[u] == INF) continue;
if(d[u] + w < d[v])
return -1;
}
return 0;
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cin >> n >> m;
for(int i = 0; i < m; i++)
{
int a, b, c;
cin >> a >> b >> c;
edges.push_back({{a,b},c});
}
if(bellmanFord() == -1)
{
cout << -1 << '\n';
return 0;
}
for(int i = 2; i <= n; i++)
{
if(d[i] == INF) cout << -1 << '\n';
else cout << d[i] << '\n';
}
return 0;
}