#include <bits/stdc++.h>
using namespace std;
#ifdef LOCAL
#include "debug.h"
#else
#define debug(...) 42
#endif
using T = array<long long, 3>;
const int N = 2e3 + 5, K = 1e5 + 5;
const long long inf = 1e18;
int n, m, t, k;
int x[K];
array<T, 4> d[N][N], s[4 * K];
vector<array<int, 2>> g[N];
void opt(array<T, 4> &buc, vector<T> cands) {
for (int i = 0; i < 4; ++i) {
buc[i] = {inf, -1, -1};
}
for (auto p : cands) {
buc[0] = min(buc[0], p);
}
for (auto p : cands) {
if (p[1] != buc[0][1] && p[2] != buc[0][2]) {
buc[1] = min(buc[1], p);
}
}
for (auto p : cands) {
if (p[1] != buc[0][1] && p[2] != buc[1][2]) {
buc[2] = min(buc[2], p);
}
if (p[1] != buc[1][1] && p[2] != buc[0][2]) {
buc[3] = min(buc[3], p);
}
}
}
bool add(array<T, 4> &buc, T cands) {
vector<T> nw;
for (int i = 0; i < 4; ++i) {
if (buc[i][0] != inf) {
nw.push_back(buc[i]);
}
}
nw.push_back(cands);
opt(buc, nw);
for (auto x : buc) {
if (x == cands) {
return 1;
}
}
return 0;
}
void dijkstra(int src, array<T, 4> *d) {
for (int i = 1; i <= n; ++i) {
for (int j = 0; j < 4; ++j) {
d[i][j] = {inf, -1, -1};
}
}
using dat = array<long long, 4>;
priority_queue<dat, vector<dat>, greater<dat>> pq;
for (auto [j, c] : g[src]) {
pq.push({c, j, j, src});
}
while (pq.size()) {
auto [c, u, s, t] = pq.top(); pq.pop();
if (!add(d[u], {c, s, t})) {
continue;
}
for (auto [v, w] : g[u]) {
if (v != t) {
pq.push({c + w, v, s, u});
}
}
}
}
void pul(int id) {
vector<T> cands;
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
if (s[id * 2][i][2] != s[id * 2 + 1][j][1]) {
cands.push_back({s[id * 2][i][0] + s[id * 2 + 1][j][0], s[id * 2][i][1], s[id * 2 + 1][j][2]});
}
}
}
opt(s[id], cands);
vector<T>().swap(cands);
}
void bld(int id = 1, int l = 1, int r = k - 1) {
if (l == r) {
int u = x[l], v = x[l + 1];
opt(s[id], {d[u][v][0], d[u][v][1], d[u][v][2], d[u][v][3]});
return;
}
int md = (l + r) / 2;
bld(id * 2, l, md);
bld(id * 2 + 1, md + 1, r);
pul(id);
}
void upd(int i, int id = 1, int l = 1, int r = k - 1) {
if (l == r) {
int u = x[l], v = x[l + 1];
opt(s[id], {d[u][v][0], d[u][v][1], d[u][v][2], d[u][v][3]});
return;
}
int md = (l + r) / 2;
if (i <= md) {
upd(i, id * 2, l, md);
} else {
upd(i, id * 2 + 1, md + 1, r);
}
pul(id);
}
int main() {
ios::sync_with_stdio(false); cin.tie(nullptr);
cin >> n >> m >> t >> k;
while (m--) {
int a, b, c; cin >> a >> b >> c;
g[a].push_back({b, c});
g[b].push_back({a, c});
}
for (int i = 1; i <= n; ++i) {
dijkstra(i, d[i]);
}
for (int i = 1; i <= k; ++i) {
cin >> x[i];
}
bld();
while (t--) {
int p, q; cin >> p >> q;
x[p] = q;
if (p > 1) {
upd(p - 1);
}
if (p < k) {
upd(p);
}
cout << (s[1][0][0] == inf ? -1 : s[1][0][0]) << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2648 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
0 ms |
4444 KB |
Output is correct |
18 |
Correct |
0 ms |
4444 KB |
Output is correct |
19 |
Correct |
0 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
0 ms |
2396 KB |
Output is correct |
22 |
Correct |
0 ms |
2396 KB |
Output is correct |
23 |
Correct |
0 ms |
2396 KB |
Output is correct |
24 |
Correct |
0 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2648 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
0 ms |
4444 KB |
Output is correct |
18 |
Correct |
0 ms |
4444 KB |
Output is correct |
19 |
Correct |
0 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
0 ms |
2396 KB |
Output is correct |
22 |
Correct |
0 ms |
2396 KB |
Output is correct |
23 |
Correct |
0 ms |
2396 KB |
Output is correct |
24 |
Correct |
0 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2392 KB |
Output is correct |
26 |
Correct |
2 ms |
6488 KB |
Output is correct |
27 |
Correct |
26 ms |
46176 KB |
Output is correct |
28 |
Correct |
25 ms |
46148 KB |
Output is correct |
29 |
Correct |
94 ms |
46420 KB |
Output is correct |
30 |
Correct |
81 ms |
46168 KB |
Output is correct |
31 |
Correct |
78 ms |
46228 KB |
Output is correct |
32 |
Correct |
77 ms |
46164 KB |
Output is correct |
33 |
Correct |
93 ms |
56656 KB |
Output is correct |
34 |
Correct |
101 ms |
56912 KB |
Output is correct |
35 |
Correct |
87 ms |
56404 KB |
Output is correct |
36 |
Correct |
89 ms |
56396 KB |
Output is correct |
37 |
Correct |
95 ms |
56656 KB |
Output is correct |
38 |
Correct |
99 ms |
64852 KB |
Output is correct |
39 |
Correct |
97 ms |
64596 KB |
Output is correct |
40 |
Correct |
99 ms |
64848 KB |
Output is correct |
41 |
Correct |
98 ms |
64884 KB |
Output is correct |
42 |
Correct |
92 ms |
65360 KB |
Output is correct |
43 |
Correct |
101 ms |
66148 KB |
Output is correct |
44 |
Correct |
99 ms |
66112 KB |
Output is correct |
45 |
Correct |
59 ms |
67920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2648 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
0 ms |
4444 KB |
Output is correct |
18 |
Correct |
0 ms |
4444 KB |
Output is correct |
19 |
Correct |
0 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
0 ms |
2396 KB |
Output is correct |
22 |
Correct |
0 ms |
2396 KB |
Output is correct |
23 |
Correct |
0 ms |
2396 KB |
Output is correct |
24 |
Correct |
0 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2392 KB |
Output is correct |
26 |
Correct |
2 ms |
6488 KB |
Output is correct |
27 |
Correct |
26 ms |
46176 KB |
Output is correct |
28 |
Correct |
25 ms |
46148 KB |
Output is correct |
29 |
Correct |
94 ms |
46420 KB |
Output is correct |
30 |
Correct |
81 ms |
46168 KB |
Output is correct |
31 |
Correct |
78 ms |
46228 KB |
Output is correct |
32 |
Correct |
77 ms |
46164 KB |
Output is correct |
33 |
Correct |
93 ms |
56656 KB |
Output is correct |
34 |
Correct |
101 ms |
56912 KB |
Output is correct |
35 |
Correct |
87 ms |
56404 KB |
Output is correct |
36 |
Correct |
89 ms |
56396 KB |
Output is correct |
37 |
Correct |
95 ms |
56656 KB |
Output is correct |
38 |
Correct |
99 ms |
64852 KB |
Output is correct |
39 |
Correct |
97 ms |
64596 KB |
Output is correct |
40 |
Correct |
99 ms |
64848 KB |
Output is correct |
41 |
Correct |
98 ms |
64884 KB |
Output is correct |
42 |
Correct |
92 ms |
65360 KB |
Output is correct |
43 |
Correct |
101 ms |
66148 KB |
Output is correct |
44 |
Correct |
99 ms |
66112 KB |
Output is correct |
45 |
Correct |
59 ms |
67920 KB |
Output is correct |
46 |
Correct |
149 ms |
39764 KB |
Output is correct |
47 |
Correct |
1714 ms |
79900 KB |
Output is correct |
48 |
Correct |
2240 ms |
112152 KB |
Output is correct |
49 |
Correct |
2670 ms |
142904 KB |
Output is correct |
50 |
Correct |
2630 ms |
142884 KB |
Output is correct |
51 |
Correct |
2630 ms |
143856 KB |
Output is correct |
52 |
Correct |
2791 ms |
143304 KB |
Output is correct |
53 |
Correct |
2815 ms |
143048 KB |
Output is correct |
54 |
Correct |
2792 ms |
144168 KB |
Output is correct |
55 |
Correct |
2776 ms |
143376 KB |
Output is correct |
56 |
Correct |
2934 ms |
161304 KB |
Output is correct |
57 |
Correct |
3006 ms |
181484 KB |
Output is correct |
58 |
Correct |
2995 ms |
203536 KB |
Output is correct |
59 |
Correct |
3021 ms |
227844 KB |
Output is correct |
60 |
Correct |
3128 ms |
252968 KB |
Output is correct |
61 |
Correct |
2960 ms |
280720 KB |
Output is correct |
62 |
Correct |
2922 ms |
309968 KB |
Output is correct |
63 |
Correct |
2858 ms |
341256 KB |
Output is correct |
64 |
Correct |
766 ms |
403280 KB |
Output is correct |
65 |
Correct |
783 ms |
403280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2648 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
0 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
0 ms |
2396 KB |
Output is correct |
12 |
Correct |
0 ms |
2396 KB |
Output is correct |
13 |
Correct |
0 ms |
2396 KB |
Output is correct |
14 |
Correct |
0 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
0 ms |
4444 KB |
Output is correct |
18 |
Correct |
0 ms |
4444 KB |
Output is correct |
19 |
Correct |
0 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
0 ms |
2396 KB |
Output is correct |
22 |
Correct |
0 ms |
2396 KB |
Output is correct |
23 |
Correct |
0 ms |
2396 KB |
Output is correct |
24 |
Correct |
0 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2392 KB |
Output is correct |
26 |
Correct |
2 ms |
6488 KB |
Output is correct |
27 |
Correct |
26 ms |
46176 KB |
Output is correct |
28 |
Correct |
25 ms |
46148 KB |
Output is correct |
29 |
Correct |
94 ms |
46420 KB |
Output is correct |
30 |
Correct |
81 ms |
46168 KB |
Output is correct |
31 |
Correct |
78 ms |
46228 KB |
Output is correct |
32 |
Correct |
77 ms |
46164 KB |
Output is correct |
33 |
Correct |
93 ms |
56656 KB |
Output is correct |
34 |
Correct |
101 ms |
56912 KB |
Output is correct |
35 |
Correct |
87 ms |
56404 KB |
Output is correct |
36 |
Correct |
89 ms |
56396 KB |
Output is correct |
37 |
Correct |
95 ms |
56656 KB |
Output is correct |
38 |
Correct |
99 ms |
64852 KB |
Output is correct |
39 |
Correct |
97 ms |
64596 KB |
Output is correct |
40 |
Correct |
99 ms |
64848 KB |
Output is correct |
41 |
Correct |
98 ms |
64884 KB |
Output is correct |
42 |
Correct |
92 ms |
65360 KB |
Output is correct |
43 |
Correct |
101 ms |
66148 KB |
Output is correct |
44 |
Correct |
99 ms |
66112 KB |
Output is correct |
45 |
Correct |
59 ms |
67920 KB |
Output is correct |
46 |
Correct |
149 ms |
39764 KB |
Output is correct |
47 |
Correct |
1714 ms |
79900 KB |
Output is correct |
48 |
Correct |
2240 ms |
112152 KB |
Output is correct |
49 |
Correct |
2670 ms |
142904 KB |
Output is correct |
50 |
Correct |
2630 ms |
142884 KB |
Output is correct |
51 |
Correct |
2630 ms |
143856 KB |
Output is correct |
52 |
Correct |
2791 ms |
143304 KB |
Output is correct |
53 |
Correct |
2815 ms |
143048 KB |
Output is correct |
54 |
Correct |
2792 ms |
144168 KB |
Output is correct |
55 |
Correct |
2776 ms |
143376 KB |
Output is correct |
56 |
Correct |
2934 ms |
161304 KB |
Output is correct |
57 |
Correct |
3006 ms |
181484 KB |
Output is correct |
58 |
Correct |
2995 ms |
203536 KB |
Output is correct |
59 |
Correct |
3021 ms |
227844 KB |
Output is correct |
60 |
Correct |
3128 ms |
252968 KB |
Output is correct |
61 |
Correct |
2960 ms |
280720 KB |
Output is correct |
62 |
Correct |
2922 ms |
309968 KB |
Output is correct |
63 |
Correct |
2858 ms |
341256 KB |
Output is correct |
64 |
Correct |
766 ms |
403280 KB |
Output is correct |
65 |
Correct |
783 ms |
403280 KB |
Output is correct |
66 |
Correct |
37 ms |
32048 KB |
Output is correct |
67 |
Correct |
257 ms |
118568 KB |
Output is correct |
68 |
Correct |
552 ms |
377940 KB |
Output is correct |
69 |
Correct |
693 ms |
378960 KB |
Output is correct |
70 |
Correct |
775 ms |
404560 KB |
Output is correct |
71 |
Correct |
2604 ms |
82056 KB |
Output is correct |
72 |
Correct |
3183 ms |
114316 KB |
Output is correct |
73 |
Correct |
3497 ms |
145808 KB |
Output is correct |
74 |
Correct |
3621 ms |
145720 KB |
Output is correct |
75 |
Correct |
3526 ms |
144740 KB |
Output is correct |
76 |
Correct |
3473 ms |
145244 KB |
Output is correct |
77 |
Correct |
3591 ms |
145368 KB |
Output is correct |
78 |
Correct |
3444 ms |
145180 KB |
Output is correct |
79 |
Correct |
3709 ms |
183976 KB |
Output is correct |
80 |
Correct |
3808 ms |
206200 KB |
Output is correct |
81 |
Correct |
3613 ms |
229272 KB |
Output is correct |
82 |
Correct |
3713 ms |
255668 KB |
Output is correct |
83 |
Correct |
3615 ms |
282552 KB |
Output is correct |
84 |
Correct |
3474 ms |
343920 KB |
Output is correct |
85 |
Correct |
1608 ms |
406156 KB |
Output is correct |