10968 | 2024-04-22 17:41:54 | k_balint | Autó-tortúra | cpp17 | Wrong answer 24/100 | 921ms | 217256 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[i][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 | 2088 KiB | ||||
2 | Accepted | 449ms | 134380 KiB | ||||
subtask2 | 0/12 | ||||||
3 | Accepted | 3ms | 2428 KiB | ||||
4 | Accepted | 3ms | 2704 KiB | ||||
5 | Accepted | 3ms | 2984 KiB | ||||
6 | Accepted | 3ms | 3172 KiB | ||||
7 | Accepted | 3ms | 3440 KiB | ||||
8 | Wrong answer | 3ms | 3656 KiB | ||||
9 | Wrong answer | 3ms | 3868 KiB | ||||
10 | Accepted | 3ms | 3828 KiB | ||||
subtask3 | 0/18 | ||||||
11 | Accepted | 3ms | 2428 KiB | ||||
12 | Accepted | 3ms | 2704 KiB | ||||
13 | Accepted | 3ms | 2984 KiB | ||||
14 | Accepted | 3ms | 3172 KiB | ||||
15 | Accepted | 3ms | 3440 KiB | ||||
16 | Wrong answer | 3ms | 3656 KiB | ||||
17 | Wrong answer | 3ms | 3868 KiB | ||||
18 | Accepted | 3ms | 3828 KiB | ||||
19 | Wrong answer | 4ms | 6344 KiB | ||||
20 | Wrong answer | 20ms | 11416 KiB | ||||
21 | Accepted | 19ms | 11628 KiB | ||||
22 | Wrong answer | 18ms | 11668 KiB | ||||
23 | Wrong answer | 9ms | 11880 KiB | ||||
24 | Wrong answer | 8ms | 9372 KiB | ||||
25 | Wrong answer | 14ms | 11536 KiB | ||||
26 | Wrong answer | 13ms | 11840 KiB | ||||
27 | Wrong answer | 8ms | 11804 KiB | ||||
subtask4 | 0/25 | ||||||
28 | Accepted | 3ms | 2428 KiB | ||||
29 | Accepted | 3ms | 2704 KiB | ||||
30 | Accepted | 3ms | 2984 KiB | ||||
31 | Accepted | 3ms | 3172 KiB | ||||
32 | Accepted | 3ms | 3440 KiB | ||||
33 | Wrong answer | 3ms | 3656 KiB | ||||
34 | Wrong answer | 3ms | 3868 KiB | ||||
35 | Accepted | 3ms | 3828 KiB | ||||
36 | Accepted | 493ms | 146404 KiB | ||||
37 | Accepted | 428ms | 146452 KiB | ||||
38 | Accepted | 472ms | 146536 KiB | ||||
39 | Wrong answer | 465ms | 146536 KiB | ||||
40 | Accepted | 437ms | 146692 KiB | ||||
41 | Accepted | 391ms | 146688 KiB | ||||
42 | Wrong answer | 448ms | 146600 KiB | ||||
43 | Wrong answer | 305ms | 146440 KiB | ||||
44 | Wrong answer | 305ms | 146436 KiB | ||||
45 | Wrong answer | 146ms | 146428 KiB | ||||
subtask5 | 24/24 | ||||||
46 | Accepted | 486ms | 182164 KiB | ||||
47 | Accepted | 439ms | 182164 KiB | ||||
48 | Accepted | 483ms | 182172 KiB | ||||
49 | Accepted | 451ms | 182312 KiB | ||||
50 | Accepted | 296ms | 182412 KiB | ||||
51 | Accepted | 481ms | 175184 KiB | ||||
52 | Accepted | 432ms | 175096 KiB | ||||
53 | Accepted | 476ms | 175080 KiB | ||||
54 | Accepted | 444ms | 175076 KiB | ||||
55 | Accepted | 291ms | 175084 KiB | ||||
subtask6 | 0/21 | ||||||
56 | Accepted | 3ms | 2428 KiB | ||||
57 | Accepted | 3ms | 2704 KiB | ||||
58 | Accepted | 3ms | 2984 KiB | ||||
59 | Accepted | 3ms | 3172 KiB | ||||
60 | Accepted | 3ms | 3440 KiB | ||||
61 | Wrong answer | 3ms | 3656 KiB | ||||
62 | Wrong answer | 3ms | 3868 KiB | ||||
63 | Accepted | 3ms | 3828 KiB | ||||
64 | Wrong answer | 4ms | 6344 KiB | ||||
65 | Wrong answer | 20ms | 11416 KiB | ||||
66 | Accepted | 19ms | 11628 KiB | ||||
67 | Wrong answer | 18ms | 11668 KiB | ||||
68 | Wrong answer | 9ms | 11880 KiB | ||||
69 | Wrong answer | 8ms | 9372 KiB | ||||
70 | Wrong answer | 14ms | 11536 KiB | ||||
71 | Wrong answer | 13ms | 11840 KiB | ||||
72 | Wrong answer | 8ms | 11804 KiB | ||||
73 | Accepted | 493ms | 146404 KiB | ||||
74 | Accepted | 428ms | 146452 KiB | ||||
75 | Accepted | 472ms | 146536 KiB | ||||
76 | Wrong answer | 465ms | 146536 KiB | ||||
77 | Accepted | 437ms | 146692 KiB | ||||
78 | Accepted | 391ms | 146688 KiB | ||||
79 | Wrong answer | 448ms | 146600 KiB | ||||
80 | Wrong answer | 305ms | 146440 KiB | ||||
81 | Wrong answer | 305ms | 146436 KiB | ||||
82 | Wrong answer | 146ms | 146428 KiB | ||||
83 | Accepted | 486ms | 182164 KiB | ||||
84 | Accepted | 439ms | 182164 KiB | ||||
85 | Accepted | 483ms | 182172 KiB | ||||
86 | Accepted | 451ms | 182312 KiB | ||||
87 | Accepted | 296ms | 182412 KiB | ||||
88 | Accepted | 481ms | 175184 KiB | ||||
89 | Accepted | 432ms | 175096 KiB | ||||
90 | Accepted | 476ms | 175080 KiB | ||||
91 | Accepted | 444ms | 175076 KiB | ||||
92 | Accepted | 291ms | 175084 KiB | ||||
93 | Wrong answer | 397ms | 152440 KiB | ||||
94 | Wrong answer | 400ms | 151020 KiB | ||||
95 | Wrong answer | 495ms | 150960 KiB | ||||
96 | Wrong answer | 532ms | 150476 KiB | ||||
97 | Wrong answer | 178ms | 150456 KiB | ||||
98 | Wrong answer | 136ms | 48828 KiB | ||||
99 | Accepted | 500ms | 152448 KiB | ||||
100 | Accepted | 908ms | 205656 KiB | ||||
101 | Accepted | 913ms | 216836 KiB | ||||
102 | Accepted | 833ms | 172784 KiB | ||||
103 | Accepted | 773ms | 216856 KiB | ||||
104 | Accepted | 629ms | 216820 KiB | ||||
105 | Wrong answer | 777ms | 216788 KiB | ||||
106 | Wrong answer | 764ms | 216784 KiB | ||||
107 | Wrong answer | 746ms | 216976 KiB | ||||
108 | Wrong answer | 921ms | 217040 KiB | ||||
109 | Accepted | 226ms | 217144 KiB | ||||
110 | Accepted | 467ms | 217256 KiB | ||||
111 | Accepted | 493ms | 217240 KiB | ||||
112 | Accepted | 395ms | 217232 KiB | ||||
113 | Accepted | 416ms | 217000 KiB | ||||
114 | Wrong answer | 754ms | 171904 KiB | ||||
115 | Wrong answer | 486ms | 149200 KiB | ||||
116 | Wrong answer | 501ms | 149212 KiB | ||||
117 | Wrong answer | 541ms | 171624 KiB | ||||
118 | Wrong answer | 241ms | 171116 KiB |