106982024-04-09 23:04:36111Autó-tortúracpp17Time limit exceeded 61/1001.6s8072 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 j=K-1;j>=0;j--){
			b[j]=min(b[j],b[j+1]);
			for(int k=0;k<B;k++){
				if(x[k][i]<=K-j&&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++){
					if(x[kk][i]<=K-j&&x[k][i-1]<=K){
						b[j]=min(b[j],a[x[k][i-1]]+x[k][i-1]+x[kk][i]+y[k][kk]);
					}
				}
			}
		}
		a=b;
	}
	cout<<(a[0]==INF?-1:a[0])<<'\n';
	return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms1824 KiB
2Time limit exceeded1.6s3788 KiB
subtask212/12
3Accepted3ms2564 KiB
4Accepted3ms2352 KiB
5Accepted3ms2676 KiB
6Accepted3ms2684 KiB
7Accepted3ms2824 KiB
8Accepted3ms2792 KiB
9Accepted3ms2924 KiB
10Accepted3ms3144 KiB
subtask30/18
11Accepted3ms2564 KiB
12Accepted3ms2352 KiB
13Accepted3ms2676 KiB
14Accepted3ms2684 KiB
15Accepted3ms2824 KiB
16Accepted3ms2792 KiB
17Accepted3ms2924 KiB
18Accepted3ms3144 KiB
19Accepted37ms3292 KiB
20Time limit exceeded1.56s3144 KiB
21Time limit exceeded1.57s3324 KiB
22Time limit exceeded1.58s3464 KiB
23Accepted439ms4196 KiB
24Accepted7ms4364 KiB
25Accepted59ms4788 KiB
26Accepted29ms4804 KiB
27Accepted14ms4768 KiB
subtask425/25
28Accepted3ms2564 KiB
29Accepted3ms2352 KiB
30Accepted3ms2676 KiB
31Accepted3ms2684 KiB
32Accepted3ms2824 KiB
33Accepted3ms2792 KiB
34Accepted3ms2924 KiB
35Accepted3ms3144 KiB
36Accepted574ms5184 KiB
37Accepted570ms5176 KiB
38Accepted575ms5252 KiB
39Accepted268ms4920 KiB
40Accepted79ms4664 KiB
41Accepted9ms4680 KiB
42Accepted79ms5224 KiB
43Accepted158ms5004 KiB
44Accepted68ms4984 KiB
45Accepted26ms4916 KiB
subtask524/24
46Accepted467ms4752 KiB
47Accepted414ms5036 KiB
48Accepted456ms4916 KiB
49Accepted423ms5012 KiB
50Accepted273ms4748 KiB
51Accepted458ms4864 KiB
52Accepted405ms4736 KiB
53Accepted448ms4872 KiB
54Accepted412ms4892 KiB
55Accepted263ms4736 KiB
subtask60/21
56Accepted3ms2564 KiB
57Accepted3ms2352 KiB
58Accepted3ms2676 KiB
59Accepted3ms2684 KiB
60Accepted3ms2824 KiB
61Accepted3ms2792 KiB
62Accepted3ms2924 KiB
63Accepted3ms3144 KiB
64Accepted37ms3292 KiB
65Time limit exceeded1.56s3144 KiB
66Time limit exceeded1.57s3324 KiB
67Time limit exceeded1.58s3464 KiB
68Accepted439ms4196 KiB
69Accepted7ms4364 KiB
70Accepted59ms4788 KiB
71Accepted29ms4804 KiB
72Accepted14ms4768 KiB
73Accepted574ms5184 KiB
74Accepted570ms5176 KiB
75Accepted575ms5252 KiB
76Accepted268ms4920 KiB
77Accepted79ms4664 KiB
78Accepted9ms4680 KiB
79Accepted79ms5224 KiB
80Accepted158ms5004 KiB
81Accepted68ms4984 KiB
82Accepted26ms4916 KiB
83Accepted467ms4752 KiB
84Accepted414ms5036 KiB
85Accepted456ms4916 KiB
86Accepted423ms5012 KiB
87Accepted273ms4748 KiB
88Accepted458ms4864 KiB
89Accepted405ms4736 KiB
90Accepted448ms4872 KiB
91Accepted412ms4892 KiB
92Accepted263ms4736 KiB
93Time limit exceeded1.531s4432 KiB
94Time limit exceeded1.554s4416 KiB
95Time limit exceeded1.557s4348 KiB
96Accepted356ms6272 KiB
97Accepted372ms5908 KiB
98Time limit exceeded1.557s4768 KiB
99Time limit exceeded1.554s4224 KiB
100Time limit exceeded1.567s7712 KiB
101Time limit exceeded1.562s7920 KiB
102Time limit exceeded1.57s7760 KiB
103Time limit exceeded1.567s7832 KiB
104Time limit exceeded1.575s7796 KiB
105Time limit exceeded1.567s7776 KiB
106Time limit exceeded1.55s7864 KiB
107Time limit exceeded1.529s7808 KiB
108Time limit exceeded1.577s8072 KiB
109Accepted252ms5252 KiB
110Time limit exceeded1.577s5436 KiB
111Accepted603ms5420 KiB
112Time limit exceeded1.565s4356 KiB
113Accepted691ms5352 KiB
114Time limit exceeded1.574s7832 KiB
115Accepted1.412s6340 KiB
116Accepted759ms6156 KiB
117Time limit exceeded1.562s7804 KiB
118Time limit exceeded1.582s7464 KiB