#include <bits/stdc++.h>
#define fr(i, n, m) for(int i = (n); i < (m); i ++)
#define pb push_back
#define st first
#define nd second
#define pq priority_queue
#define all(x) begin(x), end(x)
#include <time.h>
#include <cmath>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const int i_inf = 1e9;
const ll inf = 1e18;
const ll mod = 1000000007;
const ld eps = 1e-13;
const ld pi = 3.14159265359;
mt19937 _rand(time(NULL));
clock_t timer = clock();
const int mxn = 4003;
const ll A = 911382323;
const ll B = 972663749;
int n, m, t, len;
int a[200000];
vector<int> g[mxn];
int l[mxn], r[mxn], w[mxn];
ll dist[2][mxn][2][mxn];
bool vis[2][mxn][2][mxn];
void dijkstra(int edge, int side){
pq<pair<ll, pii> > Q;
Q.push({0, {edge, side}});
dist[side][edge][side][edge] = 0;
while(!Q.empty()){
int e = Q.top().nd.st;
int s = Q.top().nd.nd;
Q.pop();
if(vis[side][edge][s][e]) continue;
vis[side][edge][s][e] = true;
int node = l[e];
if(s == 1) node = r[e];
for(auto edge2 : g[node]){
if(edge2 == e || edge2>=m) continue;
int side2 = 0;
if(l[edge2] == node) side2 = 1;
if(dist[side][edge][s][e] + w[edge2] < dist[side][edge][side2][edge2]){
dist[side][edge][side2][edge2] = dist[side][edge][s][e] + w[edge2];
Q.push({-dist[side][edge][side2][edge2], {edge2, side2}});
}
}
}
}
void prec(){
fr(i, 0, 2) fr(j, 0, n+m) fr(k, 0, 2)fr(p, 0, n+m) dist[i][j][k][p] = inf;
fr(i, 0, n){
for(auto edge : g[i]){
int side = 0;
if(r[edge] == i) side = 1;
dijkstra(edge, side);
}
}
}
void solve(){
cin >> n >> m >> t >> len;
fr(i, 0, m){
cin >> l[i] >> r[i] >> w[i];
--l[i], --r[i];
g[l[i]].pb(i);
g[r[i]].pb(i);
}
fr(i, 0, n){
l[i+m] = r[i+m] = i;
g[i].pb(i+m);
}
fr(i, 0, len){
cin >> a[i];
--a[i];
}
prec();
fr(I, 0, t){
int p, q;
cin >> p >> q;
a[p-1] = q-1;
pq<pair<ll, pii> > Q;
Q.push({0, {a[0]+m, 1}});
fr(i, 1, len){
vector<pair<ll,pii> > v;
while(!Q.empty()){
ll w = Q.top().st;
int side = Q.top().nd.nd;
int edge = Q.top().nd.st;
Q.pop();
for(auto e : g[a[i]]){
if(e >= m) continue;
int s = 0;
if(a[i] == r[e]) s = 1;
v.pb({w + dist[side][edge][s][e], {e, s}});
}
}
sort(all(v));
fr(i, 0, (int)v.size()){
Q.push({v[i].st,{v[i].nd.st, v[i].nd.nd}});
int ed = v[i].nd.st;
while(i + 1 < (int)v.size() && v[i+1].nd.st == ed) i ++;
}
while(Q.size()>2){
Q.pop();
}
}
while(Q.size()>1) Q.pop();
ll ans = Q.top().st;
if(ans >= inf) ans = -1;
cout<<ans<<endl;
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int tc = 1;
//cin >> tc;
while(tc--) solve();
}
/*
5 10 1 10
1 2 8
1 3 5
1 4 2
1 5 1
2 3 7
2 4 9
2 5 6
3 4 10
3 5 10
4 5 6
4
2
4
3
5
3
1
4
3
5
3 5
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
768 KB |
Output is correct |
2 |
Correct |
1 ms |
896 KB |
Output is correct |
3 |
Correct |
1 ms |
896 KB |
Output is correct |
4 |
Correct |
1 ms |
896 KB |
Output is correct |
5 |
Correct |
1 ms |
896 KB |
Output is correct |
6 |
Correct |
1 ms |
896 KB |
Output is correct |
7 |
Correct |
1 ms |
896 KB |
Output is correct |
8 |
Correct |
1 ms |
896 KB |
Output is correct |
9 |
Correct |
1 ms |
1024 KB |
Output is correct |
10 |
Correct |
1 ms |
896 KB |
Output is correct |
11 |
Correct |
1 ms |
1024 KB |
Output is correct |
12 |
Correct |
1 ms |
1024 KB |
Output is correct |
13 |
Correct |
1 ms |
1024 KB |
Output is correct |
14 |
Correct |
1 ms |
1024 KB |
Output is correct |
15 |
Correct |
1 ms |
1024 KB |
Output is correct |
16 |
Correct |
1 ms |
1024 KB |
Output is correct |
17 |
Correct |
1 ms |
1024 KB |
Output is correct |
18 |
Correct |
1 ms |
1024 KB |
Output is correct |
19 |
Correct |
1 ms |
1024 KB |
Output is correct |
20 |
Correct |
1 ms |
1024 KB |
Output is correct |
21 |
Correct |
1 ms |
896 KB |
Output is correct |
22 |
Correct |
1 ms |
896 KB |
Output is correct |
23 |
Correct |
1 ms |
896 KB |
Output is correct |
24 |
Correct |
1 ms |
896 KB |
Output is correct |
25 |
Correct |
1 ms |
896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
768 KB |
Output is correct |
2 |
Correct |
1 ms |
896 KB |
Output is correct |
3 |
Correct |
1 ms |
896 KB |
Output is correct |
4 |
Correct |
1 ms |
896 KB |
Output is correct |
5 |
Correct |
1 ms |
896 KB |
Output is correct |
6 |
Correct |
1 ms |
896 KB |
Output is correct |
7 |
Correct |
1 ms |
896 KB |
Output is correct |
8 |
Correct |
1 ms |
896 KB |
Output is correct |
9 |
Correct |
1 ms |
1024 KB |
Output is correct |
10 |
Correct |
1 ms |
896 KB |
Output is correct |
11 |
Correct |
1 ms |
1024 KB |
Output is correct |
12 |
Correct |
1 ms |
1024 KB |
Output is correct |
13 |
Correct |
1 ms |
1024 KB |
Output is correct |
14 |
Correct |
1 ms |
1024 KB |
Output is correct |
15 |
Correct |
1 ms |
1024 KB |
Output is correct |
16 |
Correct |
1 ms |
1024 KB |
Output is correct |
17 |
Correct |
1 ms |
1024 KB |
Output is correct |
18 |
Correct |
1 ms |
1024 KB |
Output is correct |
19 |
Correct |
1 ms |
1024 KB |
Output is correct |
20 |
Correct |
1 ms |
1024 KB |
Output is correct |
21 |
Correct |
1 ms |
896 KB |
Output is correct |
22 |
Correct |
1 ms |
896 KB |
Output is correct |
23 |
Correct |
1 ms |
896 KB |
Output is correct |
24 |
Correct |
1 ms |
896 KB |
Output is correct |
25 |
Correct |
1 ms |
896 KB |
Output is correct |
26 |
Correct |
3 ms |
2304 KB |
Output is correct |
27 |
Correct |
41 ms |
7552 KB |
Output is correct |
28 |
Correct |
40 ms |
7552 KB |
Output is correct |
29 |
Correct |
201 ms |
17916 KB |
Output is correct |
30 |
Correct |
544 ms |
17912 KB |
Output is correct |
31 |
Correct |
1445 ms |
17912 KB |
Output is correct |
32 |
Correct |
1548 ms |
17912 KB |
Output is correct |
33 |
Correct |
167 ms |
20344 KB |
Output is correct |
34 |
Correct |
166 ms |
20412 KB |
Output is correct |
35 |
Correct |
1294 ms |
20404 KB |
Output is correct |
36 |
Correct |
1022 ms |
20472 KB |
Output is correct |
37 |
Correct |
183 ms |
20344 KB |
Output is correct |
38 |
Correct |
155 ms |
23028 KB |
Output is correct |
39 |
Correct |
709 ms |
23032 KB |
Output is correct |
40 |
Correct |
151 ms |
23032 KB |
Output is correct |
41 |
Correct |
149 ms |
23016 KB |
Output is correct |
42 |
Correct |
624 ms |
24824 KB |
Output is correct |
43 |
Correct |
135 ms |
25848 KB |
Output is correct |
44 |
Correct |
135 ms |
25944 KB |
Output is correct |
45 |
Correct |
91 ms |
28920 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
768 KB |
Output is correct |
2 |
Correct |
1 ms |
896 KB |
Output is correct |
3 |
Correct |
1 ms |
896 KB |
Output is correct |
4 |
Correct |
1 ms |
896 KB |
Output is correct |
5 |
Correct |
1 ms |
896 KB |
Output is correct |
6 |
Correct |
1 ms |
896 KB |
Output is correct |
7 |
Correct |
1 ms |
896 KB |
Output is correct |
8 |
Correct |
1 ms |
896 KB |
Output is correct |
9 |
Correct |
1 ms |
1024 KB |
Output is correct |
10 |
Correct |
1 ms |
896 KB |
Output is correct |
11 |
Correct |
1 ms |
1024 KB |
Output is correct |
12 |
Correct |
1 ms |
1024 KB |
Output is correct |
13 |
Correct |
1 ms |
1024 KB |
Output is correct |
14 |
Correct |
1 ms |
1024 KB |
Output is correct |
15 |
Correct |
1 ms |
1024 KB |
Output is correct |
16 |
Correct |
1 ms |
1024 KB |
Output is correct |
17 |
Correct |
1 ms |
1024 KB |
Output is correct |
18 |
Correct |
1 ms |
1024 KB |
Output is correct |
19 |
Correct |
1 ms |
1024 KB |
Output is correct |
20 |
Correct |
1 ms |
1024 KB |
Output is correct |
21 |
Correct |
1 ms |
896 KB |
Output is correct |
22 |
Correct |
1 ms |
896 KB |
Output is correct |
23 |
Correct |
1 ms |
896 KB |
Output is correct |
24 |
Correct |
1 ms |
896 KB |
Output is correct |
25 |
Correct |
1 ms |
896 KB |
Output is correct |
26 |
Correct |
3 ms |
2304 KB |
Output is correct |
27 |
Correct |
41 ms |
7552 KB |
Output is correct |
28 |
Correct |
40 ms |
7552 KB |
Output is correct |
29 |
Correct |
201 ms |
17916 KB |
Output is correct |
30 |
Correct |
544 ms |
17912 KB |
Output is correct |
31 |
Correct |
1445 ms |
17912 KB |
Output is correct |
32 |
Correct |
1548 ms |
17912 KB |
Output is correct |
33 |
Correct |
167 ms |
20344 KB |
Output is correct |
34 |
Correct |
166 ms |
20412 KB |
Output is correct |
35 |
Correct |
1294 ms |
20404 KB |
Output is correct |
36 |
Correct |
1022 ms |
20472 KB |
Output is correct |
37 |
Correct |
183 ms |
20344 KB |
Output is correct |
38 |
Correct |
155 ms |
23028 KB |
Output is correct |
39 |
Correct |
709 ms |
23032 KB |
Output is correct |
40 |
Correct |
151 ms |
23032 KB |
Output is correct |
41 |
Correct |
149 ms |
23016 KB |
Output is correct |
42 |
Correct |
624 ms |
24824 KB |
Output is correct |
43 |
Correct |
135 ms |
25848 KB |
Output is correct |
44 |
Correct |
135 ms |
25944 KB |
Output is correct |
45 |
Correct |
91 ms |
28920 KB |
Output is correct |
46 |
Correct |
247 ms |
36600 KB |
Output is correct |
47 |
Correct |
16789 ms |
272172 KB |
Output is correct |
48 |
Correct |
15928 ms |
329008 KB |
Output is correct |
49 |
Correct |
11346 ms |
370232 KB |
Output is correct |
50 |
Correct |
13332 ms |
370392 KB |
Output is correct |
51 |
Correct |
16337 ms |
370308 KB |
Output is correct |
52 |
Correct |
4062 ms |
370732 KB |
Output is correct |
53 |
Correct |
4093 ms |
370684 KB |
Output is correct |
54 |
Correct |
4117 ms |
370572 KB |
Output is correct |
55 |
Correct |
4136 ms |
370740 KB |
Output is correct |
56 |
Correct |
4162 ms |
391940 KB |
Output is correct |
57 |
Correct |
4131 ms |
414440 KB |
Output is correct |
58 |
Correct |
4090 ms |
436956 KB |
Output is correct |
59 |
Correct |
4069 ms |
460280 KB |
Output is correct |
60 |
Correct |
4005 ms |
483480 KB |
Output is correct |
61 |
Correct |
3929 ms |
496760 KB |
Output is correct |
62 |
Correct |
3793 ms |
510148 KB |
Output is correct |
63 |
Correct |
3697 ms |
523560 KB |
Output is correct |
64 |
Correct |
1445 ms |
549880 KB |
Output is correct |
65 |
Correct |
1438 ms |
550220 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
768 KB |
Output is correct |
2 |
Correct |
1 ms |
896 KB |
Output is correct |
3 |
Correct |
1 ms |
896 KB |
Output is correct |
4 |
Correct |
1 ms |
896 KB |
Output is correct |
5 |
Correct |
1 ms |
896 KB |
Output is correct |
6 |
Correct |
1 ms |
896 KB |
Output is correct |
7 |
Correct |
1 ms |
896 KB |
Output is correct |
8 |
Correct |
1 ms |
896 KB |
Output is correct |
9 |
Correct |
1 ms |
1024 KB |
Output is correct |
10 |
Correct |
1 ms |
896 KB |
Output is correct |
11 |
Correct |
1 ms |
1024 KB |
Output is correct |
12 |
Correct |
1 ms |
1024 KB |
Output is correct |
13 |
Correct |
1 ms |
1024 KB |
Output is correct |
14 |
Correct |
1 ms |
1024 KB |
Output is correct |
15 |
Correct |
1 ms |
1024 KB |
Output is correct |
16 |
Correct |
1 ms |
1024 KB |
Output is correct |
17 |
Correct |
1 ms |
1024 KB |
Output is correct |
18 |
Correct |
1 ms |
1024 KB |
Output is correct |
19 |
Correct |
1 ms |
1024 KB |
Output is correct |
20 |
Correct |
1 ms |
1024 KB |
Output is correct |
21 |
Correct |
1 ms |
896 KB |
Output is correct |
22 |
Correct |
1 ms |
896 KB |
Output is correct |
23 |
Correct |
1 ms |
896 KB |
Output is correct |
24 |
Correct |
1 ms |
896 KB |
Output is correct |
25 |
Correct |
1 ms |
896 KB |
Output is correct |
26 |
Correct |
3 ms |
2304 KB |
Output is correct |
27 |
Correct |
41 ms |
7552 KB |
Output is correct |
28 |
Correct |
40 ms |
7552 KB |
Output is correct |
29 |
Correct |
201 ms |
17916 KB |
Output is correct |
30 |
Correct |
544 ms |
17912 KB |
Output is correct |
31 |
Correct |
1445 ms |
17912 KB |
Output is correct |
32 |
Correct |
1548 ms |
17912 KB |
Output is correct |
33 |
Correct |
167 ms |
20344 KB |
Output is correct |
34 |
Correct |
166 ms |
20412 KB |
Output is correct |
35 |
Correct |
1294 ms |
20404 KB |
Output is correct |
36 |
Correct |
1022 ms |
20472 KB |
Output is correct |
37 |
Correct |
183 ms |
20344 KB |
Output is correct |
38 |
Correct |
155 ms |
23028 KB |
Output is correct |
39 |
Correct |
709 ms |
23032 KB |
Output is correct |
40 |
Correct |
151 ms |
23032 KB |
Output is correct |
41 |
Correct |
149 ms |
23016 KB |
Output is correct |
42 |
Correct |
624 ms |
24824 KB |
Output is correct |
43 |
Correct |
135 ms |
25848 KB |
Output is correct |
44 |
Correct |
135 ms |
25944 KB |
Output is correct |
45 |
Correct |
91 ms |
28920 KB |
Output is correct |
46 |
Correct |
247 ms |
36600 KB |
Output is correct |
47 |
Correct |
16789 ms |
272172 KB |
Output is correct |
48 |
Correct |
15928 ms |
329008 KB |
Output is correct |
49 |
Correct |
11346 ms |
370232 KB |
Output is correct |
50 |
Correct |
13332 ms |
370392 KB |
Output is correct |
51 |
Correct |
16337 ms |
370308 KB |
Output is correct |
52 |
Correct |
4062 ms |
370732 KB |
Output is correct |
53 |
Correct |
4093 ms |
370684 KB |
Output is correct |
54 |
Correct |
4117 ms |
370572 KB |
Output is correct |
55 |
Correct |
4136 ms |
370740 KB |
Output is correct |
56 |
Correct |
4162 ms |
391940 KB |
Output is correct |
57 |
Correct |
4131 ms |
414440 KB |
Output is correct |
58 |
Correct |
4090 ms |
436956 KB |
Output is correct |
59 |
Correct |
4069 ms |
460280 KB |
Output is correct |
60 |
Correct |
4005 ms |
483480 KB |
Output is correct |
61 |
Correct |
3929 ms |
496760 KB |
Output is correct |
62 |
Correct |
3793 ms |
510148 KB |
Output is correct |
63 |
Correct |
3697 ms |
523560 KB |
Output is correct |
64 |
Correct |
1445 ms |
549880 KB |
Output is correct |
65 |
Correct |
1438 ms |
550220 KB |
Output is correct |
66 |
Correct |
6744 ms |
3040 KB |
Output is correct |
67 |
Incorrect |
3358 ms |
181356 KB |
Output isn't correct |
68 |
Halted |
0 ms |
0 KB |
- |