10697 | 2024-04-09 22:39:44 | mraron | Autó-tortúra | cpp17 | Accepted 100/100 | 768ms | 76420 KiB |
// @check-accepted: examples very-small small-n small-l rest
#include<bits/stdc++.h>
using namespace std;
const int MAXN=3001;
const int MAXB=151;
#define LOG(x) cerr<<(#x)<<" = "<<x<<"\n";
#define chkmn(a,b) (a)=min((a), (b))
int n,m,k,l,b;
int path[MAXN], tank[MAXN];
vector<int> adj[MAXN];
int dist[MAXN][MAXN];
int tankDist[MAXB][MAXB];
void bfs(int ind) {
queue<int> q;
q.push(ind);
vector<int> vis(n+1);
vis[ind]=1;
for(int j=1;j<=n;++j) {
dist[ind][j]=1e9;
}
dist[ind][ind]=0;
while(!q.empty()) {
int i=q.front();q.pop();
for(auto j:adj[i]) {
if(!vis[j]) {
vis[j]=1;
q.push(j);
dist[ind][j]=dist[ind][i]+1;
}
}
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin>>n>>m>>k;
for(int i=0;i<m;++i) {
int a,b;
cin>>a>>b;
adj[a].push_back(b);
adj[b].push_back(a);
}
cin>>l;
for(int i=0;i<l;++i) {
cin>>path[i];
}
for(int i=1;i<=n;++i) {
bfs(i);
}
cin>>b;
for(int i=0;i<b;++i) {
cin>>tank[i];
}
for(int i=0;i<b;++i) {
tankDist[i][i]=0;
for(int j=0;j<b;++j) {
tankDist[i][j]=1e9;
if(dist[tank[i]][tank[j]]<=k) {
tankDist[i][j]=dist[tank[i]][tank[j]];
}
}
}
for(int j=0;j<b;++j) {
for(int i=0;i<b;++i) {
for(int k=0;k<b;++k) {
chkmn(tankDist[i][k], tankDist[i][j]+tankDist[j][k]);
}
}
}
vector<int> dp_prv, dp_nxt;
dp_prv.assign(k+1, 1e9);
dp_prv[k]=0;
for(int i=1;i<l;++i) {
dp_nxt.assign(k+1, 1e9);
int need=dist[path[i-1]][path[i]];
for(int j=need;j<=k;++j) {
chkmn(dp_nxt[j-need], dp_prv[j]+need);
}
for(int j=k-1;j>=0;j--) {
chkmn(dp_prv[j], dp_prv[j+1]);
}
for(int first_tank=0;first_tank<b;++first_tank) {
for(int last_tank=0;last_tank<b;++last_tank) {
if(tankDist[first_tank][last_tank]>=1e9) continue ;
int while_tank=tankDist[first_tank][last_tank];
int before_tank=dist[path[i-1]][tank[first_tank]];
int after_tank=dist[path[i]][tank[last_tank]];
int with_tank=k-after_tank;
if(with_tank>=0 && before_tank<=k) {
chkmn(dp_nxt[with_tank], dp_prv[before_tank]+before_tank+while_tank+after_tank);
}
}
}
dp_prv.swap(dp_nxt);
}
int ans=*min_element(begin(dp_prv), end(dp_prv));
if(ans>=1e9) {
ans=-1;
}
cout<<ans<<"\n";
return 0;
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 2080 KiB | ||||
2 | Accepted | 377ms | 54788 KiB | ||||
subtask2 | 12/12 | ||||||
3 | Accepted | 3ms | 2560 KiB | ||||
4 | Accepted | 3ms | 2776 KiB | ||||
5 | Accepted | 3ms | 2888 KiB | ||||
6 | Accepted | 3ms | 2964 KiB | ||||
7 | Accepted | 3ms | 3188 KiB | ||||
8 | Accepted | 3ms | 3268 KiB | ||||
9 | Accepted | 3ms | 3400 KiB | ||||
10 | Accepted | 3ms | 3396 KiB | ||||
subtask3 | 18/18 | ||||||
11 | Accepted | 3ms | 2560 KiB | ||||
12 | Accepted | 3ms | 2776 KiB | ||||
13 | Accepted | 3ms | 2888 KiB | ||||
14 | Accepted | 3ms | 2964 KiB | ||||
15 | Accepted | 3ms | 3188 KiB | ||||
16 | Accepted | 3ms | 3268 KiB | ||||
17 | Accepted | 3ms | 3400 KiB | ||||
18 | Accepted | 3ms | 3396 KiB | ||||
19 | Accepted | 4ms | 4468 KiB | ||||
20 | Accepted | 17ms | 6156 KiB | ||||
21 | Accepted | 16ms | 6260 KiB | ||||
22 | Accepted | 16ms | 6284 KiB | ||||
23 | Accepted | 8ms | 6112 KiB | ||||
24 | Accepted | 7ms | 6404 KiB | ||||
25 | Accepted | 13ms | 6448 KiB | ||||
26 | Accepted | 13ms | 6452 KiB | ||||
27 | Accepted | 8ms | 6436 KiB | ||||
subtask4 | 25/25 | ||||||
28 | Accepted | 3ms | 2560 KiB | ||||
29 | Accepted | 3ms | 2776 KiB | ||||
30 | Accepted | 3ms | 2888 KiB | ||||
31 | Accepted | 3ms | 2964 KiB | ||||
32 | Accepted | 3ms | 3188 KiB | ||||
33 | Accepted | 3ms | 3268 KiB | ||||
34 | Accepted | 3ms | 3400 KiB | ||||
35 | Accepted | 3ms | 3396 KiB | ||||
36 | Accepted | 449ms | 74712 KiB | ||||
37 | Accepted | 398ms | 74908 KiB | ||||
38 | Accepted | 442ms | 75040 KiB | ||||
39 | Accepted | 439ms | 74848 KiB | ||||
40 | Accepted | 398ms | 74776 KiB | ||||
41 | Accepted | 370ms | 74752 KiB | ||||
42 | Accepted | 418ms | 75104 KiB | ||||
43 | Accepted | 284ms | 74936 KiB | ||||
44 | Accepted | 282ms | 74968 KiB | ||||
45 | Accepted | 125ms | 74964 KiB | ||||
subtask5 | 24/24 | ||||||
46 | Accepted | 451ms | 74860 KiB | ||||
47 | Accepted | 400ms | 74924 KiB | ||||
48 | Accepted | 444ms | 74872 KiB | ||||
49 | Accepted | 418ms | 75200 KiB | ||||
50 | Accepted | 273ms | 75284 KiB | ||||
51 | Accepted | 446ms | 75184 KiB | ||||
52 | Accepted | 404ms | 75404 KiB | ||||
53 | Accepted | 448ms | 75616 KiB | ||||
54 | Accepted | 409ms | 75500 KiB | ||||
55 | Accepted | 268ms | 75424 KiB | ||||
subtask6 | 21/21 | ||||||
56 | Accepted | 3ms | 2560 KiB | ||||
57 | Accepted | 3ms | 2776 KiB | ||||
58 | Accepted | 3ms | 2888 KiB | ||||
59 | Accepted | 3ms | 2964 KiB | ||||
60 | Accepted | 3ms | 3188 KiB | ||||
61 | Accepted | 3ms | 3268 KiB | ||||
62 | Accepted | 3ms | 3400 KiB | ||||
63 | Accepted | 3ms | 3396 KiB | ||||
64 | Accepted | 4ms | 4468 KiB | ||||
65 | Accepted | 17ms | 6156 KiB | ||||
66 | Accepted | 16ms | 6260 KiB | ||||
67 | Accepted | 16ms | 6284 KiB | ||||
68 | Accepted | 8ms | 6112 KiB | ||||
69 | Accepted | 7ms | 6404 KiB | ||||
70 | Accepted | 13ms | 6448 KiB | ||||
71 | Accepted | 13ms | 6452 KiB | ||||
72 | Accepted | 8ms | 6436 KiB | ||||
73 | Accepted | 449ms | 74712 KiB | ||||
74 | Accepted | 398ms | 74908 KiB | ||||
75 | Accepted | 442ms | 75040 KiB | ||||
76 | Accepted | 439ms | 74848 KiB | ||||
77 | Accepted | 398ms | 74776 KiB | ||||
78 | Accepted | 370ms | 74752 KiB | ||||
79 | Accepted | 418ms | 75104 KiB | ||||
80 | Accepted | 284ms | 74936 KiB | ||||
81 | Accepted | 282ms | 74968 KiB | ||||
82 | Accepted | 125ms | 74964 KiB | ||||
83 | Accepted | 451ms | 74860 KiB | ||||
84 | Accepted | 400ms | 74924 KiB | ||||
85 | Accepted | 444ms | 74872 KiB | ||||
86 | Accepted | 418ms | 75200 KiB | ||||
87 | Accepted | 273ms | 75284 KiB | ||||
88 | Accepted | 446ms | 75184 KiB | ||||
89 | Accepted | 404ms | 75404 KiB | ||||
90 | Accepted | 448ms | 75616 KiB | ||||
91 | Accepted | 409ms | 75500 KiB | ||||
92 | Accepted | 268ms | 75424 KiB | ||||
93 | Accepted | 367ms | 75468 KiB | ||||
94 | Accepted | 358ms | 75724 KiB | ||||
95 | Accepted | 456ms | 75844 KiB | ||||
96 | Accepted | 486ms | 75968 KiB | ||||
97 | Accepted | 150ms | 75796 KiB | ||||
98 | Accepted | 119ms | 21108 KiB | ||||
99 | Accepted | 460ms | 75816 KiB | ||||
100 | Accepted | 732ms | 75960 KiB | ||||
101 | Accepted | 768ms | 76032 KiB | ||||
102 | Accepted | 709ms | 75964 KiB | ||||
103 | Accepted | 620ms | 76092 KiB | ||||
104 | Accepted | 492ms | 75968 KiB | ||||
105 | Accepted | 617ms | 75908 KiB | ||||
106 | Accepted | 606ms | 75912 KiB | ||||
107 | Accepted | 600ms | 75916 KiB | ||||
108 | Accepted | 726ms | 76072 KiB | ||||
109 | Accepted | 159ms | 75920 KiB | ||||
110 | Accepted | 400ms | 75856 KiB | ||||
111 | Accepted | 444ms | 75960 KiB | ||||
112 | Accepted | 349ms | 75972 KiB | ||||
113 | Accepted | 377ms | 75900 KiB | ||||
114 | Accepted | 670ms | 76064 KiB | ||||
115 | Accepted | 453ms | 76300 KiB | ||||
116 | Accepted | 462ms | 76420 KiB | ||||
117 | Accepted | 488ms | 76224 KiB | ||||
118 | Accepted | 279ms | 76248 KiB |