#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,ll> 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 = 2e5+5;
const ll A = 911382323;
const ll B = 972663749;
int n, m, t, len;
ll dist[2][501][100000];
bool vis[2][501][100000];
void solve(){
cin >> n >> m >> t >> len;
vector<pii> g[n];
int l[m+1], r[m+1];
fr(i, 0, m){
int u, v, w;
cin >> u >> v >> w;
--u, --v;
l[i+1] = u, r[i+1] = v;
g[u].pb({i+1, w});
g[v].pb({i+1, w});
}
int a[len];
fr(i, 0, len){
cin >> a[i];
--a[i];
}
fr(I, 0, t){
int p, q;
cin >> p >> q;
a[p-1] = q-1;
l[0] = r[0] = a[0];
memset(vis, false, sizeof(vis));
fr(i, 0, 2) fr(j, 0, m+1) fr(k, 0, len) dist[i][j][k] = inf;
pq<pair<ll, pair<int,pii> > > Q;
Q.push({0, {0, {0, 0}}});
dist[0][0][0] = 0;
while(!Q.empty()){
int side = Q.top().nd.st;
int edge = Q.top().nd.nd.st;
int k = Q.top().nd.nd.nd;
Q.pop();
if(vis[side][edge][k]) continue;
vis[side][edge][k] = true;
int u;
if(side == 0){
u = l[edge];
}
else{
u = r[edge];
}
for(auto e : g[u]){
if(e.st == edge) continue;
int newside, newnode;
if(u == l[e.st]){
newnode = r[e.st];
newside = 1;
}
else{
newnode = l[e.st];
newside = 0;
}
int newk = k;
if(newnode == a[k+1]){
newk += 1;
}
if(dist[side][edge][k] + e.nd < dist[newside][e.st][newk]){
dist[newside][e.st][newk] = dist[side][edge][k] + e.nd;
Q.push({-dist[newside][e.st][newk], {newside,{e.st, newk}}});
}
}
}
ll ans = inf;
for(auto u : g[a[len-1]]){
int side;
if(a[len-1] == l[u.st]) side = 0;
else side = 1;
ans = min(ans, dist[side][u.st][len-1]);
}
if(ans == inf)cout<<-1<<endl;
else 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
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
98424 KB |
Output is correct |
2 |
Correct |
57 ms |
98552 KB |
Output is correct |
3 |
Correct |
64 ms |
98552 KB |
Output is correct |
4 |
Correct |
57 ms |
98608 KB |
Output is correct |
5 |
Correct |
57 ms |
98552 KB |
Output is correct |
6 |
Correct |
57 ms |
98552 KB |
Output is correct |
7 |
Correct |
58 ms |
98556 KB |
Output is correct |
8 |
Correct |
58 ms |
98552 KB |
Output is correct |
9 |
Correct |
62 ms |
98680 KB |
Output is correct |
10 |
Correct |
57 ms |
98552 KB |
Output is correct |
11 |
Correct |
58 ms |
98552 KB |
Output is correct |
12 |
Correct |
57 ms |
98532 KB |
Output is correct |
13 |
Correct |
58 ms |
98552 KB |
Output is correct |
14 |
Correct |
58 ms |
98552 KB |
Output is correct |
15 |
Correct |
58 ms |
98552 KB |
Output is correct |
16 |
Correct |
57 ms |
98552 KB |
Output is correct |
17 |
Correct |
57 ms |
98552 KB |
Output is correct |
18 |
Correct |
58 ms |
98552 KB |
Output is correct |
19 |
Correct |
58 ms |
98552 KB |
Output is correct |
20 |
Correct |
56 ms |
98552 KB |
Output is correct |
21 |
Correct |
57 ms |
98552 KB |
Output is correct |
22 |
Correct |
57 ms |
98552 KB |
Output is correct |
23 |
Correct |
57 ms |
98552 KB |
Output is correct |
24 |
Correct |
58 ms |
98552 KB |
Output is correct |
25 |
Correct |
59 ms |
98552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
98424 KB |
Output is correct |
2 |
Correct |
57 ms |
98552 KB |
Output is correct |
3 |
Correct |
64 ms |
98552 KB |
Output is correct |
4 |
Correct |
57 ms |
98608 KB |
Output is correct |
5 |
Correct |
57 ms |
98552 KB |
Output is correct |
6 |
Correct |
57 ms |
98552 KB |
Output is correct |
7 |
Correct |
58 ms |
98556 KB |
Output is correct |
8 |
Correct |
58 ms |
98552 KB |
Output is correct |
9 |
Correct |
62 ms |
98680 KB |
Output is correct |
10 |
Correct |
57 ms |
98552 KB |
Output is correct |
11 |
Correct |
58 ms |
98552 KB |
Output is correct |
12 |
Correct |
57 ms |
98532 KB |
Output is correct |
13 |
Correct |
58 ms |
98552 KB |
Output is correct |
14 |
Correct |
58 ms |
98552 KB |
Output is correct |
15 |
Correct |
58 ms |
98552 KB |
Output is correct |
16 |
Correct |
57 ms |
98552 KB |
Output is correct |
17 |
Correct |
57 ms |
98552 KB |
Output is correct |
18 |
Correct |
58 ms |
98552 KB |
Output is correct |
19 |
Correct |
58 ms |
98552 KB |
Output is correct |
20 |
Correct |
56 ms |
98552 KB |
Output is correct |
21 |
Correct |
57 ms |
98552 KB |
Output is correct |
22 |
Correct |
57 ms |
98552 KB |
Output is correct |
23 |
Correct |
57 ms |
98552 KB |
Output is correct |
24 |
Correct |
58 ms |
98552 KB |
Output is correct |
25 |
Correct |
59 ms |
98552 KB |
Output is correct |
26 |
Correct |
60 ms |
98936 KB |
Output is correct |
27 |
Correct |
159 ms |
257272 KB |
Output is correct |
28 |
Correct |
160 ms |
257272 KB |
Output is correct |
29 |
Correct |
12813 ms |
570488 KB |
Output is correct |
30 |
Correct |
14120 ms |
570616 KB |
Output is correct |
31 |
Correct |
15290 ms |
570496 KB |
Output is correct |
32 |
Correct |
16366 ms |
570744 KB |
Output is correct |
33 |
Correct |
12188 ms |
570580 KB |
Output is correct |
34 |
Correct |
12258 ms |
570480 KB |
Output is correct |
35 |
Correct |
13035 ms |
570408 KB |
Output is correct |
36 |
Correct |
13110 ms |
570356 KB |
Output is correct |
37 |
Correct |
12070 ms |
570488 KB |
Output is correct |
38 |
Correct |
11683 ms |
570592 KB |
Output is correct |
39 |
Correct |
11622 ms |
570352 KB |
Output is correct |
40 |
Correct |
11391 ms |
570488 KB |
Output is correct |
41 |
Correct |
11413 ms |
570596 KB |
Output is correct |
42 |
Correct |
10484 ms |
570324 KB |
Output is correct |
43 |
Correct |
10423 ms |
570552 KB |
Output is correct |
44 |
Correct |
10688 ms |
570488 KB |
Output is correct |
45 |
Correct |
3959 ms |
570460 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
98424 KB |
Output is correct |
2 |
Correct |
57 ms |
98552 KB |
Output is correct |
3 |
Correct |
64 ms |
98552 KB |
Output is correct |
4 |
Correct |
57 ms |
98608 KB |
Output is correct |
5 |
Correct |
57 ms |
98552 KB |
Output is correct |
6 |
Correct |
57 ms |
98552 KB |
Output is correct |
7 |
Correct |
58 ms |
98556 KB |
Output is correct |
8 |
Correct |
58 ms |
98552 KB |
Output is correct |
9 |
Correct |
62 ms |
98680 KB |
Output is correct |
10 |
Correct |
57 ms |
98552 KB |
Output is correct |
11 |
Correct |
58 ms |
98552 KB |
Output is correct |
12 |
Correct |
57 ms |
98532 KB |
Output is correct |
13 |
Correct |
58 ms |
98552 KB |
Output is correct |
14 |
Correct |
58 ms |
98552 KB |
Output is correct |
15 |
Correct |
58 ms |
98552 KB |
Output is correct |
16 |
Correct |
57 ms |
98552 KB |
Output is correct |
17 |
Correct |
57 ms |
98552 KB |
Output is correct |
18 |
Correct |
58 ms |
98552 KB |
Output is correct |
19 |
Correct |
58 ms |
98552 KB |
Output is correct |
20 |
Correct |
56 ms |
98552 KB |
Output is correct |
21 |
Correct |
57 ms |
98552 KB |
Output is correct |
22 |
Correct |
57 ms |
98552 KB |
Output is correct |
23 |
Correct |
57 ms |
98552 KB |
Output is correct |
24 |
Correct |
58 ms |
98552 KB |
Output is correct |
25 |
Correct |
59 ms |
98552 KB |
Output is correct |
26 |
Correct |
60 ms |
98936 KB |
Output is correct |
27 |
Correct |
159 ms |
257272 KB |
Output is correct |
28 |
Correct |
160 ms |
257272 KB |
Output is correct |
29 |
Correct |
12813 ms |
570488 KB |
Output is correct |
30 |
Correct |
14120 ms |
570616 KB |
Output is correct |
31 |
Correct |
15290 ms |
570496 KB |
Output is correct |
32 |
Correct |
16366 ms |
570744 KB |
Output is correct |
33 |
Correct |
12188 ms |
570580 KB |
Output is correct |
34 |
Correct |
12258 ms |
570480 KB |
Output is correct |
35 |
Correct |
13035 ms |
570408 KB |
Output is correct |
36 |
Correct |
13110 ms |
570356 KB |
Output is correct |
37 |
Correct |
12070 ms |
570488 KB |
Output is correct |
38 |
Correct |
11683 ms |
570592 KB |
Output is correct |
39 |
Correct |
11622 ms |
570352 KB |
Output is correct |
40 |
Correct |
11391 ms |
570488 KB |
Output is correct |
41 |
Correct |
11413 ms |
570596 KB |
Output is correct |
42 |
Correct |
10484 ms |
570324 KB |
Output is correct |
43 |
Correct |
10423 ms |
570552 KB |
Output is correct |
44 |
Correct |
10688 ms |
570488 KB |
Output is correct |
45 |
Correct |
3959 ms |
570460 KB |
Output is correct |
46 |
Correct |
82 ms |
104312 KB |
Output is correct |
47 |
Runtime error |
1303 ms |
1048576 KB |
Execution killed with signal 11 |
48 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
57 ms |
98424 KB |
Output is correct |
2 |
Correct |
57 ms |
98552 KB |
Output is correct |
3 |
Correct |
64 ms |
98552 KB |
Output is correct |
4 |
Correct |
57 ms |
98608 KB |
Output is correct |
5 |
Correct |
57 ms |
98552 KB |
Output is correct |
6 |
Correct |
57 ms |
98552 KB |
Output is correct |
7 |
Correct |
58 ms |
98556 KB |
Output is correct |
8 |
Correct |
58 ms |
98552 KB |
Output is correct |
9 |
Correct |
62 ms |
98680 KB |
Output is correct |
10 |
Correct |
57 ms |
98552 KB |
Output is correct |
11 |
Correct |
58 ms |
98552 KB |
Output is correct |
12 |
Correct |
57 ms |
98532 KB |
Output is correct |
13 |
Correct |
58 ms |
98552 KB |
Output is correct |
14 |
Correct |
58 ms |
98552 KB |
Output is correct |
15 |
Correct |
58 ms |
98552 KB |
Output is correct |
16 |
Correct |
57 ms |
98552 KB |
Output is correct |
17 |
Correct |
57 ms |
98552 KB |
Output is correct |
18 |
Correct |
58 ms |
98552 KB |
Output is correct |
19 |
Correct |
58 ms |
98552 KB |
Output is correct |
20 |
Correct |
56 ms |
98552 KB |
Output is correct |
21 |
Correct |
57 ms |
98552 KB |
Output is correct |
22 |
Correct |
57 ms |
98552 KB |
Output is correct |
23 |
Correct |
57 ms |
98552 KB |
Output is correct |
24 |
Correct |
58 ms |
98552 KB |
Output is correct |
25 |
Correct |
59 ms |
98552 KB |
Output is correct |
26 |
Correct |
60 ms |
98936 KB |
Output is correct |
27 |
Correct |
159 ms |
257272 KB |
Output is correct |
28 |
Correct |
160 ms |
257272 KB |
Output is correct |
29 |
Correct |
12813 ms |
570488 KB |
Output is correct |
30 |
Correct |
14120 ms |
570616 KB |
Output is correct |
31 |
Correct |
15290 ms |
570496 KB |
Output is correct |
32 |
Correct |
16366 ms |
570744 KB |
Output is correct |
33 |
Correct |
12188 ms |
570580 KB |
Output is correct |
34 |
Correct |
12258 ms |
570480 KB |
Output is correct |
35 |
Correct |
13035 ms |
570408 KB |
Output is correct |
36 |
Correct |
13110 ms |
570356 KB |
Output is correct |
37 |
Correct |
12070 ms |
570488 KB |
Output is correct |
38 |
Correct |
11683 ms |
570592 KB |
Output is correct |
39 |
Correct |
11622 ms |
570352 KB |
Output is correct |
40 |
Correct |
11391 ms |
570488 KB |
Output is correct |
41 |
Correct |
11413 ms |
570596 KB |
Output is correct |
42 |
Correct |
10484 ms |
570324 KB |
Output is correct |
43 |
Correct |
10423 ms |
570552 KB |
Output is correct |
44 |
Correct |
10688 ms |
570488 KB |
Output is correct |
45 |
Correct |
3959 ms |
570460 KB |
Output is correct |
46 |
Correct |
82 ms |
104312 KB |
Output is correct |
47 |
Runtime error |
1303 ms |
1048576 KB |
Execution killed with signal 11 |
48 |
Halted |
0 ms |
0 KB |
- |