107022024-04-09 23:17:07111Autó-tortúracpp17Accepted 100/100721ms13428 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;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1700 KiB
2Accepted347ms6936 KiB
subtask212/12
3Accepted3ms2132 KiB
4Accepted3ms2288 KiB
5Accepted3ms2516 KiB
6Accepted3ms2740 KiB
7Accepted3ms2976 KiB
8Accepted3ms3056 KiB
9Accepted3ms3128 KiB
10Accepted3ms3216 KiB
subtask318/18
11Accepted3ms2132 KiB
12Accepted3ms2288 KiB
13Accepted3ms2516 KiB
14Accepted3ms2740 KiB
15Accepted3ms2976 KiB
16Accepted3ms3056 KiB
17Accepted3ms3128 KiB
18Accepted3ms3216 KiB
19Accepted4ms3468 KiB
20Accepted14ms3820 KiB
21Accepted14ms4064 KiB
22Accepted13ms3896 KiB
23Accepted7ms3756 KiB
24Accepted4ms3664 KiB
25Accepted14ms4344 KiB
26Accepted13ms4276 KiB
27Accepted8ms4336 KiB
subtask425/25
28Accepted3ms2132 KiB
29Accepted3ms2288 KiB
30Accepted3ms2516 KiB
31Accepted3ms2740 KiB
32Accepted3ms2976 KiB
33Accepted3ms3056 KiB
34Accepted3ms3128 KiB
35Accepted3ms3216 KiB
36Accepted35ms5240 KiB
37Accepted32ms5312 KiB
38Accepted35ms5584 KiB
39Accepted24ms5424 KiB
40Accepted14ms5228 KiB
41Accepted8ms5304 KiB
42Accepted34ms5780 KiB
43Accepted26ms5824 KiB
44Accepted26ms5780 KiB
45Accepted14ms5716 KiB
subtask524/24
46Accepted455ms5904 KiB
47Accepted402ms5904 KiB
48Accepted444ms5852 KiB
49Accepted414ms5980 KiB
50Accepted266ms5836 KiB
51Accepted449ms5840 KiB
52Accepted398ms5724 KiB
53Accepted439ms5852 KiB
54Accepted409ms5980 KiB
55Accepted261ms5956 KiB
subtask621/21
56Accepted3ms2132 KiB
57Accepted3ms2288 KiB
58Accepted3ms2516 KiB
59Accepted3ms2740 KiB
60Accepted3ms2976 KiB
61Accepted3ms3056 KiB
62Accepted3ms3128 KiB
63Accepted3ms3216 KiB
64Accepted4ms3468 KiB
65Accepted14ms3820 KiB
66Accepted14ms4064 KiB
67Accepted13ms3896 KiB
68Accepted7ms3756 KiB
69Accepted4ms3664 KiB
70Accepted14ms4344 KiB
71Accepted13ms4276 KiB
72Accepted8ms4336 KiB
73Accepted35ms5240 KiB
74Accepted32ms5312 KiB
75Accepted35ms5584 KiB
76Accepted24ms5424 KiB
77Accepted14ms5228 KiB
78Accepted8ms5304 KiB
79Accepted34ms5780 KiB
80Accepted26ms5824 KiB
81Accepted26ms5780 KiB
82Accepted14ms5716 KiB
83Accepted455ms5904 KiB
84Accepted402ms5904 KiB
85Accepted444ms5852 KiB
86Accepted414ms5980 KiB
87Accepted266ms5836 KiB
88Accepted449ms5840 KiB
89Accepted398ms5724 KiB
90Accepted439ms5852 KiB
91Accepted409ms5980 KiB
92Accepted261ms5956 KiB
93Accepted108ms7152 KiB
94Accepted105ms7144 KiB
95Accepted128ms7376 KiB
96Accepted135ms7488 KiB
97Accepted46ms7084 KiB
98Accepted101ms8036 KiB
99Accepted94ms6712 KiB
100Accepted688ms13264 KiB
101Accepted721ms13428 KiB
102Accepted666ms13240 KiB
103Accepted574ms13108 KiB
104Accepted435ms13128 KiB
105Accepted564ms13060 KiB
106Accepted556ms13152 KiB
107Accepted550ms12996 KiB
108Accepted680ms13228 KiB
109Accepted143ms5692 KiB
110Accepted386ms8504 KiB
111Accepted451ms5912 KiB
112Accepted356ms6188 KiB
113Accepted381ms5964 KiB
114Accepted629ms13224 KiB
115Accepted90ms6864 KiB
116Accepted92ms6828 KiB
117Accepted575ms13164 KiB
118Accepted209ms12652 KiB