#include <bits/stdc++.h>
#define f first
#define s second
#define m_p make_pair
#define vec vector
#define pb push_back
#define all(x) (x).begin(),(x).end()
#define rall(x) (x).rbegin(),(x).rend()
#define pw(x) (1LL<<(x))
#define sz(x) (int)(x).size()
#define fast_resp ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define ar array
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef long double ld;
template<class T> bool umin(T &a,const T &b){return (a>b?a=b,1:0);};
template<class T> bool umax(T &a,const T &b){return (a<b?a=b,1:0);};
const ll inf=1e18;
const int N=1e5+1;
ar<ll,3> emp={inf,inf,inf};
struct T{
ar<ll,3> paths[5];
///cost,start,end
void init(const vec<ar<ll,3>> &goes){
fill(paths,paths+5,emp);
for(auto &z : goes) umin(paths[0],z);
for(auto &z : goes){
if(z[1]!=paths[0][1])
umin(paths[1],z);
if(z[2]!=paths[0][2])
umin(paths[2],z);
}
for(auto &z : goes){
if(z[1]!=paths[0][1] && z[2]!=paths[1][2])
umin(paths[3],z);
if(z[2]!=paths[0][2] && z[1]!=paths[2][1])
umin(paths[4],z);
}
}
};
vec<ar<ll,3>> goes;
T dst[2001][2001];
vec<ar<ll,3>> jo[2001][2001];
ll dt[4001][4001];
T t[4*N];
int a[N];
vec<ar<ll,3>> g[2001];
void pull(int v){
goes.clear();
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
if(t[v<<1].paths[i][2]!=t[v<<1|1].paths[j][1] && t[v<<1].paths[i][0]!=inf && t[v<<1|1].paths[j][0]!=inf)
goes.pb({t[v<<1].paths[i][0]+t[v<<1|1].paths[j][0],t[v<<1].paths[i][1],t[v<<1|1].paths[j][2]});
}
}
t[v].init(goes);
}
void build(int v,int tl,int tr){
if(tl==tr){
t[v]=dst[a[tl-1]][a[tl]];
}
else{
int tm=(tl+tr)>>1;
build(v<<1,tl,tm);build(v<<1|1,tm+1,tr);
pull(v);
}
}
void upd(int i,int v,int tl,int tr){
if(tl==tr){
t[v]=dst[a[tl-1]][a[tl]];
return;
}
int tm=(tl+tr)>>1;
if(tm>=i)
upd(i,v<<1,tl,tm);
else
upd(i,v<<1|1,tm+1,tr);
pull(v);
}
signed main(){
fast_resp;
int n,m,k,q;
cin>>n>>m>>q>>k;
vec<ar<ll,3>> e;
priority_queue<ar<ll,2>,vec<ar<ll,2>>,greater<ar<ll,2>>>pq;
for(int i=0;i<m;i++){
int v,u,c;
cin>>v>>u>>c;--v;--u;
g[v].pb({c,u,sz(e)});
e.pb({v,u,c});
g[u].pb({c,v,sz(e)});
e.pb({u,v,c});
}
for(int i=0;i<sz(e);i++){
fill(dt[i],dt[i]+sz(e),inf);
dt[i][i]=e[i][2];
pq.push({e[i][2],i});
while(!pq.empty()){
auto c=pq.top();pq.pop();
if(c[0]!=dt[i][c[1]]) continue;
int v=e[c[1]][1];
int last=e[c[1]][0];
ll cst=c[0];
// cout<<"I "<<i<<' '<<cst<<' '<<
for(auto &u : g[v]){
if(last==u[1]) continue;
if(umin(dt[i][u[2]],cst+u[0])){
pq.push({dt[i][u[2]],u[2]});
}
}
}
}
for(int i=0;i<sz(e);i++){
for(int j=0;j<sz(e);j++){
if(dt[i][j]==inf) continue;
int v=e[i][0],u=e[j][1];
jo[v][u].pb({dt[i][j],e[i][1],e[j][0]});
}
}
// return 0;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++)
dst[i][j].init(jo[i][j]);
}
// return 0;
for(int i=0;i<k;i++) cin>>a[i],--a[i];
// return 0;
build(1,1,k-1);
// return 0;
while(q--){
int i,x;
cin>>i>>x;--i;--x;
a[i]=x;
if(i+1<k) upd(i+1,1,1,k-1);
if(i) upd(i,1,1,k-1);
cout<<(t[1].paths[0][0]==inf?-1:t[1].paths[0][0])<<'\n';
}
return 0;
}
/*
4 4 1 3
1 2 1
2 3 1
1 3 1
1 4 1
2
4
2
2 4
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
94360 KB |
Output is correct |
2 |
Correct |
49 ms |
94524 KB |
Output is correct |
3 |
Correct |
49 ms |
94424 KB |
Output is correct |
4 |
Correct |
49 ms |
94440 KB |
Output is correct |
5 |
Correct |
48 ms |
94556 KB |
Output is correct |
6 |
Correct |
47 ms |
94468 KB |
Output is correct |
7 |
Correct |
46 ms |
94528 KB |
Output is correct |
8 |
Correct |
47 ms |
94452 KB |
Output is correct |
9 |
Correct |
48 ms |
94528 KB |
Output is correct |
10 |
Correct |
50 ms |
94420 KB |
Output is correct |
11 |
Correct |
54 ms |
94552 KB |
Output is correct |
12 |
Correct |
49 ms |
94440 KB |
Output is correct |
13 |
Correct |
56 ms |
94464 KB |
Output is correct |
14 |
Correct |
48 ms |
94540 KB |
Output is correct |
15 |
Correct |
48 ms |
94524 KB |
Output is correct |
16 |
Correct |
49 ms |
94468 KB |
Output is correct |
17 |
Correct |
48 ms |
94540 KB |
Output is correct |
18 |
Correct |
46 ms |
94540 KB |
Output is correct |
19 |
Correct |
48 ms |
94528 KB |
Output is correct |
20 |
Correct |
49 ms |
94440 KB |
Output is correct |
21 |
Correct |
47 ms |
94524 KB |
Output is correct |
22 |
Correct |
49 ms |
94408 KB |
Output is correct |
23 |
Correct |
49 ms |
94528 KB |
Output is correct |
24 |
Correct |
52 ms |
94516 KB |
Output is correct |
25 |
Correct |
57 ms |
94516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
94360 KB |
Output is correct |
2 |
Correct |
49 ms |
94524 KB |
Output is correct |
3 |
Correct |
49 ms |
94424 KB |
Output is correct |
4 |
Correct |
49 ms |
94440 KB |
Output is correct |
5 |
Correct |
48 ms |
94556 KB |
Output is correct |
6 |
Correct |
47 ms |
94468 KB |
Output is correct |
7 |
Correct |
46 ms |
94528 KB |
Output is correct |
8 |
Correct |
47 ms |
94452 KB |
Output is correct |
9 |
Correct |
48 ms |
94528 KB |
Output is correct |
10 |
Correct |
50 ms |
94420 KB |
Output is correct |
11 |
Correct |
54 ms |
94552 KB |
Output is correct |
12 |
Correct |
49 ms |
94440 KB |
Output is correct |
13 |
Correct |
56 ms |
94464 KB |
Output is correct |
14 |
Correct |
48 ms |
94540 KB |
Output is correct |
15 |
Correct |
48 ms |
94524 KB |
Output is correct |
16 |
Correct |
49 ms |
94468 KB |
Output is correct |
17 |
Correct |
48 ms |
94540 KB |
Output is correct |
18 |
Correct |
46 ms |
94540 KB |
Output is correct |
19 |
Correct |
48 ms |
94528 KB |
Output is correct |
20 |
Correct |
49 ms |
94440 KB |
Output is correct |
21 |
Correct |
47 ms |
94524 KB |
Output is correct |
22 |
Correct |
49 ms |
94408 KB |
Output is correct |
23 |
Correct |
49 ms |
94528 KB |
Output is correct |
24 |
Correct |
52 ms |
94516 KB |
Output is correct |
25 |
Correct |
57 ms |
94516 KB |
Output is correct |
26 |
Correct |
50 ms |
95164 KB |
Output is correct |
27 |
Correct |
77 ms |
128976 KB |
Output is correct |
28 |
Correct |
75 ms |
129096 KB |
Output is correct |
29 |
Correct |
198 ms |
145096 KB |
Output is correct |
30 |
Correct |
197 ms |
144584 KB |
Output is correct |
31 |
Correct |
201 ms |
144440 KB |
Output is correct |
32 |
Correct |
196 ms |
144932 KB |
Output is correct |
33 |
Correct |
200 ms |
146740 KB |
Output is correct |
34 |
Correct |
189 ms |
146840 KB |
Output is correct |
35 |
Correct |
184 ms |
146284 KB |
Output is correct |
36 |
Correct |
193 ms |
146280 KB |
Output is correct |
37 |
Correct |
194 ms |
146852 KB |
Output is correct |
38 |
Correct |
187 ms |
148904 KB |
Output is correct |
39 |
Correct |
204 ms |
148320 KB |
Output is correct |
40 |
Correct |
184 ms |
148732 KB |
Output is correct |
41 |
Correct |
185 ms |
148944 KB |
Output is correct |
42 |
Correct |
200 ms |
150596 KB |
Output is correct |
43 |
Correct |
188 ms |
151380 KB |
Output is correct |
44 |
Correct |
186 ms |
151492 KB |
Output is correct |
45 |
Correct |
117 ms |
148812 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
94360 KB |
Output is correct |
2 |
Correct |
49 ms |
94524 KB |
Output is correct |
3 |
Correct |
49 ms |
94424 KB |
Output is correct |
4 |
Correct |
49 ms |
94440 KB |
Output is correct |
5 |
Correct |
48 ms |
94556 KB |
Output is correct |
6 |
Correct |
47 ms |
94468 KB |
Output is correct |
7 |
Correct |
46 ms |
94528 KB |
Output is correct |
8 |
Correct |
47 ms |
94452 KB |
Output is correct |
9 |
Correct |
48 ms |
94528 KB |
Output is correct |
10 |
Correct |
50 ms |
94420 KB |
Output is correct |
11 |
Correct |
54 ms |
94552 KB |
Output is correct |
12 |
Correct |
49 ms |
94440 KB |
Output is correct |
13 |
Correct |
56 ms |
94464 KB |
Output is correct |
14 |
Correct |
48 ms |
94540 KB |
Output is correct |
15 |
Correct |
48 ms |
94524 KB |
Output is correct |
16 |
Correct |
49 ms |
94468 KB |
Output is correct |
17 |
Correct |
48 ms |
94540 KB |
Output is correct |
18 |
Correct |
46 ms |
94540 KB |
Output is correct |
19 |
Correct |
48 ms |
94528 KB |
Output is correct |
20 |
Correct |
49 ms |
94440 KB |
Output is correct |
21 |
Correct |
47 ms |
94524 KB |
Output is correct |
22 |
Correct |
49 ms |
94408 KB |
Output is correct |
23 |
Correct |
49 ms |
94528 KB |
Output is correct |
24 |
Correct |
52 ms |
94516 KB |
Output is correct |
25 |
Correct |
57 ms |
94516 KB |
Output is correct |
26 |
Correct |
50 ms |
95164 KB |
Output is correct |
27 |
Correct |
77 ms |
128976 KB |
Output is correct |
28 |
Correct |
75 ms |
129096 KB |
Output is correct |
29 |
Correct |
198 ms |
145096 KB |
Output is correct |
30 |
Correct |
197 ms |
144584 KB |
Output is correct |
31 |
Correct |
201 ms |
144440 KB |
Output is correct |
32 |
Correct |
196 ms |
144932 KB |
Output is correct |
33 |
Correct |
200 ms |
146740 KB |
Output is correct |
34 |
Correct |
189 ms |
146840 KB |
Output is correct |
35 |
Correct |
184 ms |
146284 KB |
Output is correct |
36 |
Correct |
193 ms |
146280 KB |
Output is correct |
37 |
Correct |
194 ms |
146852 KB |
Output is correct |
38 |
Correct |
187 ms |
148904 KB |
Output is correct |
39 |
Correct |
204 ms |
148320 KB |
Output is correct |
40 |
Correct |
184 ms |
148732 KB |
Output is correct |
41 |
Correct |
185 ms |
148944 KB |
Output is correct |
42 |
Correct |
200 ms |
150596 KB |
Output is correct |
43 |
Correct |
188 ms |
151380 KB |
Output is correct |
44 |
Correct |
186 ms |
151492 KB |
Output is correct |
45 |
Correct |
117 ms |
148812 KB |
Output is correct |
46 |
Correct |
274 ms |
145544 KB |
Output is correct |
47 |
Correct |
5468 ms |
769940 KB |
Output is correct |
48 |
Correct |
4988 ms |
809760 KB |
Output is correct |
49 |
Correct |
4514 ms |
884404 KB |
Output is correct |
50 |
Correct |
4691 ms |
868664 KB |
Output is correct |
51 |
Correct |
5070 ms |
869144 KB |
Output is correct |
52 |
Correct |
4070 ms |
914824 KB |
Output is correct |
53 |
Correct |
4117 ms |
918992 KB |
Output is correct |
54 |
Correct |
4100 ms |
911940 KB |
Output is correct |
55 |
Correct |
4102 ms |
917092 KB |
Output is correct |
56 |
Correct |
4112 ms |
943280 KB |
Output is correct |
57 |
Correct |
4046 ms |
967908 KB |
Output is correct |
58 |
Correct |
4112 ms |
995636 KB |
Output is correct |
59 |
Correct |
4152 ms |
1033140 KB |
Output is correct |
60 |
Runtime error |
3792 ms |
1048576 KB |
Execution killed with signal 9 |
61 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
47 ms |
94360 KB |
Output is correct |
2 |
Correct |
49 ms |
94524 KB |
Output is correct |
3 |
Correct |
49 ms |
94424 KB |
Output is correct |
4 |
Correct |
49 ms |
94440 KB |
Output is correct |
5 |
Correct |
48 ms |
94556 KB |
Output is correct |
6 |
Correct |
47 ms |
94468 KB |
Output is correct |
7 |
Correct |
46 ms |
94528 KB |
Output is correct |
8 |
Correct |
47 ms |
94452 KB |
Output is correct |
9 |
Correct |
48 ms |
94528 KB |
Output is correct |
10 |
Correct |
50 ms |
94420 KB |
Output is correct |
11 |
Correct |
54 ms |
94552 KB |
Output is correct |
12 |
Correct |
49 ms |
94440 KB |
Output is correct |
13 |
Correct |
56 ms |
94464 KB |
Output is correct |
14 |
Correct |
48 ms |
94540 KB |
Output is correct |
15 |
Correct |
48 ms |
94524 KB |
Output is correct |
16 |
Correct |
49 ms |
94468 KB |
Output is correct |
17 |
Correct |
48 ms |
94540 KB |
Output is correct |
18 |
Correct |
46 ms |
94540 KB |
Output is correct |
19 |
Correct |
48 ms |
94528 KB |
Output is correct |
20 |
Correct |
49 ms |
94440 KB |
Output is correct |
21 |
Correct |
47 ms |
94524 KB |
Output is correct |
22 |
Correct |
49 ms |
94408 KB |
Output is correct |
23 |
Correct |
49 ms |
94528 KB |
Output is correct |
24 |
Correct |
52 ms |
94516 KB |
Output is correct |
25 |
Correct |
57 ms |
94516 KB |
Output is correct |
26 |
Correct |
50 ms |
95164 KB |
Output is correct |
27 |
Correct |
77 ms |
128976 KB |
Output is correct |
28 |
Correct |
75 ms |
129096 KB |
Output is correct |
29 |
Correct |
198 ms |
145096 KB |
Output is correct |
30 |
Correct |
197 ms |
144584 KB |
Output is correct |
31 |
Correct |
201 ms |
144440 KB |
Output is correct |
32 |
Correct |
196 ms |
144932 KB |
Output is correct |
33 |
Correct |
200 ms |
146740 KB |
Output is correct |
34 |
Correct |
189 ms |
146840 KB |
Output is correct |
35 |
Correct |
184 ms |
146284 KB |
Output is correct |
36 |
Correct |
193 ms |
146280 KB |
Output is correct |
37 |
Correct |
194 ms |
146852 KB |
Output is correct |
38 |
Correct |
187 ms |
148904 KB |
Output is correct |
39 |
Correct |
204 ms |
148320 KB |
Output is correct |
40 |
Correct |
184 ms |
148732 KB |
Output is correct |
41 |
Correct |
185 ms |
148944 KB |
Output is correct |
42 |
Correct |
200 ms |
150596 KB |
Output is correct |
43 |
Correct |
188 ms |
151380 KB |
Output is correct |
44 |
Correct |
186 ms |
151492 KB |
Output is correct |
45 |
Correct |
117 ms |
148812 KB |
Output is correct |
46 |
Correct |
274 ms |
145544 KB |
Output is correct |
47 |
Correct |
5468 ms |
769940 KB |
Output is correct |
48 |
Correct |
4988 ms |
809760 KB |
Output is correct |
49 |
Correct |
4514 ms |
884404 KB |
Output is correct |
50 |
Correct |
4691 ms |
868664 KB |
Output is correct |
51 |
Correct |
5070 ms |
869144 KB |
Output is correct |
52 |
Correct |
4070 ms |
914824 KB |
Output is correct |
53 |
Correct |
4117 ms |
918992 KB |
Output is correct |
54 |
Correct |
4100 ms |
911940 KB |
Output is correct |
55 |
Correct |
4102 ms |
917092 KB |
Output is correct |
56 |
Correct |
4112 ms |
943280 KB |
Output is correct |
57 |
Correct |
4046 ms |
967908 KB |
Output is correct |
58 |
Correct |
4112 ms |
995636 KB |
Output is correct |
59 |
Correct |
4152 ms |
1033140 KB |
Output is correct |
60 |
Runtime error |
3792 ms |
1048576 KB |
Execution killed with signal 9 |
61 |
Halted |
0 ms |
0 KB |
- |