106972024-04-09 22:39:44mraronAutó-tortúracpp17Accepted 100/100768ms76420 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;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms2080 KiB
2Accepted377ms54788 KiB
subtask212/12
3Accepted3ms2560 KiB
4Accepted3ms2776 KiB
5Accepted3ms2888 KiB
6Accepted3ms2964 KiB
7Accepted3ms3188 KiB
8Accepted3ms3268 KiB
9Accepted3ms3400 KiB
10Accepted3ms3396 KiB
subtask318/18
11Accepted3ms2560 KiB
12Accepted3ms2776 KiB
13Accepted3ms2888 KiB
14Accepted3ms2964 KiB
15Accepted3ms3188 KiB
16Accepted3ms3268 KiB
17Accepted3ms3400 KiB
18Accepted3ms3396 KiB
19Accepted4ms4468 KiB
20Accepted17ms6156 KiB
21Accepted16ms6260 KiB
22Accepted16ms6284 KiB
23Accepted8ms6112 KiB
24Accepted7ms6404 KiB
25Accepted13ms6448 KiB
26Accepted13ms6452 KiB
27Accepted8ms6436 KiB
subtask425/25
28Accepted3ms2560 KiB
29Accepted3ms2776 KiB
30Accepted3ms2888 KiB
31Accepted3ms2964 KiB
32Accepted3ms3188 KiB
33Accepted3ms3268 KiB
34Accepted3ms3400 KiB
35Accepted3ms3396 KiB
36Accepted449ms74712 KiB
37Accepted398ms74908 KiB
38Accepted442ms75040 KiB
39Accepted439ms74848 KiB
40Accepted398ms74776 KiB
41Accepted370ms74752 KiB
42Accepted418ms75104 KiB
43Accepted284ms74936 KiB
44Accepted282ms74968 KiB
45Accepted125ms74964 KiB
subtask524/24
46Accepted451ms74860 KiB
47Accepted400ms74924 KiB
48Accepted444ms74872 KiB
49Accepted418ms75200 KiB
50Accepted273ms75284 KiB
51Accepted446ms75184 KiB
52Accepted404ms75404 KiB
53Accepted448ms75616 KiB
54Accepted409ms75500 KiB
55Accepted268ms75424 KiB
subtask621/21
56Accepted3ms2560 KiB
57Accepted3ms2776 KiB
58Accepted3ms2888 KiB
59Accepted3ms2964 KiB
60Accepted3ms3188 KiB
61Accepted3ms3268 KiB
62Accepted3ms3400 KiB
63Accepted3ms3396 KiB
64Accepted4ms4468 KiB
65Accepted17ms6156 KiB
66Accepted16ms6260 KiB
67Accepted16ms6284 KiB
68Accepted8ms6112 KiB
69Accepted7ms6404 KiB
70Accepted13ms6448 KiB
71Accepted13ms6452 KiB
72Accepted8ms6436 KiB
73Accepted449ms74712 KiB
74Accepted398ms74908 KiB
75Accepted442ms75040 KiB
76Accepted439ms74848 KiB
77Accepted398ms74776 KiB
78Accepted370ms74752 KiB
79Accepted418ms75104 KiB
80Accepted284ms74936 KiB
81Accepted282ms74968 KiB
82Accepted125ms74964 KiB
83Accepted451ms74860 KiB
84Accepted400ms74924 KiB
85Accepted444ms74872 KiB
86Accepted418ms75200 KiB
87Accepted273ms75284 KiB
88Accepted446ms75184 KiB
89Accepted404ms75404 KiB
90Accepted448ms75616 KiB
91Accepted409ms75500 KiB
92Accepted268ms75424 KiB
93Accepted367ms75468 KiB
94Accepted358ms75724 KiB
95Accepted456ms75844 KiB
96Accepted486ms75968 KiB
97Accepted150ms75796 KiB
98Accepted119ms21108 KiB
99Accepted460ms75816 KiB
100Accepted732ms75960 KiB
101Accepted768ms76032 KiB
102Accepted709ms75964 KiB
103Accepted620ms76092 KiB
104Accepted492ms75968 KiB
105Accepted617ms75908 KiB
106Accepted606ms75912 KiB
107Accepted600ms75916 KiB
108Accepted726ms76072 KiB
109Accepted159ms75920 KiB
110Accepted400ms75856 KiB
111Accepted444ms75960 KiB
112Accepted349ms75972 KiB
113Accepted377ms75900 KiB
114Accepted670ms76064 KiB
115Accepted453ms76300 KiB
116Accepted462ms76420 KiB
117Accepted488ms76224 KiB
118Accepted279ms76248 KiB