#include <bits/stdc++.h>
#define eb emplace_back
#define mp make_pair
#define F first
#define S second
using namespace std;
typedef long long LL;
typedef pair<int, int> pii;
typedef pair<int, LL> pil;
const LL llinf=2e18;
int n, m, q, len;
int num[2010][2010], re;
vector<pii> link[2010];
LL dist[4010][4010];
bool ch[4010];
priority_queue<pair<LL, pii>, vector<pair<LL, pii> >, greater<pair<LL, pii> > > pq;
void do_dijk(int s, int e){
memset(ch, 0, sizeof ch);
pq.push(mp(0ll, mp(s, e)));
dist[num[s][e]][num[s][e]]=0;
while(!pq.empty()){
LL d=pq.top().F;
int fr=pq.top().S.F, nw=pq.top().S.S;
pq.pop();
if(ch[num[fr][nw]])continue;
ch[num[fr][nw]]=true;
for(auto j:link[nw]){
if(j.F!=fr&&dist[num[s][e]][num[nw][j.F]]>d+j.S){
dist[num[s][e]][num[nw][j.F]]=d+j.S;
pq.push(mp(d+j.S, mp(nw, j.F)));
}
}
}
}
int arr[100010];
struct NODE{
pair<LL, pii> p[4];
NODE(){for(int i=0; i<4; i++)p[i]=mp(llinf, mp(0, 0));}
}proc[2010][2010], tree[400010];
NODE f(NODE a, NODE b){
NODE ret;
vector<pair<LL, pii> > vc;
for(int i=0; i<4; i++){
for(int j=0; j<4; j++){
if(a.p[i].S.S==b.p[j].S.F)vc.eb(mp(llinf, mp(0, 0)));
else vc.eb(mp(a.p[i].F+b.p[j].F, mp(a.p[i].S.F, b.p[j].S.S)));
}
}
for(auto k:vc)ret.p[0]=min(ret.p[0], k);
for(auto k:vc){
if(k.S.F!=ret.p[0].S.F&&k.S.S!=ret.p[0].S.S)
ret.p[1]=min(ret.p[1], k);
}
for(auto k:vc){
if(k.S.F!=ret.p[1].S.F&&k.S.S!=ret.p[0].S.S)
ret.p[2]=min(ret.p[2], k);
}
for(auto k:vc){
if(k.S.F!=ret.p[0].S.F&&k.S.S!=ret.p[1].S.S)
ret.p[3]=min(ret.p[3], k);
}
return ret;
}
void init(int point, int s, int e){
if(s==e){
tree[point]=proc[arr[s]][arr[s+1]];
return;
}
init(point*2, s, (s+e)/2);
init(point*2+1, (s+e)/2+1, e);
tree[point]=f(tree[point*2], tree[point*2+1]);
}
void update(int point, int s, int e, int num){
if(s==e){
tree[point]=proc[arr[s]][arr[s+1]];
return;
}
if(num<=(s+e)/2)update(point*2, s, (s+e)/2, num);
else update(point*2+1, (s+e)/2+1, e, num);
tree[point]=f(tree[point*2], tree[point*2+1]);
}
int main(){
scanf("%d %d %d %d", &n, &m, &q, &len);
for(int i=1; i<=m; i++){
int a, b, c;
scanf("%d %d %d", &a, &b, &c);
link[a].eb(b, c);
num[a][b]=++re;
link[b].eb(a, c);
num[b][a]=++re;
}
for(int i=1; i<=re; i++){
for(int j=1; j<=re; j++)dist[i][j]=llinf;
}
for(int i=1; i<=n; i++){
for(auto j:link[i])do_dijk(i, j.F);
}
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
if(i==j)continue;
vector<pair<LL, pii> > vc;
for(auto k1:link[i]){
for(auto k2:link[j]){
vc.eb(dist[num[i][k1.F]][num[k2.F][j]]+k1.S, mp(k1.F, k2.F));
}
}
for(auto k:vc)proc[i][j].p[0]=min(proc[i][j].p[0], k);
for(auto k:vc){
if(k.S.F!=proc[i][j].p[0].S.F&&k.S.S!=proc[i][j].p[0].S.S)
proc[i][j].p[1]=min(proc[i][j].p[1], k);
}
for(auto k:vc){
if(k.S.F!=proc[i][j].p[1].S.F&&k.S.S!=proc[i][j].p[0].S.S)
proc[i][j].p[2]=min(proc[i][j].p[2], k);
}
for(auto k:vc){
if(k.S.F!=proc[i][j].p[0].S.F&&k.S.S!=proc[i][j].p[1].S.S)
proc[i][j].p[3]=min(proc[i][j].p[3], k);
}
}
}
for(int i=1; i<=len; i++)scanf("%d", &arr[i]);
init(1, 1, len-1);
while(q--){
int a, b;
scanf("%d %d", &a, &b);
arr[a]=b;
if(a<len)update(1, 1, len-1, a);
if(a>1)update(1, 1, len-1, a-1);
printf("%lld\n", tree[1].p[0].F>=llinf?-1:tree[1].p[0].F);
}
}
Compilation message
wild_boar.cpp: In function 'int main()':
wild_boar.cpp:88: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, &q, &len);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
wild_boar.cpp:91:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d", &a, &b, &c);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
wild_boar.cpp:127:35: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(int i=1; i<=len; i++)scanf("%d", &arr[i]);
~~~~~^~~~~~~~~~~~~~~
wild_boar.cpp:131:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &a, &b);
~~~~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
173 ms |
278520 KB |
Output is correct |
2 |
Correct |
175 ms |
278520 KB |
Output is correct |
3 |
Correct |
163 ms |
278520 KB |
Output is correct |
4 |
Correct |
179 ms |
278568 KB |
Output is correct |
5 |
Correct |
167 ms |
278592 KB |
Output is correct |
6 |
Correct |
160 ms |
278520 KB |
Output is correct |
7 |
Correct |
168 ms |
278572 KB |
Output is correct |
8 |
Correct |
162 ms |
278520 KB |
Output is correct |
9 |
Correct |
159 ms |
278520 KB |
Output is correct |
10 |
Correct |
158 ms |
278520 KB |
Output is correct |
11 |
Correct |
158 ms |
278520 KB |
Output is correct |
12 |
Correct |
156 ms |
278528 KB |
Output is correct |
13 |
Correct |
165 ms |
278520 KB |
Output is correct |
14 |
Correct |
156 ms |
278520 KB |
Output is correct |
15 |
Correct |
175 ms |
278480 KB |
Output is correct |
16 |
Correct |
162 ms |
278648 KB |
Output is correct |
17 |
Correct |
160 ms |
278520 KB |
Output is correct |
18 |
Correct |
165 ms |
278520 KB |
Output is correct |
19 |
Correct |
168 ms |
278520 KB |
Output is correct |
20 |
Correct |
168 ms |
278520 KB |
Output is correct |
21 |
Correct |
164 ms |
278520 KB |
Output is correct |
22 |
Correct |
168 ms |
278520 KB |
Output is correct |
23 |
Correct |
161 ms |
278520 KB |
Output is correct |
24 |
Correct |
174 ms |
278492 KB |
Output is correct |
25 |
Correct |
182 ms |
278520 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
173 ms |
278520 KB |
Output is correct |
2 |
Correct |
175 ms |
278520 KB |
Output is correct |
3 |
Correct |
163 ms |
278520 KB |
Output is correct |
4 |
Correct |
179 ms |
278568 KB |
Output is correct |
5 |
Correct |
167 ms |
278592 KB |
Output is correct |
6 |
Correct |
160 ms |
278520 KB |
Output is correct |
7 |
Correct |
168 ms |
278572 KB |
Output is correct |
8 |
Correct |
162 ms |
278520 KB |
Output is correct |
9 |
Correct |
159 ms |
278520 KB |
Output is correct |
10 |
Correct |
158 ms |
278520 KB |
Output is correct |
11 |
Correct |
158 ms |
278520 KB |
Output is correct |
12 |
Correct |
156 ms |
278528 KB |
Output is correct |
13 |
Correct |
165 ms |
278520 KB |
Output is correct |
14 |
Correct |
156 ms |
278520 KB |
Output is correct |
15 |
Correct |
175 ms |
278480 KB |
Output is correct |
16 |
Correct |
162 ms |
278648 KB |
Output is correct |
17 |
Correct |
160 ms |
278520 KB |
Output is correct |
18 |
Correct |
165 ms |
278520 KB |
Output is correct |
19 |
Correct |
168 ms |
278520 KB |
Output is correct |
20 |
Correct |
168 ms |
278520 KB |
Output is correct |
21 |
Correct |
164 ms |
278520 KB |
Output is correct |
22 |
Correct |
168 ms |
278520 KB |
Output is correct |
23 |
Correct |
161 ms |
278520 KB |
Output is correct |
24 |
Correct |
174 ms |
278492 KB |
Output is correct |
25 |
Correct |
182 ms |
278520 KB |
Output is correct |
26 |
Correct |
162 ms |
278904 KB |
Output is correct |
27 |
Correct |
251 ms |
280696 KB |
Output is correct |
28 |
Correct |
230 ms |
280696 KB |
Output is correct |
29 |
Correct |
324 ms |
284792 KB |
Output is correct |
30 |
Correct |
309 ms |
284796 KB |
Output is correct |
31 |
Correct |
327 ms |
284880 KB |
Output is correct |
32 |
Correct |
317 ms |
284868 KB |
Output is correct |
33 |
Correct |
311 ms |
285048 KB |
Output is correct |
34 |
Correct |
318 ms |
285180 KB |
Output is correct |
35 |
Correct |
309 ms |
285080 KB |
Output is correct |
36 |
Correct |
301 ms |
285048 KB |
Output is correct |
37 |
Correct |
315 ms |
285176 KB |
Output is correct |
38 |
Correct |
304 ms |
285304 KB |
Output is correct |
39 |
Correct |
307 ms |
285304 KB |
Output is correct |
40 |
Correct |
322 ms |
285304 KB |
Output is correct |
41 |
Correct |
311 ms |
285432 KB |
Output is correct |
42 |
Correct |
301 ms |
285436 KB |
Output is correct |
43 |
Correct |
321 ms |
285560 KB |
Output is correct |
44 |
Correct |
314 ms |
285560 KB |
Output is correct |
45 |
Correct |
266 ms |
285692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
173 ms |
278520 KB |
Output is correct |
2 |
Correct |
175 ms |
278520 KB |
Output is correct |
3 |
Correct |
163 ms |
278520 KB |
Output is correct |
4 |
Correct |
179 ms |
278568 KB |
Output is correct |
5 |
Correct |
167 ms |
278592 KB |
Output is correct |
6 |
Correct |
160 ms |
278520 KB |
Output is correct |
7 |
Correct |
168 ms |
278572 KB |
Output is correct |
8 |
Correct |
162 ms |
278520 KB |
Output is correct |
9 |
Correct |
159 ms |
278520 KB |
Output is correct |
10 |
Correct |
158 ms |
278520 KB |
Output is correct |
11 |
Correct |
158 ms |
278520 KB |
Output is correct |
12 |
Correct |
156 ms |
278528 KB |
Output is correct |
13 |
Correct |
165 ms |
278520 KB |
Output is correct |
14 |
Correct |
156 ms |
278520 KB |
Output is correct |
15 |
Correct |
175 ms |
278480 KB |
Output is correct |
16 |
Correct |
162 ms |
278648 KB |
Output is correct |
17 |
Correct |
160 ms |
278520 KB |
Output is correct |
18 |
Correct |
165 ms |
278520 KB |
Output is correct |
19 |
Correct |
168 ms |
278520 KB |
Output is correct |
20 |
Correct |
168 ms |
278520 KB |
Output is correct |
21 |
Correct |
164 ms |
278520 KB |
Output is correct |
22 |
Correct |
168 ms |
278520 KB |
Output is correct |
23 |
Correct |
161 ms |
278520 KB |
Output is correct |
24 |
Correct |
174 ms |
278492 KB |
Output is correct |
25 |
Correct |
182 ms |
278520 KB |
Output is correct |
26 |
Correct |
162 ms |
278904 KB |
Output is correct |
27 |
Correct |
251 ms |
280696 KB |
Output is correct |
28 |
Correct |
230 ms |
280696 KB |
Output is correct |
29 |
Correct |
324 ms |
284792 KB |
Output is correct |
30 |
Correct |
309 ms |
284796 KB |
Output is correct |
31 |
Correct |
327 ms |
284880 KB |
Output is correct |
32 |
Correct |
317 ms |
284868 KB |
Output is correct |
33 |
Correct |
311 ms |
285048 KB |
Output is correct |
34 |
Correct |
318 ms |
285180 KB |
Output is correct |
35 |
Correct |
309 ms |
285080 KB |
Output is correct |
36 |
Correct |
301 ms |
285048 KB |
Output is correct |
37 |
Correct |
315 ms |
285176 KB |
Output is correct |
38 |
Correct |
304 ms |
285304 KB |
Output is correct |
39 |
Correct |
307 ms |
285304 KB |
Output is correct |
40 |
Correct |
322 ms |
285304 KB |
Output is correct |
41 |
Correct |
311 ms |
285432 KB |
Output is correct |
42 |
Correct |
301 ms |
285436 KB |
Output is correct |
43 |
Correct |
321 ms |
285560 KB |
Output is correct |
44 |
Correct |
314 ms |
285560 KB |
Output is correct |
45 |
Correct |
266 ms |
285692 KB |
Output is correct |
46 |
Correct |
397 ms |
291328 KB |
Output is correct |
47 |
Correct |
6733 ms |
410508 KB |
Output is correct |
48 |
Correct |
6134 ms |
412324 KB |
Output is correct |
49 |
Correct |
5367 ms |
412332 KB |
Output is correct |
50 |
Correct |
5654 ms |
413688 KB |
Output is correct |
51 |
Correct |
6345 ms |
414308 KB |
Output is correct |
52 |
Correct |
4456 ms |
411260 KB |
Output is correct |
53 |
Correct |
4478 ms |
411304 KB |
Output is correct |
54 |
Correct |
4557 ms |
411348 KB |
Output is correct |
55 |
Correct |
4458 ms |
411252 KB |
Output is correct |
56 |
Correct |
4548 ms |
411940 KB |
Output is correct |
57 |
Correct |
4602 ms |
412728 KB |
Output is correct |
58 |
Correct |
4525 ms |
413056 KB |
Output is correct |
59 |
Correct |
4598 ms |
413704 KB |
Output is correct |
60 |
Correct |
4470 ms |
414328 KB |
Output is correct |
61 |
Correct |
4455 ms |
414664 KB |
Output is correct |
62 |
Correct |
4172 ms |
415196 KB |
Output is correct |
63 |
Correct |
4035 ms |
415604 KB |
Output is correct |
64 |
Correct |
1941 ms |
416248 KB |
Output is correct |
65 |
Correct |
1900 ms |
416504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
173 ms |
278520 KB |
Output is correct |
2 |
Correct |
175 ms |
278520 KB |
Output is correct |
3 |
Correct |
163 ms |
278520 KB |
Output is correct |
4 |
Correct |
179 ms |
278568 KB |
Output is correct |
5 |
Correct |
167 ms |
278592 KB |
Output is correct |
6 |
Correct |
160 ms |
278520 KB |
Output is correct |
7 |
Correct |
168 ms |
278572 KB |
Output is correct |
8 |
Correct |
162 ms |
278520 KB |
Output is correct |
9 |
Correct |
159 ms |
278520 KB |
Output is correct |
10 |
Correct |
158 ms |
278520 KB |
Output is correct |
11 |
Correct |
158 ms |
278520 KB |
Output is correct |
12 |
Correct |
156 ms |
278528 KB |
Output is correct |
13 |
Correct |
165 ms |
278520 KB |
Output is correct |
14 |
Correct |
156 ms |
278520 KB |
Output is correct |
15 |
Correct |
175 ms |
278480 KB |
Output is correct |
16 |
Correct |
162 ms |
278648 KB |
Output is correct |
17 |
Correct |
160 ms |
278520 KB |
Output is correct |
18 |
Correct |
165 ms |
278520 KB |
Output is correct |
19 |
Correct |
168 ms |
278520 KB |
Output is correct |
20 |
Correct |
168 ms |
278520 KB |
Output is correct |
21 |
Correct |
164 ms |
278520 KB |
Output is correct |
22 |
Correct |
168 ms |
278520 KB |
Output is correct |
23 |
Correct |
161 ms |
278520 KB |
Output is correct |
24 |
Correct |
174 ms |
278492 KB |
Output is correct |
25 |
Correct |
182 ms |
278520 KB |
Output is correct |
26 |
Correct |
162 ms |
278904 KB |
Output is correct |
27 |
Correct |
251 ms |
280696 KB |
Output is correct |
28 |
Correct |
230 ms |
280696 KB |
Output is correct |
29 |
Correct |
324 ms |
284792 KB |
Output is correct |
30 |
Correct |
309 ms |
284796 KB |
Output is correct |
31 |
Correct |
327 ms |
284880 KB |
Output is correct |
32 |
Correct |
317 ms |
284868 KB |
Output is correct |
33 |
Correct |
311 ms |
285048 KB |
Output is correct |
34 |
Correct |
318 ms |
285180 KB |
Output is correct |
35 |
Correct |
309 ms |
285080 KB |
Output is correct |
36 |
Correct |
301 ms |
285048 KB |
Output is correct |
37 |
Correct |
315 ms |
285176 KB |
Output is correct |
38 |
Correct |
304 ms |
285304 KB |
Output is correct |
39 |
Correct |
307 ms |
285304 KB |
Output is correct |
40 |
Correct |
322 ms |
285304 KB |
Output is correct |
41 |
Correct |
311 ms |
285432 KB |
Output is correct |
42 |
Correct |
301 ms |
285436 KB |
Output is correct |
43 |
Correct |
321 ms |
285560 KB |
Output is correct |
44 |
Correct |
314 ms |
285560 KB |
Output is correct |
45 |
Correct |
266 ms |
285692 KB |
Output is correct |
46 |
Correct |
397 ms |
291328 KB |
Output is correct |
47 |
Correct |
6733 ms |
410508 KB |
Output is correct |
48 |
Correct |
6134 ms |
412324 KB |
Output is correct |
49 |
Correct |
5367 ms |
412332 KB |
Output is correct |
50 |
Correct |
5654 ms |
413688 KB |
Output is correct |
51 |
Correct |
6345 ms |
414308 KB |
Output is correct |
52 |
Correct |
4456 ms |
411260 KB |
Output is correct |
53 |
Correct |
4478 ms |
411304 KB |
Output is correct |
54 |
Correct |
4557 ms |
411348 KB |
Output is correct |
55 |
Correct |
4458 ms |
411252 KB |
Output is correct |
56 |
Correct |
4548 ms |
411940 KB |
Output is correct |
57 |
Correct |
4602 ms |
412728 KB |
Output is correct |
58 |
Correct |
4525 ms |
413056 KB |
Output is correct |
59 |
Correct |
4598 ms |
413704 KB |
Output is correct |
60 |
Correct |
4470 ms |
414328 KB |
Output is correct |
61 |
Correct |
4455 ms |
414664 KB |
Output is correct |
62 |
Correct |
4172 ms |
415196 KB |
Output is correct |
63 |
Correct |
4035 ms |
415604 KB |
Output is correct |
64 |
Correct |
1941 ms |
416248 KB |
Output is correct |
65 |
Correct |
1900 ms |
416504 KB |
Output is correct |
66 |
Correct |
243 ms |
279544 KB |
Output is correct |
67 |
Correct |
1311 ms |
323528 KB |
Output is correct |
68 |
Correct |
1810 ms |
414908 KB |
Output is correct |
69 |
Correct |
2584 ms |
415508 KB |
Output is correct |
70 |
Correct |
3856 ms |
416012 KB |
Output is correct |
71 |
Correct |
8972 ms |
410512 KB |
Output is correct |
72 |
Correct |
8409 ms |
412496 KB |
Output is correct |
73 |
Correct |
6838 ms |
412812 KB |
Output is correct |
74 |
Correct |
6871 ms |
412716 KB |
Output is correct |
75 |
Correct |
6573 ms |
412736 KB |
Output is correct |
76 |
Correct |
7708 ms |
412612 KB |
Output is correct |
77 |
Correct |
8111 ms |
414260 KB |
Output is correct |
78 |
Correct |
9252 ms |
418508 KB |
Output is correct |
79 |
Correct |
6636 ms |
413812 KB |
Output is correct |
80 |
Correct |
6704 ms |
414712 KB |
Output is correct |
81 |
Correct |
7113 ms |
415400 KB |
Output is correct |
82 |
Correct |
6657 ms |
415612 KB |
Output is correct |
83 |
Correct |
6697 ms |
416032 KB |
Output is correct |
84 |
Correct |
5981 ms |
417456 KB |
Output is correct |
85 |
Correct |
4250 ms |
417892 KB |
Output is correct |