109692024-04-22 17:56:40k_balintAutó-tortúracpp17Accepted 100/100949ms217200 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;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms2084 KiB
2Accepted476ms134368 KiB
subtask212/12
3Accepted3ms2648 KiB
4Accepted3ms2896 KiB
5Accepted3ms3108 KiB
6Accepted3ms3536 KiB
7Accepted3ms3264 KiB
8Accepted3ms3452 KiB
9Accepted3ms3440 KiB
10Accepted3ms3588 KiB
subtask318/18
11Accepted3ms2648 KiB
12Accepted3ms2896 KiB
13Accepted3ms3108 KiB
14Accepted3ms3536 KiB
15Accepted3ms3264 KiB
16Accepted3ms3452 KiB
17Accepted3ms3440 KiB
18Accepted3ms3588 KiB
19Accepted4ms6096 KiB
20Accepted23ms11248 KiB
21Accepted20ms11332 KiB
22Accepted21ms11436 KiB
23Accepted9ms11528 KiB
24Accepted9ms9388 KiB
25Accepted17ms11332 KiB
26Accepted14ms11428 KiB
27Accepted9ms11408 KiB
subtask425/25
28Accepted3ms2648 KiB
29Accepted3ms2896 KiB
30Accepted3ms3108 KiB
31Accepted3ms3536 KiB
32Accepted3ms3264 KiB
33Accepted3ms3452 KiB
34Accepted3ms3440 KiB
35Accepted3ms3588 KiB
36Accepted495ms146008 KiB
37Accepted435ms146072 KiB
38Accepted476ms146072 KiB
39Accepted481ms146180 KiB
40Accepted421ms146172 KiB
41Accepted405ms146168 KiB
42Accepted453ms146184 KiB
43Accepted314ms146156 KiB
44Accepted328ms146188 KiB
45Accepted155ms146188 KiB
subtask524/24
46Accepted499ms181828 KiB
47Accepted437ms181880 KiB
48Accepted495ms181932 KiB
49Accepted446ms181828 KiB
50Accepted319ms182092 KiB
51Accepted493ms175080 KiB
52Accepted430ms175268 KiB
53Accepted490ms175136 KiB
54Accepted439ms175084 KiB
55Accepted293ms175092 KiB
subtask621/21
56Accepted3ms2648 KiB
57Accepted3ms2896 KiB
58Accepted3ms3108 KiB
59Accepted3ms3536 KiB
60Accepted3ms3264 KiB
61Accepted3ms3452 KiB
62Accepted3ms3440 KiB
63Accepted3ms3588 KiB
64Accepted4ms6096 KiB
65Accepted23ms11248 KiB
66Accepted20ms11332 KiB
67Accepted21ms11436 KiB
68Accepted9ms11528 KiB
69Accepted9ms9388 KiB
70Accepted17ms11332 KiB
71Accepted14ms11428 KiB
72Accepted9ms11408 KiB
73Accepted495ms146008 KiB
74Accepted435ms146072 KiB
75Accepted476ms146072 KiB
76Accepted481ms146180 KiB
77Accepted421ms146172 KiB
78Accepted405ms146168 KiB
79Accepted453ms146184 KiB
80Accepted314ms146156 KiB
81Accepted328ms146188 KiB
82Accepted155ms146188 KiB
83Accepted499ms181828 KiB
84Accepted437ms181880 KiB
85Accepted495ms181932 KiB
86Accepted446ms181828 KiB
87Accepted319ms182092 KiB
88Accepted493ms175080 KiB
89Accepted430ms175268 KiB
90Accepted490ms175136 KiB
91Accepted439ms175084 KiB
92Accepted293ms175092 KiB
93Accepted421ms152464 KiB
94Accepted389ms151036 KiB
95Accepted497ms151108 KiB
96Accepted554ms150748 KiB
97Accepted168ms150796 KiB
98Accepted149ms49172 KiB
99Accepted518ms152752 KiB
100Accepted904ms205860 KiB
101Accepted949ms217040 KiB
102Accepted843ms172972 KiB
103Accepted815ms217004 KiB
104Accepted643ms217016 KiB
105Accepted802ms217020 KiB
106Accepted788ms217056 KiB
107Accepted795ms217020 KiB
108Accepted875ms217068 KiB
109Accepted229ms216988 KiB
110Accepted442ms217180 KiB
111Accepted505ms217200 KiB
112Accepted416ms217160 KiB
113Accepted418ms217036 KiB
114Accepted782ms171960 KiB
115Accepted493ms149276 KiB
116Accepted507ms149308 KiB
117Accepted569ms171640 KiB
118Accepted250ms171160 KiB