#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <unordered_map>
#include <functional>
#include <cstring>
#include <cmath>
#include <ctime>
#include <cstdlib>
using namespace std;
typedef long long llong;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<llong, llong> pll;
const llong inf = 1e18;
int n, m, T, L;
struct _edge {
int x, c, i;
_edge(int x, int c, int i) : x(x), c(c), i(i) {}
};
vector<_edge> edge[2001];
int a[2000];
int b[2000];
int c[2000];
llong dist[4000][2001][2];
int used[4000][2001][2];
struct pqNode {
int x, t;
llong c;
pqNode(int x, int t, llong c) : x(x), t(t), c(c) {}
bool operator<(const pqNode &p) const {
return c > p.c;
}
};
void dijkstra(int se, llong dist[2001][2], int used[2001][2]) {
for (int i = 1; i <= n; ++i) {
dist[i][0] = inf;
dist[i][1] = inf;
used[i][0] = -1;
used[i][1] = -1;
}
int s = (se & 1) ? a[se >> 1] : b[se >> 1];
priority_queue<pqNode> pq;
pq.emplace(s, used[s][0] = (se >> 1), dist[s][0] = c[se >> 1]);
while (!pq.empty()) {
pqNode x = pq.top();
pq.pop();
if (used[x.x][0] != x.t && used[x.x][1] != x.t) continue;
if (used[x.x][0] == x.t && dist[x.x][0] != x.c) continue;
if (used[x.x][1] == x.t && dist[x.x][1] != x.c) continue;
for (_edge i : edge[x.x]) {
if ((i.i >> 1) == x.t) continue;
llong d = x.c + i.c;
if (d < dist[i.x][0]) {
dist[i.x][1] = dist[i.x][0];
used[i.x][1] = used[i.x][0];
pq.emplace(i.x, used[i.x][0] = (i.i >> 1), dist[i.x][0] = d);
}
else if (used[i.x][0] != (i.i >> 1) && d < dist[i.x][1]) {
pq.emplace(i.x, used[i.x][1] = (i.i >> 1), dist[i.x][1] = d);
}
}
}
}
struct _distData {
int sx, ex;
llong c;
_distData() : c(inf) {}
_distData(int sx, int ex, llong c)
: sx(sx), ex(ex), c(c) {}
_distData operator+(const _distData &x) const {
if (ex == x.sx) return _distData();
llong d = c + x.c;
if (inf <= d) return _distData();
return _distData(sx, x.ex, d);
}
};
struct distData {
_distData x[5];
void push(int i, _distData p) {
if (i == 0) {
if (p.c < x[0].c) x[0] = p;
}
else if (i == 1) {
if (p.sx == x[0].sx) return;
if (p.c < x[1].c) x[1] = p;
}
else if (i == 2) {
if (p.sx == x[0].sx) return;
if (p.ex == x[1].ex) return;
if (p.c < x[2].c) x[2] = p;
}
else if (i == 3) {
if (p.ex == x[0].ex) return;
if (p.c < x[3].c) x[3] = p;
}
else {
if (p.ex == x[0].ex) return;
if (p.sx == x[3].sx) return;
if (p.c < x[4].c) x[4] = p;
}
}
void merge(const distData &a, const distData &b) {
for (int i = 0; i < 5; ++i) x[i] = _distData();
static _distData v[25];
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 5; ++j) {
v[5 * i + j] = a.x[i] + b.x[j];
}
}
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < 25; ++j) {
push(i, v[j]);
}
}
}
} dst[2001][2001], seg[1 << 18];
int xs[100001];
void init(int i, int s, int e) {
if (s + 1 == e) {
seg[i] = dst[xs[s]][xs[e]];
return;
}
int m = (s + e) / 2;
init(i << 1, s, m);
init(i << 1 | 1, m, e);
seg[i].merge(seg[i << 1], seg[i << 1 | 1]);
}
void update(int i, int s, int e, int x) {
if (s + 1 == e) {
seg[i] = dst[xs[s]][xs[e]];
return;
}
int m = (s + e) / 2;
if (x < m) update(i << 1, s, m, x);
else update(i << 1 | 1, m, e, x);
seg[i].merge(seg[i << 1], seg[i << 1 | 1]);
}
int main() {
scanf("%d%d%d%d", &n, &m, &T, &L);
for (int i = 0; i < m; ++i) {
scanf("%d%d%d", a + i, b + i, c + i);
edge[a[i]].emplace_back(b[i], c[i], i << 1);
edge[b[i]].emplace_back(a[i], c[i], i << 1 | 1);
}
for (int i = 0; i < (m << 1); ++i) {
dijkstra(i, dist[i], used[i]);
}
for (int i = 0; i < 5; ++i) {
for (int j = 0; j < (m << 1); ++j) {
int s = (j & 1) ? b[j >> 1] : a[j >> 1];
for (int k = 1; k <= n; ++k) {
dst[s][k].push(i, _distData(j >> 1, used[j][k][0], dist[j][k][0]));
dst[s][k].push(i, _distData(j >> 1, used[j][k][1], dist[j][k][1]));
}
}
}
for (int i = 1; i <= L; ++i) {
scanf("%d", xs + i);
}
init(1, 1, L);
for (int i = 0; i < T; ++i) {
int p, q;
scanf("%d%d", &p, &q);
xs[p] = q;
if (p < L) update(1, 1, L, p);
if (p > 1) update(1, 1, L, p - 1);
llong ans = seg[1].x[0].c;
printf("%lld\n", ans < inf ? ans : -1);
}
return 0;
}
Compilation message
wild_boar.cpp: In function 'int main()':
wild_boar.cpp:155:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d%d", &n, &m, &T, &L);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
wild_boar.cpp:157:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d%d", a + i, b + i, c + i);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
wild_boar.cpp:174:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", xs + i);
~~~~~^~~~~~~~~~~~~~
wild_boar.cpp:181:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d", &p, &q);
~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
253 ms |
334588 KB |
Output is correct |
2 |
Correct |
254 ms |
334524 KB |
Output is correct |
3 |
Correct |
254 ms |
334628 KB |
Output is correct |
4 |
Correct |
255 ms |
334592 KB |
Output is correct |
5 |
Correct |
256 ms |
334584 KB |
Output is correct |
6 |
Correct |
255 ms |
334492 KB |
Output is correct |
7 |
Correct |
254 ms |
334492 KB |
Output is correct |
8 |
Correct |
263 ms |
334684 KB |
Output is correct |
9 |
Correct |
255 ms |
334456 KB |
Output is correct |
10 |
Correct |
264 ms |
334588 KB |
Output is correct |
11 |
Correct |
255 ms |
334588 KB |
Output is correct |
12 |
Correct |
255 ms |
334456 KB |
Output is correct |
13 |
Correct |
254 ms |
334448 KB |
Output is correct |
14 |
Correct |
254 ms |
334584 KB |
Output is correct |
15 |
Correct |
254 ms |
334552 KB |
Output is correct |
16 |
Correct |
260 ms |
334584 KB |
Output is correct |
17 |
Correct |
255 ms |
334504 KB |
Output is correct |
18 |
Correct |
254 ms |
334584 KB |
Output is correct |
19 |
Correct |
255 ms |
334688 KB |
Output is correct |
20 |
Correct |
253 ms |
334456 KB |
Output is correct |
21 |
Correct |
252 ms |
334544 KB |
Output is correct |
22 |
Correct |
254 ms |
334584 KB |
Output is correct |
23 |
Correct |
254 ms |
334456 KB |
Output is correct |
24 |
Correct |
254 ms |
334452 KB |
Output is correct |
25 |
Correct |
255 ms |
334472 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
253 ms |
334588 KB |
Output is correct |
2 |
Correct |
254 ms |
334524 KB |
Output is correct |
3 |
Correct |
254 ms |
334628 KB |
Output is correct |
4 |
Correct |
255 ms |
334592 KB |
Output is correct |
5 |
Correct |
256 ms |
334584 KB |
Output is correct |
6 |
Correct |
255 ms |
334492 KB |
Output is correct |
7 |
Correct |
254 ms |
334492 KB |
Output is correct |
8 |
Correct |
263 ms |
334684 KB |
Output is correct |
9 |
Correct |
255 ms |
334456 KB |
Output is correct |
10 |
Correct |
264 ms |
334588 KB |
Output is correct |
11 |
Correct |
255 ms |
334588 KB |
Output is correct |
12 |
Correct |
255 ms |
334456 KB |
Output is correct |
13 |
Correct |
254 ms |
334448 KB |
Output is correct |
14 |
Correct |
254 ms |
334584 KB |
Output is correct |
15 |
Correct |
254 ms |
334552 KB |
Output is correct |
16 |
Correct |
260 ms |
334584 KB |
Output is correct |
17 |
Correct |
255 ms |
334504 KB |
Output is correct |
18 |
Correct |
254 ms |
334584 KB |
Output is correct |
19 |
Correct |
255 ms |
334688 KB |
Output is correct |
20 |
Correct |
253 ms |
334456 KB |
Output is correct |
21 |
Correct |
252 ms |
334544 KB |
Output is correct |
22 |
Correct |
254 ms |
334584 KB |
Output is correct |
23 |
Correct |
254 ms |
334456 KB |
Output is correct |
24 |
Correct |
254 ms |
334452 KB |
Output is correct |
25 |
Correct |
255 ms |
334472 KB |
Output is correct |
26 |
Correct |
255 ms |
335016 KB |
Output is correct |
27 |
Correct |
315 ms |
337112 KB |
Output is correct |
28 |
Correct |
314 ms |
337068 KB |
Output is correct |
29 |
Correct |
365 ms |
341420 KB |
Output is correct |
30 |
Correct |
373 ms |
341248 KB |
Output is correct |
31 |
Correct |
337 ms |
341112 KB |
Output is correct |
32 |
Correct |
346 ms |
341240 KB |
Output is correct |
33 |
Correct |
377 ms |
342008 KB |
Output is correct |
34 |
Correct |
370 ms |
342168 KB |
Output is correct |
35 |
Correct |
329 ms |
341948 KB |
Output is correct |
36 |
Correct |
349 ms |
342052 KB |
Output is correct |
37 |
Correct |
384 ms |
342220 KB |
Output is correct |
38 |
Correct |
369 ms |
342788 KB |
Output is correct |
39 |
Correct |
368 ms |
342748 KB |
Output is correct |
40 |
Correct |
373 ms |
342776 KB |
Output is correct |
41 |
Correct |
372 ms |
342800 KB |
Output is correct |
42 |
Correct |
335 ms |
343032 KB |
Output is correct |
43 |
Correct |
371 ms |
343588 KB |
Output is correct |
44 |
Correct |
368 ms |
343516 KB |
Output is correct |
45 |
Correct |
337 ms |
344184 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
253 ms |
334588 KB |
Output is correct |
2 |
Correct |
254 ms |
334524 KB |
Output is correct |
3 |
Correct |
254 ms |
334628 KB |
Output is correct |
4 |
Correct |
255 ms |
334592 KB |
Output is correct |
5 |
Correct |
256 ms |
334584 KB |
Output is correct |
6 |
Correct |
255 ms |
334492 KB |
Output is correct |
7 |
Correct |
254 ms |
334492 KB |
Output is correct |
8 |
Correct |
263 ms |
334684 KB |
Output is correct |
9 |
Correct |
255 ms |
334456 KB |
Output is correct |
10 |
Correct |
264 ms |
334588 KB |
Output is correct |
11 |
Correct |
255 ms |
334588 KB |
Output is correct |
12 |
Correct |
255 ms |
334456 KB |
Output is correct |
13 |
Correct |
254 ms |
334448 KB |
Output is correct |
14 |
Correct |
254 ms |
334584 KB |
Output is correct |
15 |
Correct |
254 ms |
334552 KB |
Output is correct |
16 |
Correct |
260 ms |
334584 KB |
Output is correct |
17 |
Correct |
255 ms |
334504 KB |
Output is correct |
18 |
Correct |
254 ms |
334584 KB |
Output is correct |
19 |
Correct |
255 ms |
334688 KB |
Output is correct |
20 |
Correct |
253 ms |
334456 KB |
Output is correct |
21 |
Correct |
252 ms |
334544 KB |
Output is correct |
22 |
Correct |
254 ms |
334584 KB |
Output is correct |
23 |
Correct |
254 ms |
334456 KB |
Output is correct |
24 |
Correct |
254 ms |
334452 KB |
Output is correct |
25 |
Correct |
255 ms |
334472 KB |
Output is correct |
26 |
Correct |
255 ms |
335016 KB |
Output is correct |
27 |
Correct |
315 ms |
337112 KB |
Output is correct |
28 |
Correct |
314 ms |
337068 KB |
Output is correct |
29 |
Correct |
365 ms |
341420 KB |
Output is correct |
30 |
Correct |
373 ms |
341248 KB |
Output is correct |
31 |
Correct |
337 ms |
341112 KB |
Output is correct |
32 |
Correct |
346 ms |
341240 KB |
Output is correct |
33 |
Correct |
377 ms |
342008 KB |
Output is correct |
34 |
Correct |
370 ms |
342168 KB |
Output is correct |
35 |
Correct |
329 ms |
341948 KB |
Output is correct |
36 |
Correct |
349 ms |
342052 KB |
Output is correct |
37 |
Correct |
384 ms |
342220 KB |
Output is correct |
38 |
Correct |
369 ms |
342788 KB |
Output is correct |
39 |
Correct |
368 ms |
342748 KB |
Output is correct |
40 |
Correct |
373 ms |
342776 KB |
Output is correct |
41 |
Correct |
372 ms |
342800 KB |
Output is correct |
42 |
Correct |
335 ms |
343032 KB |
Output is correct |
43 |
Correct |
371 ms |
343588 KB |
Output is correct |
44 |
Correct |
368 ms |
343516 KB |
Output is correct |
45 |
Correct |
337 ms |
344184 KB |
Output is correct |
46 |
Correct |
354 ms |
347236 KB |
Output is correct |
47 |
Correct |
1467 ms |
414388 KB |
Output is correct |
48 |
Correct |
1710 ms |
442324 KB |
Output is correct |
49 |
Correct |
1935 ms |
461092 KB |
Output is correct |
50 |
Correct |
1978 ms |
461244 KB |
Output is correct |
51 |
Correct |
2024 ms |
461352 KB |
Output is correct |
52 |
Correct |
2212 ms |
461440 KB |
Output is correct |
53 |
Correct |
2169 ms |
461340 KB |
Output is correct |
54 |
Correct |
2206 ms |
461420 KB |
Output is correct |
55 |
Correct |
2161 ms |
461436 KB |
Output is correct |
56 |
Correct |
2423 ms |
470780 KB |
Output is correct |
57 |
Correct |
2346 ms |
480104 KB |
Output is correct |
58 |
Correct |
2437 ms |
489616 KB |
Output is correct |
59 |
Correct |
2547 ms |
498820 KB |
Output is correct |
60 |
Correct |
2701 ms |
507924 KB |
Output is correct |
61 |
Correct |
2566 ms |
514364 KB |
Output is correct |
62 |
Correct |
2502 ms |
520400 KB |
Output is correct |
63 |
Correct |
2499 ms |
523384 KB |
Output is correct |
64 |
Correct |
1226 ms |
523480 KB |
Output is correct |
65 |
Correct |
1196 ms |
523356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
253 ms |
334588 KB |
Output is correct |
2 |
Correct |
254 ms |
334524 KB |
Output is correct |
3 |
Correct |
254 ms |
334628 KB |
Output is correct |
4 |
Correct |
255 ms |
334592 KB |
Output is correct |
5 |
Correct |
256 ms |
334584 KB |
Output is correct |
6 |
Correct |
255 ms |
334492 KB |
Output is correct |
7 |
Correct |
254 ms |
334492 KB |
Output is correct |
8 |
Correct |
263 ms |
334684 KB |
Output is correct |
9 |
Correct |
255 ms |
334456 KB |
Output is correct |
10 |
Correct |
264 ms |
334588 KB |
Output is correct |
11 |
Correct |
255 ms |
334588 KB |
Output is correct |
12 |
Correct |
255 ms |
334456 KB |
Output is correct |
13 |
Correct |
254 ms |
334448 KB |
Output is correct |
14 |
Correct |
254 ms |
334584 KB |
Output is correct |
15 |
Correct |
254 ms |
334552 KB |
Output is correct |
16 |
Correct |
260 ms |
334584 KB |
Output is correct |
17 |
Correct |
255 ms |
334504 KB |
Output is correct |
18 |
Correct |
254 ms |
334584 KB |
Output is correct |
19 |
Correct |
255 ms |
334688 KB |
Output is correct |
20 |
Correct |
253 ms |
334456 KB |
Output is correct |
21 |
Correct |
252 ms |
334544 KB |
Output is correct |
22 |
Correct |
254 ms |
334584 KB |
Output is correct |
23 |
Correct |
254 ms |
334456 KB |
Output is correct |
24 |
Correct |
254 ms |
334452 KB |
Output is correct |
25 |
Correct |
255 ms |
334472 KB |
Output is correct |
26 |
Correct |
255 ms |
335016 KB |
Output is correct |
27 |
Correct |
315 ms |
337112 KB |
Output is correct |
28 |
Correct |
314 ms |
337068 KB |
Output is correct |
29 |
Correct |
365 ms |
341420 KB |
Output is correct |
30 |
Correct |
373 ms |
341248 KB |
Output is correct |
31 |
Correct |
337 ms |
341112 KB |
Output is correct |
32 |
Correct |
346 ms |
341240 KB |
Output is correct |
33 |
Correct |
377 ms |
342008 KB |
Output is correct |
34 |
Correct |
370 ms |
342168 KB |
Output is correct |
35 |
Correct |
329 ms |
341948 KB |
Output is correct |
36 |
Correct |
349 ms |
342052 KB |
Output is correct |
37 |
Correct |
384 ms |
342220 KB |
Output is correct |
38 |
Correct |
369 ms |
342788 KB |
Output is correct |
39 |
Correct |
368 ms |
342748 KB |
Output is correct |
40 |
Correct |
373 ms |
342776 KB |
Output is correct |
41 |
Correct |
372 ms |
342800 KB |
Output is correct |
42 |
Correct |
335 ms |
343032 KB |
Output is correct |
43 |
Correct |
371 ms |
343588 KB |
Output is correct |
44 |
Correct |
368 ms |
343516 KB |
Output is correct |
45 |
Correct |
337 ms |
344184 KB |
Output is correct |
46 |
Correct |
354 ms |
347236 KB |
Output is correct |
47 |
Correct |
1467 ms |
414388 KB |
Output is correct |
48 |
Correct |
1710 ms |
442324 KB |
Output is correct |
49 |
Correct |
1935 ms |
461092 KB |
Output is correct |
50 |
Correct |
1978 ms |
461244 KB |
Output is correct |
51 |
Correct |
2024 ms |
461352 KB |
Output is correct |
52 |
Correct |
2212 ms |
461440 KB |
Output is correct |
53 |
Correct |
2169 ms |
461340 KB |
Output is correct |
54 |
Correct |
2206 ms |
461420 KB |
Output is correct |
55 |
Correct |
2161 ms |
461436 KB |
Output is correct |
56 |
Correct |
2423 ms |
470780 KB |
Output is correct |
57 |
Correct |
2346 ms |
480104 KB |
Output is correct |
58 |
Correct |
2437 ms |
489616 KB |
Output is correct |
59 |
Correct |
2547 ms |
498820 KB |
Output is correct |
60 |
Correct |
2701 ms |
507924 KB |
Output is correct |
61 |
Correct |
2566 ms |
514364 KB |
Output is correct |
62 |
Correct |
2502 ms |
520400 KB |
Output is correct |
63 |
Correct |
2499 ms |
523384 KB |
Output is correct |
64 |
Correct |
1226 ms |
523480 KB |
Output is correct |
65 |
Correct |
1196 ms |
523356 KB |
Output is correct |
66 |
Correct |
334 ms |
335836 KB |
Output is correct |
67 |
Correct |
1014 ms |
398456 KB |
Output is correct |
68 |
Correct |
1208 ms |
522616 KB |
Output is correct |
69 |
Correct |
1734 ms |
523488 KB |
Output is correct |
70 |
Correct |
2571 ms |
524776 KB |
Output is correct |
71 |
Correct |
2986 ms |
416648 KB |
Output is correct |
72 |
Correct |
3202 ms |
444764 KB |
Output is correct |
73 |
Correct |
3718 ms |
463880 KB |
Output is correct |
74 |
Correct |
3768 ms |
464068 KB |
Output is correct |
75 |
Correct |
3763 ms |
464016 KB |
Output is correct |
76 |
Correct |
3457 ms |
463588 KB |
Output is correct |
77 |
Correct |
3279 ms |
463420 KB |
Output is correct |
78 |
Correct |
3173 ms |
463568 KB |
Output is correct |
79 |
Correct |
3901 ms |
482632 KB |
Output is correct |
80 |
Correct |
3918 ms |
492160 KB |
Output is correct |
81 |
Correct |
3804 ms |
501008 KB |
Output is correct |
82 |
Correct |
4066 ms |
510608 KB |
Output is correct |
83 |
Correct |
3958 ms |
516228 KB |
Output is correct |
84 |
Correct |
3985 ms |
526068 KB |
Output is correct |
85 |
Correct |
2569 ms |
526180 KB |
Output is correct |