10969 | 2024-04-22 17:56:40 | k_balint | Autó-tortúra | cpp17 | Accepted 100/100 | 949ms | 217200 KiB |
#include <bits/stdc++.h>
using namespace std;
const int c=3005;
const int inf = 1e8;
int n,K,m,l,b;
vector<int> adj[c];
int dist[c][c];
int distb[c][c];
int arr[c];
int brr[c];
int dp[c][c];
void bfs(int v){
dist[v][v]=0;
queue<int> q;
q.push(v);
while(!q.empty()){
int cur=q.front();
q.pop();
for(int x:adj[cur]){
if(dist[v][x]==inf){
dist[v][x]=dist[v][cur]+1;
q.push(x);
}
}
}
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m>>K;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(i==j) dist[i][j]=0, distb[i][j]=0;
else dist[i][j]=inf, distb[i][j]=inf;
}
}
for(int i=1;i<=m;i++){
int a,b; cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
cin>>l;
for(int i=1;i<=l;i++) cin>>brr[i];
cin>>b;
for(int i=1;i<=b;i++) cin>>arr[i];
for(int i=1;i<=n;i++) bfs(i);
for(int i=1;i<=b;i++){
for(int j=1;j<=b;j++){
distb[arr[i]][arr[j]]=dist[arr[i]][arr[j]]>K?inf:dist[arr[i]][arr[j]];
}
}
for(int k=1;k<=b;k++){
for(int i=1;i<=b;i++){
for(int j=1;j<=b;j++){
int a=arr[i],b=arr[j],v=arr[k];
if(distb[a][v] < inf && distb[v][b] < inf){
distb[a][b]=min(distb[a][v]+distb[v][b], distb[a][b]);
}
}
}
}
for(int i=1;i<l;i++){
for(int j=0;j<=K;j++) dp[i+1][j]=inf;
int u=brr[i], v=brr[i+1];
for(int j=1; j<=b;j++){
for(int k=1;k<=b;k++){
int a=arr[j],b=arr[k];
if(dist[b][v]>K || dist[u][a]>K) continue;
dp[i+1][K-dist[b][v]] = min(dp[i+1][K-dist[b][v]], dp[i][dist[u][a]] + dist[u][a] + distb[a][b] + dist[b][v]);
}
}
for(int j=K;j>=dist[u][v];j--){
dp[i+1][j-dist[u][v]]=min(dp[i+1][j-dist[u][v]], dp[i][j]+dist[u][v]);
}
for(int j=K-1;j>=0;j--){
dp[i+1][j]=min(dp[i+1][j],dp[i+1][j+1]);
}
}
if(dp[l][0]>=inf) cout << -1 << endl;
else cout << dp[l][0] << endl;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 2084 KiB | ||||
2 | Accepted | 476ms | 134368 KiB | ||||
subtask2 | 12/12 | ||||||
3 | Accepted | 3ms | 2648 KiB | ||||
4 | Accepted | 3ms | 2896 KiB | ||||
5 | Accepted | 3ms | 3108 KiB | ||||
6 | Accepted | 3ms | 3536 KiB | ||||
7 | Accepted | 3ms | 3264 KiB | ||||
8 | Accepted | 3ms | 3452 KiB | ||||
9 | Accepted | 3ms | 3440 KiB | ||||
10 | Accepted | 3ms | 3588 KiB | ||||
subtask3 | 18/18 | ||||||
11 | Accepted | 3ms | 2648 KiB | ||||
12 | Accepted | 3ms | 2896 KiB | ||||
13 | Accepted | 3ms | 3108 KiB | ||||
14 | Accepted | 3ms | 3536 KiB | ||||
15 | Accepted | 3ms | 3264 KiB | ||||
16 | Accepted | 3ms | 3452 KiB | ||||
17 | Accepted | 3ms | 3440 KiB | ||||
18 | Accepted | 3ms | 3588 KiB | ||||
19 | Accepted | 4ms | 6096 KiB | ||||
20 | Accepted | 23ms | 11248 KiB | ||||
21 | Accepted | 20ms | 11332 KiB | ||||
22 | Accepted | 21ms | 11436 KiB | ||||
23 | Accepted | 9ms | 11528 KiB | ||||
24 | Accepted | 9ms | 9388 KiB | ||||
25 | Accepted | 17ms | 11332 KiB | ||||
26 | Accepted | 14ms | 11428 KiB | ||||
27 | Accepted | 9ms | 11408 KiB | ||||
subtask4 | 25/25 | ||||||
28 | Accepted | 3ms | 2648 KiB | ||||
29 | Accepted | 3ms | 2896 KiB | ||||
30 | Accepted | 3ms | 3108 KiB | ||||
31 | Accepted | 3ms | 3536 KiB | ||||
32 | Accepted | 3ms | 3264 KiB | ||||
33 | Accepted | 3ms | 3452 KiB | ||||
34 | Accepted | 3ms | 3440 KiB | ||||
35 | Accepted | 3ms | 3588 KiB | ||||
36 | Accepted | 495ms | 146008 KiB | ||||
37 | Accepted | 435ms | 146072 KiB | ||||
38 | Accepted | 476ms | 146072 KiB | ||||
39 | Accepted | 481ms | 146180 KiB | ||||
40 | Accepted | 421ms | 146172 KiB | ||||
41 | Accepted | 405ms | 146168 KiB | ||||
42 | Accepted | 453ms | 146184 KiB | ||||
43 | Accepted | 314ms | 146156 KiB | ||||
44 | Accepted | 328ms | 146188 KiB | ||||
45 | Accepted | 155ms | 146188 KiB | ||||
subtask5 | 24/24 | ||||||
46 | Accepted | 499ms | 181828 KiB | ||||
47 | Accepted | 437ms | 181880 KiB | ||||
48 | Accepted | 495ms | 181932 KiB | ||||
49 | Accepted | 446ms | 181828 KiB | ||||
50 | Accepted | 319ms | 182092 KiB | ||||
51 | Accepted | 493ms | 175080 KiB | ||||
52 | Accepted | 430ms | 175268 KiB | ||||
53 | Accepted | 490ms | 175136 KiB | ||||
54 | Accepted | 439ms | 175084 KiB | ||||
55 | Accepted | 293ms | 175092 KiB | ||||
subtask6 | 21/21 | ||||||
56 | Accepted | 3ms | 2648 KiB | ||||
57 | Accepted | 3ms | 2896 KiB | ||||
58 | Accepted | 3ms | 3108 KiB | ||||
59 | Accepted | 3ms | 3536 KiB | ||||
60 | Accepted | 3ms | 3264 KiB | ||||
61 | Accepted | 3ms | 3452 KiB | ||||
62 | Accepted | 3ms | 3440 KiB | ||||
63 | Accepted | 3ms | 3588 KiB | ||||
64 | Accepted | 4ms | 6096 KiB | ||||
65 | Accepted | 23ms | 11248 KiB | ||||
66 | Accepted | 20ms | 11332 KiB | ||||
67 | Accepted | 21ms | 11436 KiB | ||||
68 | Accepted | 9ms | 11528 KiB | ||||
69 | Accepted | 9ms | 9388 KiB | ||||
70 | Accepted | 17ms | 11332 KiB | ||||
71 | Accepted | 14ms | 11428 KiB | ||||
72 | Accepted | 9ms | 11408 KiB | ||||
73 | Accepted | 495ms | 146008 KiB | ||||
74 | Accepted | 435ms | 146072 KiB | ||||
75 | Accepted | 476ms | 146072 KiB | ||||
76 | Accepted | 481ms | 146180 KiB | ||||
77 | Accepted | 421ms | 146172 KiB | ||||
78 | Accepted | 405ms | 146168 KiB | ||||
79 | Accepted | 453ms | 146184 KiB | ||||
80 | Accepted | 314ms | 146156 KiB | ||||
81 | Accepted | 328ms | 146188 KiB | ||||
82 | Accepted | 155ms | 146188 KiB | ||||
83 | Accepted | 499ms | 181828 KiB | ||||
84 | Accepted | 437ms | 181880 KiB | ||||
85 | Accepted | 495ms | 181932 KiB | ||||
86 | Accepted | 446ms | 181828 KiB | ||||
87 | Accepted | 319ms | 182092 KiB | ||||
88 | Accepted | 493ms | 175080 KiB | ||||
89 | Accepted | 430ms | 175268 KiB | ||||
90 | Accepted | 490ms | 175136 KiB | ||||
91 | Accepted | 439ms | 175084 KiB | ||||
92 | Accepted | 293ms | 175092 KiB | ||||
93 | Accepted | 421ms | 152464 KiB | ||||
94 | Accepted | 389ms | 151036 KiB | ||||
95 | Accepted | 497ms | 151108 KiB | ||||
96 | Accepted | 554ms | 150748 KiB | ||||
97 | Accepted | 168ms | 150796 KiB | ||||
98 | Accepted | 149ms | 49172 KiB | ||||
99 | Accepted | 518ms | 152752 KiB | ||||
100 | Accepted | 904ms | 205860 KiB | ||||
101 | Accepted | 949ms | 217040 KiB | ||||
102 | Accepted | 843ms | 172972 KiB | ||||
103 | Accepted | 815ms | 217004 KiB | ||||
104 | Accepted | 643ms | 217016 KiB | ||||
105 | Accepted | 802ms | 217020 KiB | ||||
106 | Accepted | 788ms | 217056 KiB | ||||
107 | Accepted | 795ms | 217020 KiB | ||||
108 | Accepted | 875ms | 217068 KiB | ||||
109 | Accepted | 229ms | 216988 KiB | ||||
110 | Accepted | 442ms | 217180 KiB | ||||
111 | Accepted | 505ms | 217200 KiB | ||||
112 | Accepted | 416ms | 217160 KiB | ||||
113 | Accepted | 418ms | 217036 KiB | ||||
114 | Accepted | 782ms | 171960 KiB | ||||
115 | Accepted | 493ms | 149276 KiB | ||||
116 | Accepted | 507ms | 149308 KiB | ||||
117 | Accepted | 569ms | 171640 KiB | ||||
118 | Accepted | 250ms | 171160 KiB |