#include <bits/stdc++.h>
#define eb emplace_back
#define INFLL (0x3f3f3f3f3f3f3f3fll)
using namespace std;
typedef long long ll;
typedef pair<ll, int> pli;
const int MAXK = 100005;
ll dp[MAXK*3][7][7];
vector<int> G[2005];
ll F[2005][2005][7];
int FVA[2005][2005][7], FVB[2005][2005][7];
int FA[2005][2005][5], FB[2005][2005][5];
ll E[4005][4005];
int A[2005], B[2005], C[2005];
int D[MAXK];
int N, M, K, Q;
ll get() { return *min_element(dp[1][0], dp[1][6]+7); }
void cal(int i, int s, int e) {
if(s == e) {
for(int a = 0; a < 7; a++) for(int b = 0; b < 7; b++)
dp[i][a][b] = a == b ? F[D[s]][D[s+1]][a] : INFLL;
return;
}
int m = (s+e) >> 1;
for(int a = 0; a < 7; a++) for(int b = 0; b < 7; b++) {
ll &ret = dp[i][a][b]; ret = INFLL;
for(int c = 0; c < 7; c++) for(int d = 0; d < 7; d++) {
if(FVB[D[m]][D[m+1]][c] == FVA[D[m+1]][D[m+2]][d]) continue;
ll t = dp[i<<1][a][c] + dp[i<<1|1][d][b];
if(t < ret) ret = t;
}
}
}
void initSeg(int i, int s, int e) {
if(s != e) {
int m = (s+e) >> 1;
initSeg(i<<1, s, m);
initSeg(i<<1|1, m+1, e);
}
cal(i, s, e);
}
void upd(int i, int s, int e, int x1, int x2) {
if(s == e) {
cal(i, s, e);
return;
}
int m = (s+e) >> 1;
if(x1 <= m) upd(i<<1, s, m, x1, m < x2 ? x1 : x2);
if(m < x2) upd(i<<1|1, m+1, e, x1 <= m ? x2 : x1, x2);
cal(i, s, e);
}
int getSidx(int e) { return ((e&1) ? B : A)[e>>1]; }
int getEidx(int e) { return ((e&1) ? A : B)[e>>1]; }
int bfE[2005];
bitset<2005> chk;
void doDijk(ll E[], int se) {
chk.reset();
priority_queue<pli, vector<pli>, greater<pli>> PQ;
fill(E, E+(M<<1), INFLL);
E[se] = 0;
PQ.emplace(0, se);
for(ll dst; !PQ.empty();) {
int eidx; tie(dst, eidx) = PQ.top(); PQ.pop();
if(E[eidx] < dst) continue;
int v = getEidx(eidx);
if(chk[v]) {
int e = bfE[v]^1;
if(e != (eidx^1)) {
ll ndst = dst + C[e>>1];
if(ndst < E[e]) {
E[e] = ndst;
PQ.emplace(ndst, e);
}
}
} else {
chk[v] = true;
bfE[v] = eidx;
for(int e : G[v]) if(e != (eidx^1)) {
ll ndst = dst + C[e>>1];
if(E[e] <= ndst) continue;
E[e] = ndst;
PQ.emplace(ndst, e);
}
}
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> N >> M >> Q >> K;
for(int i = 0; i < M; i++) {
cin >> A[i] >> B[i] >> C[i];
if(A[i] > B[i]) swap(A[i], B[i]);
G[A[i]].eb(i<<1);
G[B[i]].eb(i<<1|1);
}
for(int i = 1; i <= K; i++) cin >> D[i];
for(int i = M<<1; i--;) doDijk(E[i], i);
for(int i = 1; i <= N; i++) for(int j = 1; j <= N; j++) if(j != i) {
fill(F[i][j], F[i][j]+7, INFLL);
fill(FA[i][j], FA[i][j]+5, -1);
fill(FB[i][j], FB[i][j]+5, -1);
// Case 0
for(int e : G[i]) if(getEidx(e) == j) F[i][j][0] = C[e>>1];
// Case 1
int &a0 = FA[i][j][0], &b0 = FB[i][j][0], ea0 = -1, eb0 = -1;
ll &c1 = F[i][j][1];
for(int ea : G[i]) for(int eb : G[j]) {
ll c = E[ea][eb^1] + C[ea>>1];
if(c1 <= c) continue;
a0 = getEidx(ea); b0 = getEidx(eb); c1 = c;
ea0 = ea; eb0 = eb;
}
if(ea0 < 0) continue;
eb0 ^= 1;
// Case 2
int &a1 = FA[i][j][1];
ll &c2 = F[i][j][2];
for(int ea : G[i]) if(ea != ea0) {
ll c = E[ea][eb0] + C[ea>>1];
if(c2 <= c) continue;
a1 = getEidx(ea); c2 = c;
}
// Case 3
int &b1 = FB[i][j][1];
ll &c3 = F[i][j][3];
for(int eb : G[j]) if(eb != (eb0^1)) {
ll c = E[ea0][eb^1] + C[ea0>>1];
if(c3 <= c) continue;
b1 = getEidx(eb); c3 = c;
}
// Case 4
int &a2 = FA[i][j][2], &b2 = FB[i][j][2];
ll &c4 = F[i][j][4];
for(int ea : G[i]) for(int eb : G[j]) if(ea != ea0 && eb != (eb0^1)) {
ll c = E[ea][eb^1] + C[ea>>1];
if(c4 <= c) continue;
a2 = getEidx(ea); b2 = getEidx(eb); c4 = c;
}
if(a2 < 0) continue;
// Case 5
int &a3 = FA[i][j][3], &b3 = FB[i][j][3];
ll &c5 = F[i][j][5];
for(int ea : G[i]) for(int eb : G[j]) {
int av = getEidx(ea), bv = getEidx(eb);
if(av == a0 || bv == b2) continue;
ll c = E[ea][eb^1] + C[ea>>1];
if(c5 <= c) continue;
a3 = av; b3 = bv; c5 = c;
}
// Case 6
int &a4 = FA[i][j][4], &b4 = FB[i][j][4];
ll &c6 = F[i][j][6];
for(int ea : G[i]) for(int eb : G[j]) {
int av = getEidx(ea), bv = getEidx(eb);
if(av == a2 || bv == b0) continue;
ll c = E[ea][eb^1] + C[ea>>1];
if(c6 <= c) continue;
a4 = av; b4 = bv; c6 = c;
}
}
for(int i = 1; i <= N; i++) for(int j = 1; j <= N; j++) {
int *fa = FA[i][j], *fb = FB[i][j];
int *fva = FVA[i][j], *fvb = FVB[i][j];
fva[0] = j; fvb[0] = i;
fva[1] = fa[0]; fvb[1] = fb[0];
fva[2] = fa[1]; fvb[2] = fb[0];
fva[3] = fa[0]; fvb[3] = fb[1];
fva[4] = fa[2]; fvb[4] = fb[2];
fva[5] = fa[3]; fvb[5] = fb[3];
fva[6] = fa[4]; fvb[6] = fb[4];
}
initSeg(1, 1, K-1);
for(int a, b; Q--;) {
cin >> a >> b;
D[a] = b;
upd(1, 1, K-1, a-1, a);
ll t = get();
if(INFLL <= t) cout << "-1\n";
else cout << t << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
632 KB |
Output is correct |
2 |
Correct |
5 ms |
632 KB |
Output is correct |
3 |
Correct |
5 ms |
632 KB |
Output is correct |
4 |
Correct |
5 ms |
632 KB |
Output is correct |
5 |
Correct |
5 ms |
632 KB |
Output is correct |
6 |
Correct |
5 ms |
632 KB |
Output is correct |
7 |
Correct |
5 ms |
632 KB |
Output is correct |
8 |
Correct |
5 ms |
632 KB |
Output is correct |
9 |
Correct |
5 ms |
632 KB |
Output is correct |
10 |
Correct |
5 ms |
760 KB |
Output is correct |
11 |
Correct |
5 ms |
760 KB |
Output is correct |
12 |
Correct |
6 ms |
636 KB |
Output is correct |
13 |
Correct |
5 ms |
760 KB |
Output is correct |
14 |
Correct |
5 ms |
632 KB |
Output is correct |
15 |
Correct |
5 ms |
760 KB |
Output is correct |
16 |
Correct |
5 ms |
760 KB |
Output is correct |
17 |
Correct |
5 ms |
760 KB |
Output is correct |
18 |
Correct |
5 ms |
760 KB |
Output is correct |
19 |
Correct |
5 ms |
760 KB |
Output is correct |
20 |
Correct |
5 ms |
792 KB |
Output is correct |
21 |
Correct |
5 ms |
632 KB |
Output is correct |
22 |
Correct |
5 ms |
632 KB |
Output is correct |
23 |
Correct |
5 ms |
632 KB |
Output is correct |
24 |
Correct |
5 ms |
632 KB |
Output is correct |
25 |
Correct |
5 ms |
632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
632 KB |
Output is correct |
2 |
Correct |
5 ms |
632 KB |
Output is correct |
3 |
Correct |
5 ms |
632 KB |
Output is correct |
4 |
Correct |
5 ms |
632 KB |
Output is correct |
5 |
Correct |
5 ms |
632 KB |
Output is correct |
6 |
Correct |
5 ms |
632 KB |
Output is correct |
7 |
Correct |
5 ms |
632 KB |
Output is correct |
8 |
Correct |
5 ms |
632 KB |
Output is correct |
9 |
Correct |
5 ms |
632 KB |
Output is correct |
10 |
Correct |
5 ms |
760 KB |
Output is correct |
11 |
Correct |
5 ms |
760 KB |
Output is correct |
12 |
Correct |
6 ms |
636 KB |
Output is correct |
13 |
Correct |
5 ms |
760 KB |
Output is correct |
14 |
Correct |
5 ms |
632 KB |
Output is correct |
15 |
Correct |
5 ms |
760 KB |
Output is correct |
16 |
Correct |
5 ms |
760 KB |
Output is correct |
17 |
Correct |
5 ms |
760 KB |
Output is correct |
18 |
Correct |
5 ms |
760 KB |
Output is correct |
19 |
Correct |
5 ms |
760 KB |
Output is correct |
20 |
Correct |
5 ms |
792 KB |
Output is correct |
21 |
Correct |
5 ms |
632 KB |
Output is correct |
22 |
Correct |
5 ms |
632 KB |
Output is correct |
23 |
Correct |
5 ms |
632 KB |
Output is correct |
24 |
Correct |
5 ms |
632 KB |
Output is correct |
25 |
Correct |
5 ms |
632 KB |
Output is correct |
26 |
Correct |
7 ms |
1400 KB |
Output is correct |
27 |
Correct |
476 ms |
106232 KB |
Output is correct |
28 |
Correct |
474 ms |
106232 KB |
Output is correct |
29 |
Correct |
566 ms |
110576 KB |
Output is correct |
30 |
Correct |
547 ms |
110460 KB |
Output is correct |
31 |
Correct |
549 ms |
110356 KB |
Output is correct |
32 |
Correct |
557 ms |
110332 KB |
Output is correct |
33 |
Correct |
568 ms |
113400 KB |
Output is correct |
34 |
Correct |
559 ms |
113400 KB |
Output is correct |
35 |
Correct |
529 ms |
113272 KB |
Output is correct |
36 |
Correct |
499 ms |
113272 KB |
Output is correct |
37 |
Correct |
580 ms |
113528 KB |
Output is correct |
38 |
Correct |
590 ms |
117044 KB |
Output is correct |
39 |
Correct |
499 ms |
116932 KB |
Output is correct |
40 |
Correct |
565 ms |
117112 KB |
Output is correct |
41 |
Correct |
567 ms |
116984 KB |
Output is correct |
42 |
Correct |
518 ms |
119436 KB |
Output is correct |
43 |
Correct |
559 ms |
121464 KB |
Output is correct |
44 |
Correct |
570 ms |
121464 KB |
Output is correct |
45 |
Correct |
466 ms |
126456 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
632 KB |
Output is correct |
2 |
Correct |
5 ms |
632 KB |
Output is correct |
3 |
Correct |
5 ms |
632 KB |
Output is correct |
4 |
Correct |
5 ms |
632 KB |
Output is correct |
5 |
Correct |
5 ms |
632 KB |
Output is correct |
6 |
Correct |
5 ms |
632 KB |
Output is correct |
7 |
Correct |
5 ms |
632 KB |
Output is correct |
8 |
Correct |
5 ms |
632 KB |
Output is correct |
9 |
Correct |
5 ms |
632 KB |
Output is correct |
10 |
Correct |
5 ms |
760 KB |
Output is correct |
11 |
Correct |
5 ms |
760 KB |
Output is correct |
12 |
Correct |
6 ms |
636 KB |
Output is correct |
13 |
Correct |
5 ms |
760 KB |
Output is correct |
14 |
Correct |
5 ms |
632 KB |
Output is correct |
15 |
Correct |
5 ms |
760 KB |
Output is correct |
16 |
Correct |
5 ms |
760 KB |
Output is correct |
17 |
Correct |
5 ms |
760 KB |
Output is correct |
18 |
Correct |
5 ms |
760 KB |
Output is correct |
19 |
Correct |
5 ms |
760 KB |
Output is correct |
20 |
Correct |
5 ms |
792 KB |
Output is correct |
21 |
Correct |
5 ms |
632 KB |
Output is correct |
22 |
Correct |
5 ms |
632 KB |
Output is correct |
23 |
Correct |
5 ms |
632 KB |
Output is correct |
24 |
Correct |
5 ms |
632 KB |
Output is correct |
25 |
Correct |
5 ms |
632 KB |
Output is correct |
26 |
Correct |
7 ms |
1400 KB |
Output is correct |
27 |
Correct |
476 ms |
106232 KB |
Output is correct |
28 |
Correct |
474 ms |
106232 KB |
Output is correct |
29 |
Correct |
566 ms |
110576 KB |
Output is correct |
30 |
Correct |
547 ms |
110460 KB |
Output is correct |
31 |
Correct |
549 ms |
110356 KB |
Output is correct |
32 |
Correct |
557 ms |
110332 KB |
Output is correct |
33 |
Correct |
568 ms |
113400 KB |
Output is correct |
34 |
Correct |
559 ms |
113400 KB |
Output is correct |
35 |
Correct |
529 ms |
113272 KB |
Output is correct |
36 |
Correct |
499 ms |
113272 KB |
Output is correct |
37 |
Correct |
580 ms |
113528 KB |
Output is correct |
38 |
Correct |
590 ms |
117044 KB |
Output is correct |
39 |
Correct |
499 ms |
116932 KB |
Output is correct |
40 |
Correct |
565 ms |
117112 KB |
Output is correct |
41 |
Correct |
567 ms |
116984 KB |
Output is correct |
42 |
Correct |
518 ms |
119436 KB |
Output is correct |
43 |
Correct |
559 ms |
121464 KB |
Output is correct |
44 |
Correct |
570 ms |
121464 KB |
Output is correct |
45 |
Correct |
466 ms |
126456 KB |
Output is correct |
46 |
Correct |
185 ms |
22368 KB |
Output is correct |
47 |
Correct |
3040 ms |
274920 KB |
Output is correct |
48 |
Correct |
3018 ms |
338680 KB |
Output is correct |
49 |
Correct |
2988 ms |
396176 KB |
Output is correct |
50 |
Correct |
2983 ms |
396216 KB |
Output is correct |
51 |
Correct |
3023 ms |
396376 KB |
Output is correct |
52 |
Correct |
3416 ms |
396516 KB |
Output is correct |
53 |
Correct |
3416 ms |
396488 KB |
Output is correct |
54 |
Correct |
3461 ms |
396352 KB |
Output is correct |
55 |
Correct |
3508 ms |
396220 KB |
Output is correct |
56 |
Correct |
3469 ms |
429636 KB |
Output is correct |
57 |
Correct |
3487 ms |
465764 KB |
Output is correct |
58 |
Correct |
3466 ms |
504984 KB |
Output is correct |
59 |
Correct |
3420 ms |
547200 KB |
Output is correct |
60 |
Correct |
3369 ms |
592264 KB |
Output is correct |
61 |
Correct |
3300 ms |
640748 KB |
Output is correct |
62 |
Correct |
3167 ms |
691308 KB |
Output is correct |
63 |
Correct |
3047 ms |
745432 KB |
Output is correct |
64 |
Correct |
1659 ms |
823820 KB |
Output is correct |
65 |
Correct |
1620 ms |
823928 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
632 KB |
Output is correct |
2 |
Correct |
5 ms |
632 KB |
Output is correct |
3 |
Correct |
5 ms |
632 KB |
Output is correct |
4 |
Correct |
5 ms |
632 KB |
Output is correct |
5 |
Correct |
5 ms |
632 KB |
Output is correct |
6 |
Correct |
5 ms |
632 KB |
Output is correct |
7 |
Correct |
5 ms |
632 KB |
Output is correct |
8 |
Correct |
5 ms |
632 KB |
Output is correct |
9 |
Correct |
5 ms |
632 KB |
Output is correct |
10 |
Correct |
5 ms |
760 KB |
Output is correct |
11 |
Correct |
5 ms |
760 KB |
Output is correct |
12 |
Correct |
6 ms |
636 KB |
Output is correct |
13 |
Correct |
5 ms |
760 KB |
Output is correct |
14 |
Correct |
5 ms |
632 KB |
Output is correct |
15 |
Correct |
5 ms |
760 KB |
Output is correct |
16 |
Correct |
5 ms |
760 KB |
Output is correct |
17 |
Correct |
5 ms |
760 KB |
Output is correct |
18 |
Correct |
5 ms |
760 KB |
Output is correct |
19 |
Correct |
5 ms |
760 KB |
Output is correct |
20 |
Correct |
5 ms |
792 KB |
Output is correct |
21 |
Correct |
5 ms |
632 KB |
Output is correct |
22 |
Correct |
5 ms |
632 KB |
Output is correct |
23 |
Correct |
5 ms |
632 KB |
Output is correct |
24 |
Correct |
5 ms |
632 KB |
Output is correct |
25 |
Correct |
5 ms |
632 KB |
Output is correct |
26 |
Correct |
7 ms |
1400 KB |
Output is correct |
27 |
Correct |
476 ms |
106232 KB |
Output is correct |
28 |
Correct |
474 ms |
106232 KB |
Output is correct |
29 |
Correct |
566 ms |
110576 KB |
Output is correct |
30 |
Correct |
547 ms |
110460 KB |
Output is correct |
31 |
Correct |
549 ms |
110356 KB |
Output is correct |
32 |
Correct |
557 ms |
110332 KB |
Output is correct |
33 |
Correct |
568 ms |
113400 KB |
Output is correct |
34 |
Correct |
559 ms |
113400 KB |
Output is correct |
35 |
Correct |
529 ms |
113272 KB |
Output is correct |
36 |
Correct |
499 ms |
113272 KB |
Output is correct |
37 |
Correct |
580 ms |
113528 KB |
Output is correct |
38 |
Correct |
590 ms |
117044 KB |
Output is correct |
39 |
Correct |
499 ms |
116932 KB |
Output is correct |
40 |
Correct |
565 ms |
117112 KB |
Output is correct |
41 |
Correct |
567 ms |
116984 KB |
Output is correct |
42 |
Correct |
518 ms |
119436 KB |
Output is correct |
43 |
Correct |
559 ms |
121464 KB |
Output is correct |
44 |
Correct |
570 ms |
121464 KB |
Output is correct |
45 |
Correct |
466 ms |
126456 KB |
Output is correct |
46 |
Correct |
185 ms |
22368 KB |
Output is correct |
47 |
Correct |
3040 ms |
274920 KB |
Output is correct |
48 |
Correct |
3018 ms |
338680 KB |
Output is correct |
49 |
Correct |
2988 ms |
396176 KB |
Output is correct |
50 |
Correct |
2983 ms |
396216 KB |
Output is correct |
51 |
Correct |
3023 ms |
396376 KB |
Output is correct |
52 |
Correct |
3416 ms |
396516 KB |
Output is correct |
53 |
Correct |
3416 ms |
396488 KB |
Output is correct |
54 |
Correct |
3461 ms |
396352 KB |
Output is correct |
55 |
Correct |
3508 ms |
396220 KB |
Output is correct |
56 |
Correct |
3469 ms |
429636 KB |
Output is correct |
57 |
Correct |
3487 ms |
465764 KB |
Output is correct |
58 |
Correct |
3466 ms |
504984 KB |
Output is correct |
59 |
Correct |
3420 ms |
547200 KB |
Output is correct |
60 |
Correct |
3369 ms |
592264 KB |
Output is correct |
61 |
Correct |
3300 ms |
640748 KB |
Output is correct |
62 |
Correct |
3167 ms |
691308 KB |
Output is correct |
63 |
Correct |
3047 ms |
745432 KB |
Output is correct |
64 |
Correct |
1659 ms |
823820 KB |
Output is correct |
65 |
Correct |
1620 ms |
823928 KB |
Output is correct |
66 |
Correct |
524 ms |
102392 KB |
Output is correct |
67 |
Correct |
3452 ms |
210096 KB |
Output is correct |
68 |
Correct |
1091 ms |
722376 KB |
Output is correct |
69 |
Correct |
4270 ms |
723852 KB |
Output is correct |
70 |
Correct |
9205 ms |
825536 KB |
Output is correct |
71 |
Correct |
10232 ms |
277340 KB |
Output is correct |
72 |
Correct |
9964 ms |
340988 KB |
Output is correct |
73 |
Correct |
10924 ms |
399212 KB |
Output is correct |
74 |
Correct |
10943 ms |
398840 KB |
Output is correct |
75 |
Correct |
11086 ms |
399028 KB |
Output is correct |
76 |
Correct |
10247 ms |
398724 KB |
Output is correct |
77 |
Correct |
10121 ms |
398728 KB |
Output is correct |
78 |
Correct |
10897 ms |
398704 KB |
Output is correct |
79 |
Correct |
11204 ms |
468580 KB |
Output is correct |
80 |
Correct |
10953 ms |
507792 KB |
Output is correct |
81 |
Correct |
9629 ms |
549416 KB |
Output is correct |
82 |
Correct |
10944 ms |
595344 KB |
Output is correct |
83 |
Correct |
9710 ms |
642612 KB |
Output is correct |
84 |
Correct |
10489 ms |
748304 KB |
Output is correct |
85 |
Correct |
8342 ms |
826648 KB |
Output is correct |