#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 = 2e18;
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[100005];
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();
pair<ll, pii> mi1;
pair<ll, pii> mi2;
pair<ll, pii> tmp;
fr(I, 0, t){
int p, q;
cin >> p >> q;
a[p-1] = q-1;
vector<pair<ll, pii> > v;
v = {{0, {a[0]+m, 1}}};
fr(i, 1, len){
mi1.st = inf;
mi2.st = inf;
for(auto G : v){
ll w = G.st;
int side = G.nd.nd;
int edge = G.nd.st;
for(auto e : g[a[i]]){
if(e >= m) continue;
int s = 0;
if(a[i] == r[e]) s = 1;
tmp = {min(inf, w + dist[side][edge][s][e]), {e, s}};
if(tmp.st < mi1.st){
if(tmp.nd.st != mi1.nd.st){
mi2 = mi1;
mi1 = tmp;
}
else{
mi1 = tmp;
}
}
else if(tmp.st < mi2.st){
if(tmp.nd.st != mi1.nd.st){
mi2 = tmp;
}
}
}
}
if(mi1.st == inf){
v = {};
break;
}
if(mi2.st == inf) v = {mi1};
else v = {mi1, mi2};
}
ll ans = inf;
if(!v.empty()) ans = v[0].st;
if(ans == inf) ans = -1;
cout<<ans<<endl;
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
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
*/
# |
Verdict |
Execution time |
Memory |
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 |
896 KB |
Output is correct |
10 |
Correct |
1 ms |
1024 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 |
# |
Verdict |
Execution time |
Memory |
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 |
896 KB |
Output is correct |
10 |
Correct |
1 ms |
1024 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 |
14 ms |
7552 KB |
Output is correct |
28 |
Correct |
13 ms |
7552 KB |
Output is correct |
29 |
Correct |
112 ms |
17784 KB |
Output is correct |
30 |
Correct |
124 ms |
17912 KB |
Output is correct |
31 |
Correct |
196 ms |
17784 KB |
Output is correct |
32 |
Correct |
188 ms |
17912 KB |
Output is correct |
33 |
Correct |
107 ms |
20344 KB |
Output is correct |
34 |
Correct |
110 ms |
20344 KB |
Output is correct |
35 |
Correct |
196 ms |
20344 KB |
Output is correct |
36 |
Correct |
158 ms |
20344 KB |
Output is correct |
37 |
Correct |
109 ms |
20472 KB |
Output is correct |
38 |
Correct |
102 ms |
23032 KB |
Output is correct |
39 |
Correct |
133 ms |
23032 KB |
Output is correct |
40 |
Correct |
103 ms |
23004 KB |
Output is correct |
41 |
Correct |
105 ms |
23056 KB |
Output is correct |
42 |
Correct |
130 ms |
24696 KB |
Output is correct |
43 |
Correct |
97 ms |
25848 KB |
Output is correct |
44 |
Correct |
99 ms |
25848 KB |
Output is correct |
45 |
Correct |
59 ms |
28920 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
896 KB |
Output is correct |
10 |
Correct |
1 ms |
1024 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 |
14 ms |
7552 KB |
Output is correct |
28 |
Correct |
13 ms |
7552 KB |
Output is correct |
29 |
Correct |
112 ms |
17784 KB |
Output is correct |
30 |
Correct |
124 ms |
17912 KB |
Output is correct |
31 |
Correct |
196 ms |
17784 KB |
Output is correct |
32 |
Correct |
188 ms |
17912 KB |
Output is correct |
33 |
Correct |
107 ms |
20344 KB |
Output is correct |
34 |
Correct |
110 ms |
20344 KB |
Output is correct |
35 |
Correct |
196 ms |
20344 KB |
Output is correct |
36 |
Correct |
158 ms |
20344 KB |
Output is correct |
37 |
Correct |
109 ms |
20472 KB |
Output is correct |
38 |
Correct |
102 ms |
23032 KB |
Output is correct |
39 |
Correct |
133 ms |
23032 KB |
Output is correct |
40 |
Correct |
103 ms |
23004 KB |
Output is correct |
41 |
Correct |
105 ms |
23056 KB |
Output is correct |
42 |
Correct |
130 ms |
24696 KB |
Output is correct |
43 |
Correct |
97 ms |
25848 KB |
Output is correct |
44 |
Correct |
99 ms |
25848 KB |
Output is correct |
45 |
Correct |
59 ms |
28920 KB |
Output is correct |
46 |
Correct |
234 ms |
36600 KB |
Output is correct |
47 |
Correct |
8735 ms |
272176 KB |
Output is correct |
48 |
Correct |
8029 ms |
329084 KB |
Output is correct |
49 |
Correct |
6348 ms |
370004 KB |
Output is correct |
50 |
Correct |
6957 ms |
370200 KB |
Output is correct |
51 |
Correct |
8016 ms |
370204 KB |
Output is correct |
52 |
Correct |
3789 ms |
370516 KB |
Output is correct |
53 |
Correct |
3808 ms |
370332 KB |
Output is correct |
54 |
Correct |
3819 ms |
370256 KB |
Output is correct |
55 |
Correct |
3790 ms |
370280 KB |
Output is correct |
56 |
Correct |
3833 ms |
391736 KB |
Output is correct |
57 |
Correct |
3829 ms |
413872 KB |
Output is correct |
58 |
Correct |
3857 ms |
436576 KB |
Output is correct |
59 |
Correct |
3815 ms |
459752 KB |
Output is correct |
60 |
Correct |
3761 ms |
482852 KB |
Output is correct |
61 |
Correct |
3711 ms |
496332 KB |
Output is correct |
62 |
Correct |
3580 ms |
509840 KB |
Output is correct |
63 |
Correct |
3470 ms |
522744 KB |
Output is correct |
64 |
Correct |
1362 ms |
549464 KB |
Output is correct |
65 |
Correct |
1371 ms |
549496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
896 KB |
Output is correct |
10 |
Correct |
1 ms |
1024 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 |
14 ms |
7552 KB |
Output is correct |
28 |
Correct |
13 ms |
7552 KB |
Output is correct |
29 |
Correct |
112 ms |
17784 KB |
Output is correct |
30 |
Correct |
124 ms |
17912 KB |
Output is correct |
31 |
Correct |
196 ms |
17784 KB |
Output is correct |
32 |
Correct |
188 ms |
17912 KB |
Output is correct |
33 |
Correct |
107 ms |
20344 KB |
Output is correct |
34 |
Correct |
110 ms |
20344 KB |
Output is correct |
35 |
Correct |
196 ms |
20344 KB |
Output is correct |
36 |
Correct |
158 ms |
20344 KB |
Output is correct |
37 |
Correct |
109 ms |
20472 KB |
Output is correct |
38 |
Correct |
102 ms |
23032 KB |
Output is correct |
39 |
Correct |
133 ms |
23032 KB |
Output is correct |
40 |
Correct |
103 ms |
23004 KB |
Output is correct |
41 |
Correct |
105 ms |
23056 KB |
Output is correct |
42 |
Correct |
130 ms |
24696 KB |
Output is correct |
43 |
Correct |
97 ms |
25848 KB |
Output is correct |
44 |
Correct |
99 ms |
25848 KB |
Output is correct |
45 |
Correct |
59 ms |
28920 KB |
Output is correct |
46 |
Correct |
234 ms |
36600 KB |
Output is correct |
47 |
Correct |
8735 ms |
272176 KB |
Output is correct |
48 |
Correct |
8029 ms |
329084 KB |
Output is correct |
49 |
Correct |
6348 ms |
370004 KB |
Output is correct |
50 |
Correct |
6957 ms |
370200 KB |
Output is correct |
51 |
Correct |
8016 ms |
370204 KB |
Output is correct |
52 |
Correct |
3789 ms |
370516 KB |
Output is correct |
53 |
Correct |
3808 ms |
370332 KB |
Output is correct |
54 |
Correct |
3819 ms |
370256 KB |
Output is correct |
55 |
Correct |
3790 ms |
370280 KB |
Output is correct |
56 |
Correct |
3833 ms |
391736 KB |
Output is correct |
57 |
Correct |
3829 ms |
413872 KB |
Output is correct |
58 |
Correct |
3857 ms |
436576 KB |
Output is correct |
59 |
Correct |
3815 ms |
459752 KB |
Output is correct |
60 |
Correct |
3761 ms |
482852 KB |
Output is correct |
61 |
Correct |
3711 ms |
496332 KB |
Output is correct |
62 |
Correct |
3580 ms |
509840 KB |
Output is correct |
63 |
Correct |
3470 ms |
522744 KB |
Output is correct |
64 |
Correct |
1362 ms |
549464 KB |
Output is correct |
65 |
Correct |
1371 ms |
549496 KB |
Output is correct |
66 |
Correct |
1078 ms |
2688 KB |
Output is correct |
67 |
Correct |
593 ms |
180088 KB |
Output is correct |
68 |
Correct |
1291 ms |
546020 KB |
Output is correct |
69 |
Correct |
1558 ms |
546380 KB |
Output is correct |
70 |
Correct |
1541 ms |
546636 KB |
Output is correct |
71 |
Execution timed out |
18081 ms |
272196 KB |
Time limit exceeded |
72 |
Halted |
0 ms |
0 KB |
- |