-
Notifications
You must be signed in to change notification settings - Fork 0
/
11657_TimeMachine.cpp
67 lines (56 loc) · 1.03 KB
/
11657_TimeMachine.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
#include<iostream>
#include<vector>
#define inf 999999999
using namespace std;
struct node {
int s;
int e;
int w;
node(int S, int E, int W) {
s = S;
e = E;
w = W;
}
};
int main() {
int N, M, A, B, C;
cin >> N >> M;
vector<node>graph;
for (int i = 0; i < M; i++) {
cin >> A >> B >> C;
graph.push_back(node(A, B, C));
}
long long dis[501];
fill_n(dis, 501, inf);
dis[1] = 0;
for (int i = 0; i < N-1; i++) {
for (int j = 0; j < M; j++) {
int start = graph[j].s;
int end = graph[j].e;
int weight = graph[j].w;
if (dis[start]!=inf && dis[start] + weight < dis[end])
dis[end] = dis[start] + weight;
}
}
bool stopped = false;
for (int i = 0; i < M; i++) {
int start = graph[i].s;
int end = graph[i].e;
int weight = graph[i].w;
if (dis[start] != inf && dis[start] + weight < dis[end]){
stopped = true;
break;
}
}
if (stopped)
cout << "-1\n";
else {
for (int i = 2; i <= N; i++) {
if (dis[i] != inf)
cout << dis[i] << "\n";
else
cout << "-1\n";
}
}
return 0;
}