#include <bits/stdc++.h>
using namespace std;
using ll = long long int;
using pl = tuple<ll, ll>;
using tl = tuple<ll, ll, ll>;
const ll INFLL = 1e18;
ll N, M, T, L;
vector<tl> G[2010]; // (to, cost, id)
vector<tl> edges; // (from, to, cost)
vector<ll> eds[4010];
ll x[200010];
ll p[200010];
ll q[200010];
vector<tl> sp[2010][2010]; // (cost, next_start, prev_end)
vector<tl> node[400010]; // (cost, next_start, prev_end)
ll siz = 1;
vector<tl> calc_top_five(vector<tl> ps){
vector<tl> res(5, make_tuple(INFLL, INFLL, INFLL));
int n = ps.size();
for(int i = 0; i < n; i++){
if(get<0>(ps[i]) < get<0>(res[0])){
res[0] = ps[i];
}
}
for(int i = 0; i < n; i++){
if(get<1>(ps[i]) != get<1>(res[0]) && get<0>(ps[i])< get<0>(res[1])){
res[1] = ps[i];
}
if(get<2>(ps[i]) != get<2>(res[0]) && get<0>(ps[i])< get<0>(res[3])){
res[3] = ps[i];
}
}
for(int i = 0; i < n; i++){
if(get<1>(ps[i]) != get<1>(res[0]) && get<2>(ps[i]) != get<2>(res[1]) && get<0>(ps[i]) < get<0>(res[2])){
res[2] = ps[i];
}
if(get<2>(ps[i]) != get<2>(res[0]) && get<1>(ps[i]) != get<1>(res[3]) && get<0>(ps[i]) < get<0>(res[4])){
res[4] = ps[i];
}
}
return res;
}
vector<tl> merge(vector<tl> s, vector<tl> t){
vector<tl> ps(0);
for(int i = 0; i < (int)s.size(); i++){
for(int j = 0; j < (int)t.size(); j++){
if(get<2>(s[i]) != get<1>(t[j])){
ps.emplace_back(get<0>(s[i]) + get<0>(t[j]), get<1>(s[i]), get<2>(t[j]));
}
}
}
return calc_top_five(ps);
}
void init_segment_tree(ll k, ll l, ll r){
if(r - l == 1){
if(l < L-1){
node[k] = sp[x[l]][x[l+1]];
} else {
node[k] = vector<tl>(5, make_tuple(INFLL, INFLL, INFLL));
}
} else {
init_segment_tree(k*2+1, l, (l+r)/2);
init_segment_tree(k*2+2, (l+r)/2, r);
node[k] = merge(node[k*2+1], node[k*2+2]);
}
}
void init_segment_tree(){
siz = 1;
while(siz < L-1){
siz *= 2;
}
init_segment_tree(0, 0, siz);
}
void update(ll p, ll k, ll l, ll r){
if(r - l == 1){
assert(l == p && l < L-1);
node[k] = sp[x[l]][x[l+1]];
} else {
ll m = (l + r) / 2;
if(p < m){
update(p, k*2+1, l, m);
} else {
update(p, k*2+2, m, r);
}
node[k] = merge(node[k*2+1], node[k*2+2]);
}
}
void update(ll p){
update(p, 0, 0, siz);
}
vector<tl> query(ll k, ll l, ll r){
if(r <= L-1){
return node[k];
} else if(L-1 <= l){
assert(false);
} else {
ll m = (l+r)/2;
vector<tl> resa = query(k*2+1, l, m);
if(L-1 <= m){
return resa;
}
vector<tl> resb = query(k*2+2, m, r);
return merge(resa, resb);
}
}
ll answer(){
return get<0>(query(0, 0, siz)[0]);
}
vector<ll> dijkstra(ll s){
vector<ll> dist(2*M, INFLL);
dist[s] = get<2>(edges[s]);
vector<ll> state(N, -1);
priority_queue<pl, vector<pl>, greater<pl> > que;
que.emplace(get<2>(edges[s]), s);
while(!que.empty()){
ll d, p;
tie(d, p) = que.top();
que.pop();
if(d > dist[p]){
continue;
}
ll cur = get<1>(edges[p]);
if(state[cur] == -1){
for(auto &w: G[cur]){
ll to, co, id;
tie(to, co, id) = w;
if(id/2 == p/2){
continue;
}
if(d + co < dist[id]){
dist[id] = d + co;
que.emplace(dist[id], id);
}
}
state[cur] = p;
} else if(p != state[cur]){
ll id = (state[cur]/2*2) + (1 - state[cur]%2);
ll co = get<2>(edges[id]);
if(d + co < dist[id]){
dist[id] = d + co;
que.emplace(dist[id], id);
}
}
}
return dist;
}
void input(){
scanf("%lld %lld %lld %lld", &N, &M, &T, &L);
for(ll i = 0; i < M; i++){
ll a, b, c;
scanf("%lld %lld %lld", &a, &b, &c);
a--;
b--;
G[a].emplace_back(b, c, 2*i);
G[b].emplace_back(a, c, 2*i+1);
edges.emplace_back(a, b, c);
edges.emplace_back(b, a, c);
}
for(ll i = 0; i < L; i++){
scanf("%lld", &x[i]);
x[i]--;
}
for(ll i = 0; i < T; i++){
scanf("%lld %lld", &p[i], &q[i]);
p[i]--;
q[i]--;
}
}
int main(){
input();
for(int i = 0; i < 2*M; i++){
eds[i] = dijkstra(i);
}
for(int i = 0; i < 2*M; i++){
for(int j = 0; j < 2*M; j++){
ll s, t, a, b, _;
tie(s, a, _) = edges[i];
tie(b, t, _) = edges[j];
sp[s][t].emplace_back(eds[i][j], a, b);
}
}
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
sp[i][j] = calc_top_five(sp[i][j]);
}
}
init_segment_tree();
for(ll i = 0; i < T; i++){
x[p[i]] = q[i];
if(p[i] < L-1){
update(p[i]);
}
if(p[i] > 0){
update(p[i]-1);
}
ll ans = answer();
if(ans == INFLL){
ans = -1;
}
printf("%lld\n", ans);
}
return 0;
}
Compilation message
wild_boar.cpp: In function 'void input()':
wild_boar.cpp:167:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
167 | scanf("%lld %lld %lld %lld", &N, &M, &T, &L);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
wild_boar.cpp:170:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
170 | scanf("%lld %lld %lld", &a, &b, &c);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
wild_boar.cpp:179:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
179 | scanf("%lld", &x[i]);
| ~~~~~^~~~~~~~~~~~~~~
wild_boar.cpp:183:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
183 | scanf("%lld %lld", &p[i], &q[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
104680 KB |
Output is correct |
2 |
Correct |
54 ms |
104720 KB |
Output is correct |
3 |
Correct |
60 ms |
104928 KB |
Output is correct |
4 |
Correct |
55 ms |
104672 KB |
Output is correct |
5 |
Correct |
57 ms |
104652 KB |
Output is correct |
6 |
Correct |
54 ms |
104716 KB |
Output is correct |
7 |
Correct |
55 ms |
104704 KB |
Output is correct |
8 |
Correct |
53 ms |
104652 KB |
Output is correct |
9 |
Correct |
57 ms |
104752 KB |
Output is correct |
10 |
Correct |
53 ms |
104668 KB |
Output is correct |
11 |
Correct |
58 ms |
104744 KB |
Output is correct |
12 |
Correct |
58 ms |
104704 KB |
Output is correct |
13 |
Correct |
56 ms |
104652 KB |
Output is correct |
14 |
Correct |
54 ms |
104704 KB |
Output is correct |
15 |
Correct |
54 ms |
104652 KB |
Output is correct |
16 |
Correct |
56 ms |
104660 KB |
Output is correct |
17 |
Correct |
57 ms |
104656 KB |
Output is correct |
18 |
Correct |
54 ms |
104704 KB |
Output is correct |
19 |
Correct |
53 ms |
104668 KB |
Output is correct |
20 |
Correct |
57 ms |
104676 KB |
Output is correct |
21 |
Correct |
57 ms |
104624 KB |
Output is correct |
22 |
Correct |
62 ms |
104736 KB |
Output is correct |
23 |
Correct |
54 ms |
104660 KB |
Output is correct |
24 |
Correct |
56 ms |
104808 KB |
Output is correct |
25 |
Correct |
54 ms |
104676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
104680 KB |
Output is correct |
2 |
Correct |
54 ms |
104720 KB |
Output is correct |
3 |
Correct |
60 ms |
104928 KB |
Output is correct |
4 |
Correct |
55 ms |
104672 KB |
Output is correct |
5 |
Correct |
57 ms |
104652 KB |
Output is correct |
6 |
Correct |
54 ms |
104716 KB |
Output is correct |
7 |
Correct |
55 ms |
104704 KB |
Output is correct |
8 |
Correct |
53 ms |
104652 KB |
Output is correct |
9 |
Correct |
57 ms |
104752 KB |
Output is correct |
10 |
Correct |
53 ms |
104668 KB |
Output is correct |
11 |
Correct |
58 ms |
104744 KB |
Output is correct |
12 |
Correct |
58 ms |
104704 KB |
Output is correct |
13 |
Correct |
56 ms |
104652 KB |
Output is correct |
14 |
Correct |
54 ms |
104704 KB |
Output is correct |
15 |
Correct |
54 ms |
104652 KB |
Output is correct |
16 |
Correct |
56 ms |
104660 KB |
Output is correct |
17 |
Correct |
57 ms |
104656 KB |
Output is correct |
18 |
Correct |
54 ms |
104704 KB |
Output is correct |
19 |
Correct |
53 ms |
104668 KB |
Output is correct |
20 |
Correct |
57 ms |
104676 KB |
Output is correct |
21 |
Correct |
57 ms |
104624 KB |
Output is correct |
22 |
Correct |
62 ms |
104736 KB |
Output is correct |
23 |
Correct |
54 ms |
104660 KB |
Output is correct |
24 |
Correct |
56 ms |
104808 KB |
Output is correct |
25 |
Correct |
54 ms |
104676 KB |
Output is correct |
26 |
Correct |
58 ms |
105020 KB |
Output is correct |
27 |
Correct |
163 ms |
140312 KB |
Output is correct |
28 |
Correct |
159 ms |
140336 KB |
Output is correct |
29 |
Correct |
251 ms |
142836 KB |
Output is correct |
30 |
Correct |
233 ms |
142820 KB |
Output is correct |
31 |
Correct |
230 ms |
142744 KB |
Output is correct |
32 |
Correct |
224 ms |
142764 KB |
Output is correct |
33 |
Correct |
243 ms |
144704 KB |
Output is correct |
34 |
Correct |
253 ms |
144700 KB |
Output is correct |
35 |
Correct |
215 ms |
144672 KB |
Output is correct |
36 |
Correct |
239 ms |
144612 KB |
Output is correct |
37 |
Correct |
253 ms |
144608 KB |
Output is correct |
38 |
Correct |
263 ms |
147388 KB |
Output is correct |
39 |
Correct |
234 ms |
147184 KB |
Output is correct |
40 |
Correct |
293 ms |
147328 KB |
Output is correct |
41 |
Correct |
246 ms |
147268 KB |
Output is correct |
42 |
Correct |
235 ms |
148956 KB |
Output is correct |
43 |
Correct |
253 ms |
150384 KB |
Output is correct |
44 |
Correct |
265 ms |
150476 KB |
Output is correct |
45 |
Correct |
222 ms |
153224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
104680 KB |
Output is correct |
2 |
Correct |
54 ms |
104720 KB |
Output is correct |
3 |
Correct |
60 ms |
104928 KB |
Output is correct |
4 |
Correct |
55 ms |
104672 KB |
Output is correct |
5 |
Correct |
57 ms |
104652 KB |
Output is correct |
6 |
Correct |
54 ms |
104716 KB |
Output is correct |
7 |
Correct |
55 ms |
104704 KB |
Output is correct |
8 |
Correct |
53 ms |
104652 KB |
Output is correct |
9 |
Correct |
57 ms |
104752 KB |
Output is correct |
10 |
Correct |
53 ms |
104668 KB |
Output is correct |
11 |
Correct |
58 ms |
104744 KB |
Output is correct |
12 |
Correct |
58 ms |
104704 KB |
Output is correct |
13 |
Correct |
56 ms |
104652 KB |
Output is correct |
14 |
Correct |
54 ms |
104704 KB |
Output is correct |
15 |
Correct |
54 ms |
104652 KB |
Output is correct |
16 |
Correct |
56 ms |
104660 KB |
Output is correct |
17 |
Correct |
57 ms |
104656 KB |
Output is correct |
18 |
Correct |
54 ms |
104704 KB |
Output is correct |
19 |
Correct |
53 ms |
104668 KB |
Output is correct |
20 |
Correct |
57 ms |
104676 KB |
Output is correct |
21 |
Correct |
57 ms |
104624 KB |
Output is correct |
22 |
Correct |
62 ms |
104736 KB |
Output is correct |
23 |
Correct |
54 ms |
104660 KB |
Output is correct |
24 |
Correct |
56 ms |
104808 KB |
Output is correct |
25 |
Correct |
54 ms |
104676 KB |
Output is correct |
26 |
Correct |
58 ms |
105020 KB |
Output is correct |
27 |
Correct |
163 ms |
140312 KB |
Output is correct |
28 |
Correct |
159 ms |
140336 KB |
Output is correct |
29 |
Correct |
251 ms |
142836 KB |
Output is correct |
30 |
Correct |
233 ms |
142820 KB |
Output is correct |
31 |
Correct |
230 ms |
142744 KB |
Output is correct |
32 |
Correct |
224 ms |
142764 KB |
Output is correct |
33 |
Correct |
243 ms |
144704 KB |
Output is correct |
34 |
Correct |
253 ms |
144700 KB |
Output is correct |
35 |
Correct |
215 ms |
144672 KB |
Output is correct |
36 |
Correct |
239 ms |
144612 KB |
Output is correct |
37 |
Correct |
253 ms |
144608 KB |
Output is correct |
38 |
Correct |
263 ms |
147388 KB |
Output is correct |
39 |
Correct |
234 ms |
147184 KB |
Output is correct |
40 |
Correct |
293 ms |
147328 KB |
Output is correct |
41 |
Correct |
246 ms |
147268 KB |
Output is correct |
42 |
Correct |
235 ms |
148956 KB |
Output is correct |
43 |
Correct |
253 ms |
150384 KB |
Output is correct |
44 |
Correct |
265 ms |
150476 KB |
Output is correct |
45 |
Correct |
222 ms |
153224 KB |
Output is correct |
46 |
Correct |
244 ms |
146500 KB |
Output is correct |
47 |
Correct |
3061 ms |
720320 KB |
Output is correct |
48 |
Correct |
3088 ms |
725984 KB |
Output is correct |
49 |
Correct |
3422 ms |
743948 KB |
Output is correct |
50 |
Correct |
3363 ms |
728248 KB |
Output is correct |
51 |
Correct |
3353 ms |
729896 KB |
Output is correct |
52 |
Correct |
3790 ms |
773052 KB |
Output is correct |
53 |
Correct |
3693 ms |
777396 KB |
Output is correct |
54 |
Correct |
3549 ms |
770472 KB |
Output is correct |
55 |
Correct |
3564 ms |
775468 KB |
Output is correct |
56 |
Correct |
3661 ms |
776736 KB |
Output is correct |
57 |
Correct |
3751 ms |
773720 KB |
Output is correct |
58 |
Correct |
3818 ms |
771704 KB |
Output is correct |
59 |
Correct |
3867 ms |
777328 KB |
Output is correct |
60 |
Correct |
3934 ms |
770284 KB |
Output is correct |
61 |
Correct |
4010 ms |
766488 KB |
Output is correct |
62 |
Correct |
4030 ms |
753536 KB |
Output is correct |
63 |
Correct |
4059 ms |
738836 KB |
Output is correct |
64 |
Correct |
2620 ms |
820944 KB |
Output is correct |
65 |
Correct |
2641 ms |
821120 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
104680 KB |
Output is correct |
2 |
Correct |
54 ms |
104720 KB |
Output is correct |
3 |
Correct |
60 ms |
104928 KB |
Output is correct |
4 |
Correct |
55 ms |
104672 KB |
Output is correct |
5 |
Correct |
57 ms |
104652 KB |
Output is correct |
6 |
Correct |
54 ms |
104716 KB |
Output is correct |
7 |
Correct |
55 ms |
104704 KB |
Output is correct |
8 |
Correct |
53 ms |
104652 KB |
Output is correct |
9 |
Correct |
57 ms |
104752 KB |
Output is correct |
10 |
Correct |
53 ms |
104668 KB |
Output is correct |
11 |
Correct |
58 ms |
104744 KB |
Output is correct |
12 |
Correct |
58 ms |
104704 KB |
Output is correct |
13 |
Correct |
56 ms |
104652 KB |
Output is correct |
14 |
Correct |
54 ms |
104704 KB |
Output is correct |
15 |
Correct |
54 ms |
104652 KB |
Output is correct |
16 |
Correct |
56 ms |
104660 KB |
Output is correct |
17 |
Correct |
57 ms |
104656 KB |
Output is correct |
18 |
Correct |
54 ms |
104704 KB |
Output is correct |
19 |
Correct |
53 ms |
104668 KB |
Output is correct |
20 |
Correct |
57 ms |
104676 KB |
Output is correct |
21 |
Correct |
57 ms |
104624 KB |
Output is correct |
22 |
Correct |
62 ms |
104736 KB |
Output is correct |
23 |
Correct |
54 ms |
104660 KB |
Output is correct |
24 |
Correct |
56 ms |
104808 KB |
Output is correct |
25 |
Correct |
54 ms |
104676 KB |
Output is correct |
26 |
Correct |
58 ms |
105020 KB |
Output is correct |
27 |
Correct |
163 ms |
140312 KB |
Output is correct |
28 |
Correct |
159 ms |
140336 KB |
Output is correct |
29 |
Correct |
251 ms |
142836 KB |
Output is correct |
30 |
Correct |
233 ms |
142820 KB |
Output is correct |
31 |
Correct |
230 ms |
142744 KB |
Output is correct |
32 |
Correct |
224 ms |
142764 KB |
Output is correct |
33 |
Correct |
243 ms |
144704 KB |
Output is correct |
34 |
Correct |
253 ms |
144700 KB |
Output is correct |
35 |
Correct |
215 ms |
144672 KB |
Output is correct |
36 |
Correct |
239 ms |
144612 KB |
Output is correct |
37 |
Correct |
253 ms |
144608 KB |
Output is correct |
38 |
Correct |
263 ms |
147388 KB |
Output is correct |
39 |
Correct |
234 ms |
147184 KB |
Output is correct |
40 |
Correct |
293 ms |
147328 KB |
Output is correct |
41 |
Correct |
246 ms |
147268 KB |
Output is correct |
42 |
Correct |
235 ms |
148956 KB |
Output is correct |
43 |
Correct |
253 ms |
150384 KB |
Output is correct |
44 |
Correct |
265 ms |
150476 KB |
Output is correct |
45 |
Correct |
222 ms |
153224 KB |
Output is correct |
46 |
Correct |
244 ms |
146500 KB |
Output is correct |
47 |
Correct |
3061 ms |
720320 KB |
Output is correct |
48 |
Correct |
3088 ms |
725984 KB |
Output is correct |
49 |
Correct |
3422 ms |
743948 KB |
Output is correct |
50 |
Correct |
3363 ms |
728248 KB |
Output is correct |
51 |
Correct |
3353 ms |
729896 KB |
Output is correct |
52 |
Correct |
3790 ms |
773052 KB |
Output is correct |
53 |
Correct |
3693 ms |
777396 KB |
Output is correct |
54 |
Correct |
3549 ms |
770472 KB |
Output is correct |
55 |
Correct |
3564 ms |
775468 KB |
Output is correct |
56 |
Correct |
3661 ms |
776736 KB |
Output is correct |
57 |
Correct |
3751 ms |
773720 KB |
Output is correct |
58 |
Correct |
3818 ms |
771704 KB |
Output is correct |
59 |
Correct |
3867 ms |
777328 KB |
Output is correct |
60 |
Correct |
3934 ms |
770284 KB |
Output is correct |
61 |
Correct |
4010 ms |
766488 KB |
Output is correct |
62 |
Correct |
4030 ms |
753536 KB |
Output is correct |
63 |
Correct |
4059 ms |
738836 KB |
Output is correct |
64 |
Correct |
2620 ms |
820944 KB |
Output is correct |
65 |
Correct |
2641 ms |
821120 KB |
Output is correct |
66 |
Correct |
182 ms |
138700 KB |
Output is correct |
67 |
Correct |
1082 ms |
275608 KB |
Output is correct |
68 |
Correct |
2587 ms |
784032 KB |
Output is correct |
69 |
Correct |
3213 ms |
791036 KB |
Output is correct |
70 |
Correct |
3859 ms |
816168 KB |
Output is correct |
71 |
Correct |
5407 ms |
715048 KB |
Output is correct |
72 |
Correct |
5721 ms |
730704 KB |
Output is correct |
73 |
Correct |
6133 ms |
774052 KB |
Output is correct |
74 |
Correct |
6207 ms |
778608 KB |
Output is correct |
75 |
Correct |
6176 ms |
775328 KB |
Output is correct |
76 |
Correct |
5912 ms |
744592 KB |
Output is correct |
77 |
Correct |
5748 ms |
733472 KB |
Output is correct |
78 |
Correct |
5608 ms |
734384 KB |
Output is correct |
79 |
Correct |
6342 ms |
781268 KB |
Output is correct |
80 |
Correct |
6324 ms |
779340 KB |
Output is correct |
81 |
Correct |
6125 ms |
759836 KB |
Output is correct |
82 |
Correct |
6408 ms |
779192 KB |
Output is correct |
83 |
Correct |
6304 ms |
766424 KB |
Output is correct |
84 |
Correct |
6475 ms |
745388 KB |
Output is correct |
85 |
Correct |
5172 ms |
823868 KB |
Output is correct |