-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathB.cpp
75 lines (75 loc) · 1.79 KB
/
B.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
#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e16;
const int maxn = 1e3 + 10;
const int maxm = 1e4 + 10;
int l[maxm], r[maxm], w[maxm];
vector <int> g[maxn];
int par[maxn][maxm];
long long dp[maxn][maxn];
struct node {
int vertex, edge;
long long dist;
node (int vertex, int edge, long long dist) : vertex(vertex), edge(edge), dist(dist) {}
node () {}
bool operator < (node other) const {
return dist > other.dist;
}
};
int main() {
int n, m, L, s, t;
scanf("%d %d %d %d %d", &n, &m, &L, &s, &t);
for(int i = 1; i <= m; i++) {
scanf("%d %d %d", &l[i], &r[i], &w[i]);
g[l[i]].push_back(i);
g[r[i]].push_back(i);
}
for(int i = 0; i <= n; i++) {
for(int j = 0; j <= n; j++) {
dp[i][j] = inf;
}
}
priority_queue <node> Q;
Q.emplace(s, 0, 0);
dp[s][0] = 0;
while(!Q.empty()) {
node x = Q.top();
Q.pop();
if(dp[x.vertex][x.edge] != x.dist || x.edge == n) continue;
for(auto e : g[x.vertex]) {
int y = l[e] ^ r[e] ^ x.vertex;
if(x.dist + max(1, w[e]) < dp[y][x.edge + (w[e] == 0)]) {
dp[y][x.edge + (w[e] == 0)] = x.dist + max(1, w[e]);
par[y][x.edge + (w[e] == 0)] = e;
Q.emplace(y, x.edge + (w[e] == 0), x.dist + max(1, w[e]));
}
}
}
int edge = 0;
int cur = t;
while(edge < n && dp[cur][edge] > L) ++edge;
if (edge >= n || (edge == 0 && dp[cur][edge] != L)) {
puts("NO");
exit(0);
}
long long add = L - dp[cur][edge];
while(cur != s) {
int e = par[cur][edge];
int next = l[e] ^ r[e] ^ cur;
if(w[e] == 0) {
--edge;
w[e] = 1;
w[e] += add;
add = 0;
}
cur = next;
}
puts("YES");
for(int i = 1; i <= m; i++) {
if(w[i] == 0) {
w[i] = L + 1;
}
printf("%d %d %d\n", l[i], r[i], w[i]);
}
return 0;
}