10814 | 2024-04-15 14:26:40 | Ablablabla | Autó-tortúra | cpp17 | Runtime error 24/100 | 711ms | 13136 KiB |
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e16;
int main(){
ll n, m, k;
cin >> n >> m >> k;
vector<vector<ll>> csucsok(n, vector<ll>());
for(ll i = 0; i < m; i++){
ll a, b;
cin >> a >> b;
a--; b--;
csucsok[a].push_back(b);
csucsok[b].push_back(a);
}
ll l;
cin >> l;
vector<ll> latogat(l);
for(ll &x : latogat){
cin >> x;
x--;
}
ll b;
cin >> b;
vector<ll> kutak(b);
for(ll &x : kutak){
cin >> x;
x--;
}
vector<vector<ll>> kutL(b, vector<ll>(l, INF));
vector<vector<ll>> kutKut(b, vector<ll>(b, INF));
for(ll i = 0; i < b; i++){
vector<ll> d(n, INF);
queue<ll> bejar;
bejar.push(kutak[i]);
d[kutak[i]] = 0;
while(!bejar.empty()){
ll akt = bejar.front();
bejar.pop();
for(ll x : csucsok[akt]){
if(d[akt] + 1 < d[x]){
d[x] = d[akt] + 1;
bejar.push(x);
}
}
}
for(ll j = 0; j < l; j++){
if(d[latogat[j]] <= k){
kutL[i][j] = d[latogat[j]];
}
}
for(ll j = 0; j < b; j++){
if(d[kutak[j]] <= k){
kutKut[i][j] = d[kutak[j]];
}
}
}
for(ll k = 0; k < b; k++){
for(ll i = 0; i < b; i++){
for(ll j = 0; j < b; j++){
kutKut[i][j] = min(kutKut[i][j], kutKut[i][k] + kutKut[k][j]);
}
}
}
vector<ll> ansA(k + 2);
ansA[k + 1] = INF;
for(ll i = 1; i < l; i++){
vector<ll> ansB(k + 2, INF);
queue<ll> bejar;
vector<ll> tavok(n, INF);
tavok[latogat[i - 1]] = 0;
bejar.push(latogat[i - 1]);
while(!bejar.empty()){
ll akt = bejar.front();
bejar.pop();
for(ll x : csucsok[akt]){
if(tavok[akt] + 1 < tavok[x]){
tavok[x] = tavok[akt] + 1;
bejar.push(x);
}
}
}
ll kovi = tavok[latogat[i]];
for(ll j = 0; j + kovi <= k; j++){
ansB[j] = min(ansB[j], ansA[j + kovi] + kovi);
}
for(ll j = 0; j < b; j++){
ll marad = k - kutL[j][i];
if(marad >= 0 && kutL[j][i - 1] <= k){
ansB[marad] = min(ansB[marad], ansA[kutL[j][i - 1]] + kutL[j][i - 1] + kutL[j][i]);
}
for(ll kk = 0; kk < b; kk++){
ll marad2 = k - kutL[kk][i];
if(marad2 >= 0 && kutL[kk][i - 1] <= k){
ansB[marad2] = min(ansB[marad2], ansA[kutL[j][i - 1]] + kutL[j][i - 1] + kutKut[j][kk] + kutL[kk][i]);
}
}
}
for(ll j = k - 1; j >= 0; j--){
ansB[j] = min(ansB[j], ansB[j + 1]);
}
ansA = ansB;
}
cout << (ansA[0] == INF ? -1 : ansA[0]) << "\n";
}
Subtask | Sum | Test | Verdict | Time | Memory | ||
---|---|---|---|---|---|---|---|
subtask1 | 0/0 | ||||||
1 | Accepted | 3ms | 1808 KiB | ||||
2 | Accepted | 342ms | 7120 KiB | ||||
subtask2 | 0/12 | ||||||
3 | Accepted | 3ms | 2360 KiB | ||||
4 | Accepted | 3ms | 2552 KiB | ||||
5 | Accepted | 3ms | 2640 KiB | ||||
6 | Runtime error | 3ms | 2940 KiB | ||||
7 | Runtime error | 3ms | 2960 KiB | ||||
8 | Runtime error | 3ms | 3236 KiB | ||||
9 | Runtime error | 3ms | 3180 KiB | ||||
10 | Runtime error | 3ms | 3372 KiB | ||||
subtask3 | 0/18 | ||||||
11 | Accepted | 3ms | 2360 KiB | ||||
12 | Accepted | 3ms | 2552 KiB | ||||
13 | Accepted | 3ms | 2640 KiB | ||||
14 | Runtime error | 3ms | 2940 KiB | ||||
15 | Runtime error | 3ms | 2960 KiB | ||||
16 | Runtime error | 3ms | 3236 KiB | ||||
17 | Runtime error | 3ms | 3180 KiB | ||||
18 | Runtime error | 3ms | 3372 KiB | ||||
19 | Accepted | 4ms | 3496 KiB | ||||
20 | Accepted | 14ms | 4032 KiB | ||||
21 | Accepted | 14ms | 4280 KiB | ||||
22 | Accepted | 14ms | 4344 KiB | ||||
23 | Accepted | 7ms | 3888 KiB | ||||
24 | Runtime error | 4ms | 4044 KiB | ||||
25 | Runtime error | 6ms | 4560 KiB | ||||
26 | Runtime error | 6ms | 4584 KiB | ||||
27 | Runtime error | 7ms | 4668 KiB | ||||
subtask4 | 0/25 | ||||||
28 | Accepted | 3ms | 2360 KiB | ||||
29 | Accepted | 3ms | 2552 KiB | ||||
30 | Accepted | 3ms | 2640 KiB | ||||
31 | Runtime error | 3ms | 2940 KiB | ||||
32 | Runtime error | 3ms | 2960 KiB | ||||
33 | Runtime error | 3ms | 3236 KiB | ||||
34 | Runtime error | 3ms | 3180 KiB | ||||
35 | Runtime error | 3ms | 3372 KiB | ||||
36 | Accepted | 39ms | 5072 KiB | ||||
37 | Accepted | 35ms | 5060 KiB | ||||
38 | Accepted | 37ms | 5188 KiB | ||||
39 | Accepted | 27ms | 4900 KiB | ||||
40 | Accepted | 14ms | 4848 KiB | ||||
41 | Accepted | 8ms | 4892 KiB | ||||
42 | Runtime error | 34ms | 5572 KiB | ||||
43 | Runtime error | 25ms | 5480 KiB | ||||
44 | Runtime error | 25ms | 5476 KiB | ||||
45 | Wrong answer | 14ms | 5320 KiB | ||||
subtask5 | 24/24 | ||||||
46 | Accepted | 435ms | 5264 KiB | ||||
47 | Accepted | 381ms | 5132 KiB | ||||
48 | Accepted | 425ms | 5168 KiB | ||||
49 | Accepted | 391ms | 5420 KiB | ||||
50 | Accepted | 246ms | 5356 KiB | ||||
51 | Accepted | 430ms | 5376 KiB | ||||
52 | Accepted | 375ms | 5372 KiB | ||||
53 | Accepted | 421ms | 5316 KiB | ||||
54 | Accepted | 388ms | 5316 KiB | ||||
55 | Accepted | 241ms | 5288 KiB | ||||
subtask6 | 0/21 | ||||||
56 | Accepted | 3ms | 2360 KiB | ||||
57 | Accepted | 3ms | 2552 KiB | ||||
58 | Accepted | 3ms | 2640 KiB | ||||
59 | Runtime error | 3ms | 2940 KiB | ||||
60 | Runtime error | 3ms | 2960 KiB | ||||
61 | Runtime error | 3ms | 3236 KiB | ||||
62 | Runtime error | 3ms | 3180 KiB | ||||
63 | Runtime error | 3ms | 3372 KiB | ||||
64 | Accepted | 4ms | 3496 KiB | ||||
65 | Accepted | 14ms | 4032 KiB | ||||
66 | Accepted | 14ms | 4280 KiB | ||||
67 | Accepted | 14ms | 4344 KiB | ||||
68 | Accepted | 7ms | 3888 KiB | ||||
69 | Runtime error | 4ms | 4044 KiB | ||||
70 | Runtime error | 6ms | 4560 KiB | ||||
71 | Runtime error | 6ms | 4584 KiB | ||||
72 | Runtime error | 7ms | 4668 KiB | ||||
73 | Accepted | 39ms | 5072 KiB | ||||
74 | Accepted | 35ms | 5060 KiB | ||||
75 | Accepted | 37ms | 5188 KiB | ||||
76 | Accepted | 27ms | 4900 KiB | ||||
77 | Accepted | 14ms | 4848 KiB | ||||
78 | Accepted | 8ms | 4892 KiB | ||||
79 | Runtime error | 34ms | 5572 KiB | ||||
80 | Runtime error | 25ms | 5480 KiB | ||||
81 | Runtime error | 25ms | 5476 KiB | ||||
82 | Wrong answer | 14ms | 5320 KiB | ||||
83 | Accepted | 435ms | 5264 KiB | ||||
84 | Accepted | 381ms | 5132 KiB | ||||
85 | Accepted | 425ms | 5168 KiB | ||||
86 | Accepted | 391ms | 5420 KiB | ||||
87 | Accepted | 246ms | 5356 KiB | ||||
88 | Accepted | 430ms | 5376 KiB | ||||
89 | Accepted | 375ms | 5372 KiB | ||||
90 | Accepted | 421ms | 5316 KiB | ||||
91 | Accepted | 388ms | 5316 KiB | ||||
92 | Accepted | 241ms | 5288 KiB | ||||
93 | Accepted | 116ms | 6736 KiB | ||||
94 | Runtime error | 28ms | 6788 KiB | ||||
95 | Runtime error | 34ms | 6916 KiB | ||||
96 | Runtime error | 35ms | 6932 KiB | ||||
97 | Wrong answer | 43ms | 6556 KiB | ||||
98 | Accepted | 104ms | 7588 KiB | ||||
99 | Accepted | 97ms | 6332 KiB | ||||
100 | Accepted | 680ms | 12720 KiB | ||||
101 | Accepted | 711ms | 12704 KiB | ||||
102 | Accepted | 658ms | 12640 KiB | ||||
103 | Accepted | 565ms | 12820 KiB | ||||
104 | Accepted | 430ms | 12832 KiB | ||||
105 | Accepted | 559ms | 12688 KiB | ||||
106 | Accepted | 554ms | 12836 KiB | ||||
107 | Accepted | 540ms | 12936 KiB | ||||
108 | Accepted | 666ms | 13020 KiB | ||||
109 | Accepted | 136ms | 5528 KiB | ||||
110 | Accepted | 370ms | 8368 KiB | ||||
111 | Accepted | 425ms | 5744 KiB | ||||
112 | Accepted | 340ms | 5988 KiB | ||||
113 | Accepted | 361ms | 5728 KiB | ||||
114 | Runtime error | 39ms | 13136 KiB | ||||
115 | Runtime error | 34ms | 6672 KiB | ||||
116 | Runtime error | 35ms | 6840 KiB | ||||
117 | Runtime error | 32ms | 12828 KiB | ||||
118 | Runtime error | 187ms | 12540 KiB |