10702 | 2024-04-09 23:17:07 | 111 | Autó-tortúra | cpp17 | Accepted 100/100 | 721ms | 13428 KiB |
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define INF (int)1e16
signed main(){
ios::sync_with_stdio(false);
cin.tie(nullptr);
#ifdef CB
freopen("be2.txt","r",stdin);
// freopen("ki.txt","w",stdout);
#endif
int N,M,K;
cin>>N>>M>>K;
vector<vector<int>>g(N+1);
for(int i=0;i<M;i++){
int a,b;
cin>>a>>b;
g[a].push_back(b);
g[b].push_back(a);
}
int L;
cin>>L;
vector<int>l(L);
for(int i=0;i<L;i++){
cin>>l[i];
}
int B;
cin>>B;
vector<int>b(B);
for(int i=0;i<B;i++){
cin>>b[i];
}
vector<vector<int>>x(B,vector<int>(L,INF));
vector<vector<int>>y(B,vector<int>(B,INF));
for(int i=0;i<B;i++){
deque<int>q;
vector<int>d(N+1,INF);
d[b[i]]=0;
q.push_back(b[i]);
while(!q.empty()){
int i=q.front();
q.pop_front();
for(int j:g[i]){
if(d[i]+1<d[j]){
d[j]=d[i]+1;
q.push_back(j);
}
}
}
for(int j=0;j<L;j++){
if(d[l[j]]<=K){
x[i][j]=d[l[j]];
}
}
for(int j=0;j<B;j++){
if(d[b[j]]<=K){
y[i][j]=d[b[j]];
}
}
}
for(int i=0;i<B;i++){
for(int j=0;j<B;j++){
for(int k=0;k<B;k++){
y[j][k]=min(y[j][k],y[j][i]+y[i][k]);
}
}
}
vector<int>a(K+2);
a[K+1]=INF;
for(int i=1;i<L;i++){
vector<int>b(K+2,INF);
deque<int>q;
vector<int>d(N+1,INF);
d[l[i-1]]=0;
q.push_back(l[i-1]);
while(!q.empty()){
int i=q.front();
q.pop_front();
for(int j:g[i]){
if(d[i]+1<d[j]){
d[j]=d[i]+1;
q.push_back(j);
}
}
}
int dl=d[l[i]];
for(int j=0;j+dl<=K;j++){
b[j]=min(b[j],a[j+dl]+dl);
}
for(int k=0;k<B;k++){
int j=K-x[k][i];
if(j>=0&&x[k][i-1]<=K){
b[j]=min(b[j],a[x[k][i-1]]+x[k][i-1]+x[k][i]);
}
for(int kk=0;kk<B;kk++){
int jj=K-x[kk][i];
if(jj>=0&&x[k][i-1]<=K){
b[jj]=min(b[jj],a[x[k][i-1]]+x[k][i-1]+x[kk][i]+y[k][kk]);
}
}
}
for(int j=K-1;j>=0;j--){
b[j]=min(b[j],b[j+1]);
}
a=b;
}
cout<<(a[0]==INF?-1:a[0])<<'\n';
return 0;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 1700 KiB | ||||
2 | Accepted | 347ms | 6936 KiB | ||||
subtask2 | 12/12 | ||||||
3 | Accepted | 3ms | 2132 KiB | ||||
4 | Accepted | 3ms | 2288 KiB | ||||
5 | Accepted | 3ms | 2516 KiB | ||||
6 | Accepted | 3ms | 2740 KiB | ||||
7 | Accepted | 3ms | 2976 KiB | ||||
8 | Accepted | 3ms | 3056 KiB | ||||
9 | Accepted | 3ms | 3128 KiB | ||||
10 | Accepted | 3ms | 3216 KiB | ||||
subtask3 | 18/18 | ||||||
11 | Accepted | 3ms | 2132 KiB | ||||
12 | Accepted | 3ms | 2288 KiB | ||||
13 | Accepted | 3ms | 2516 KiB | ||||
14 | Accepted | 3ms | 2740 KiB | ||||
15 | Accepted | 3ms | 2976 KiB | ||||
16 | Accepted | 3ms | 3056 KiB | ||||
17 | Accepted | 3ms | 3128 KiB | ||||
18 | Accepted | 3ms | 3216 KiB | ||||
19 | Accepted | 4ms | 3468 KiB | ||||
20 | Accepted | 14ms | 3820 KiB | ||||
21 | Accepted | 14ms | 4064 KiB | ||||
22 | Accepted | 13ms | 3896 KiB | ||||
23 | Accepted | 7ms | 3756 KiB | ||||
24 | Accepted | 4ms | 3664 KiB | ||||
25 | Accepted | 14ms | 4344 KiB | ||||
26 | Accepted | 13ms | 4276 KiB | ||||
27 | Accepted | 8ms | 4336 KiB | ||||
subtask4 | 25/25 | ||||||
28 | Accepted | 3ms | 2132 KiB | ||||
29 | Accepted | 3ms | 2288 KiB | ||||
30 | Accepted | 3ms | 2516 KiB | ||||
31 | Accepted | 3ms | 2740 KiB | ||||
32 | Accepted | 3ms | 2976 KiB | ||||
33 | Accepted | 3ms | 3056 KiB | ||||
34 | Accepted | 3ms | 3128 KiB | ||||
35 | Accepted | 3ms | 3216 KiB | ||||
36 | Accepted | 35ms | 5240 KiB | ||||
37 | Accepted | 32ms | 5312 KiB | ||||
38 | Accepted | 35ms | 5584 KiB | ||||
39 | Accepted | 24ms | 5424 KiB | ||||
40 | Accepted | 14ms | 5228 KiB | ||||
41 | Accepted | 8ms | 5304 KiB | ||||
42 | Accepted | 34ms | 5780 KiB | ||||
43 | Accepted | 26ms | 5824 KiB | ||||
44 | Accepted | 26ms | 5780 KiB | ||||
45 | Accepted | 14ms | 5716 KiB | ||||
subtask5 | 24/24 | ||||||
46 | Accepted | 455ms | 5904 KiB | ||||
47 | Accepted | 402ms | 5904 KiB | ||||
48 | Accepted | 444ms | 5852 KiB | ||||
49 | Accepted | 414ms | 5980 KiB | ||||
50 | Accepted | 266ms | 5836 KiB | ||||
51 | Accepted | 449ms | 5840 KiB | ||||
52 | Accepted | 398ms | 5724 KiB | ||||
53 | Accepted | 439ms | 5852 KiB | ||||
54 | Accepted | 409ms | 5980 KiB | ||||
55 | Accepted | 261ms | 5956 KiB | ||||
subtask6 | 21/21 | ||||||
56 | Accepted | 3ms | 2132 KiB | ||||
57 | Accepted | 3ms | 2288 KiB | ||||
58 | Accepted | 3ms | 2516 KiB | ||||
59 | Accepted | 3ms | 2740 KiB | ||||
60 | Accepted | 3ms | 2976 KiB | ||||
61 | Accepted | 3ms | 3056 KiB | ||||
62 | Accepted | 3ms | 3128 KiB | ||||
63 | Accepted | 3ms | 3216 KiB | ||||
64 | Accepted | 4ms | 3468 KiB | ||||
65 | Accepted | 14ms | 3820 KiB | ||||
66 | Accepted | 14ms | 4064 KiB | ||||
67 | Accepted | 13ms | 3896 KiB | ||||
68 | Accepted | 7ms | 3756 KiB | ||||
69 | Accepted | 4ms | 3664 KiB | ||||
70 | Accepted | 14ms | 4344 KiB | ||||
71 | Accepted | 13ms | 4276 KiB | ||||
72 | Accepted | 8ms | 4336 KiB | ||||
73 | Accepted | 35ms | 5240 KiB | ||||
74 | Accepted | 32ms | 5312 KiB | ||||
75 | Accepted | 35ms | 5584 KiB | ||||
76 | Accepted | 24ms | 5424 KiB | ||||
77 | Accepted | 14ms | 5228 KiB | ||||
78 | Accepted | 8ms | 5304 KiB | ||||
79 | Accepted | 34ms | 5780 KiB | ||||
80 | Accepted | 26ms | 5824 KiB | ||||
81 | Accepted | 26ms | 5780 KiB | ||||
82 | Accepted | 14ms | 5716 KiB | ||||
83 | Accepted | 455ms | 5904 KiB | ||||
84 | Accepted | 402ms | 5904 KiB | ||||
85 | Accepted | 444ms | 5852 KiB | ||||
86 | Accepted | 414ms | 5980 KiB | ||||
87 | Accepted | 266ms | 5836 KiB | ||||
88 | Accepted | 449ms | 5840 KiB | ||||
89 | Accepted | 398ms | 5724 KiB | ||||
90 | Accepted | 439ms | 5852 KiB | ||||
91 | Accepted | 409ms | 5980 KiB | ||||
92 | Accepted | 261ms | 5956 KiB | ||||
93 | Accepted | 108ms | 7152 KiB | ||||
94 | Accepted | 105ms | 7144 KiB | ||||
95 | Accepted | 128ms | 7376 KiB | ||||
96 | Accepted | 135ms | 7488 KiB | ||||
97 | Accepted | 46ms | 7084 KiB | ||||
98 | Accepted | 101ms | 8036 KiB | ||||
99 | Accepted | 94ms | 6712 KiB | ||||
100 | Accepted | 688ms | 13264 KiB | ||||
101 | Accepted | 721ms | 13428 KiB | ||||
102 | Accepted | 666ms | 13240 KiB | ||||
103 | Accepted | 574ms | 13108 KiB | ||||
104 | Accepted | 435ms | 13128 KiB | ||||
105 | Accepted | 564ms | 13060 KiB | ||||
106 | Accepted | 556ms | 13152 KiB | ||||
107 | Accepted | 550ms | 12996 KiB | ||||
108 | Accepted | 680ms | 13228 KiB | ||||
109 | Accepted | 143ms | 5692 KiB | ||||
110 | Accepted | 386ms | 8504 KiB | ||||
111 | Accepted | 451ms | 5912 KiB | ||||
112 | Accepted | 356ms | 6188 KiB | ||||
113 | Accepted | 381ms | 5964 KiB | ||||
114 | Accepted | 629ms | 13224 KiB | ||||
115 | Accepted | 90ms | 6864 KiB | ||||
116 | Accepted | 92ms | 6828 KiB | ||||
117 | Accepted | 575ms | 13164 KiB | ||||
118 | Accepted | 209ms | 12652 KiB |