-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathE.cpp
94 lines (81 loc) · 1.85 KB
/
E.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Created by Walingar on 06.03.2017.
//
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <climits>
long long n, m, s, f;
const unsigned long long MAX_N = 2017;
using namespace std;
ifstream in("path.in");
ofstream out("path.out");
long long ans = 0;
struct edge {
long long from, to, w;
edge(long long x, long long y, long long z) {
from = x;
to = y;
w = z;
}
};
vector<edge> edges;
vector<vector<long long>> edgesDFS(MAX_N);
void input() {
in >> n >> m >> s;
--s;
for (long long i = 0; i < m; ++i) {
long long from, to, w;
in >> from >> to >> w;
--from;
--to;
edges.push_back(edge(from, to, w));
edgesDFS[from].push_back(to);
}
}
long long INF = LLONG_MAX / 2;
vector<long long> d(MAX_N, INF);
vector<long long> ok(MAX_N, true);
vector<bool> was(MAX_N);
void reset() {
for (long long i = 0; i < n; ++i) {
was[i] = false;
}
}
void dfs(long long v) {
was[v] = true;
ok[v] = false;
for (auto to: edgesDFS[v]) {
if (!was[to])
dfs(to);
}
}
void solve() {
d[s] = 0;
long long x;
for (long long i = 0; i < n; ++i) {
for (long long j = 0; j < m; ++j)
if (d[edges[j].from] < INF) {
if (d[edges[j].to] > d[edges[j].from] + edges[j].w) {
d[edges[j].to] = max(-INF, d[edges[j].from] + edges[j].w);
if (i == n - 1) {
reset();
dfs(edges[j].to);
}
}
}
}
}
void output() {
for (long long i = 0; i < n; ++i) {
if (d[i] == INF) out << "*" << "\n";
else if (!ok[i]) out << "-" << "\n";
else out << d[i] << "\n";
}
}
int main() {
input();
solve();
output();
return 0;
}