109682024-04-22 17:41:54k_balintAutó-tortúracpp17Wrong answer 24/100921ms217256 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;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted3ms2088 KiB
2Accepted449ms134380 KiB
subtask20/12
3Accepted3ms2428 KiB
4Accepted3ms2704 KiB
5Accepted3ms2984 KiB
6Accepted3ms3172 KiB
7Accepted3ms3440 KiB
8Wrong answer3ms3656 KiB
9Wrong answer3ms3868 KiB
10Accepted3ms3828 KiB
subtask30/18
11Accepted3ms2428 KiB
12Accepted3ms2704 KiB
13Accepted3ms2984 KiB
14Accepted3ms3172 KiB
15Accepted3ms3440 KiB
16Wrong answer3ms3656 KiB
17Wrong answer3ms3868 KiB
18Accepted3ms3828 KiB
19Wrong answer4ms6344 KiB
20Wrong answer20ms11416 KiB
21Accepted19ms11628 KiB
22Wrong answer18ms11668 KiB
23Wrong answer9ms11880 KiB
24Wrong answer8ms9372 KiB
25Wrong answer14ms11536 KiB
26Wrong answer13ms11840 KiB
27Wrong answer8ms11804 KiB
subtask40/25
28Accepted3ms2428 KiB
29Accepted3ms2704 KiB
30Accepted3ms2984 KiB
31Accepted3ms3172 KiB
32Accepted3ms3440 KiB
33Wrong answer3ms3656 KiB
34Wrong answer3ms3868 KiB
35Accepted3ms3828 KiB
36Accepted493ms146404 KiB
37Accepted428ms146452 KiB
38Accepted472ms146536 KiB
39Wrong answer465ms146536 KiB
40Accepted437ms146692 KiB
41Accepted391ms146688 KiB
42Wrong answer448ms146600 KiB
43Wrong answer305ms146440 KiB
44Wrong answer305ms146436 KiB
45Wrong answer146ms146428 KiB
subtask524/24
46Accepted486ms182164 KiB
47Accepted439ms182164 KiB
48Accepted483ms182172 KiB
49Accepted451ms182312 KiB
50Accepted296ms182412 KiB
51Accepted481ms175184 KiB
52Accepted432ms175096 KiB
53Accepted476ms175080 KiB
54Accepted444ms175076 KiB
55Accepted291ms175084 KiB
subtask60/21
56Accepted3ms2428 KiB
57Accepted3ms2704 KiB
58Accepted3ms2984 KiB
59Accepted3ms3172 KiB
60Accepted3ms3440 KiB
61Wrong answer3ms3656 KiB
62Wrong answer3ms3868 KiB
63Accepted3ms3828 KiB
64Wrong answer4ms6344 KiB
65Wrong answer20ms11416 KiB
66Accepted19ms11628 KiB
67Wrong answer18ms11668 KiB
68Wrong answer9ms11880 KiB
69Wrong answer8ms9372 KiB
70Wrong answer14ms11536 KiB
71Wrong answer13ms11840 KiB
72Wrong answer8ms11804 KiB
73Accepted493ms146404 KiB
74Accepted428ms146452 KiB
75Accepted472ms146536 KiB
76Wrong answer465ms146536 KiB
77Accepted437ms146692 KiB
78Accepted391ms146688 KiB
79Wrong answer448ms146600 KiB
80Wrong answer305ms146440 KiB
81Wrong answer305ms146436 KiB
82Wrong answer146ms146428 KiB
83Accepted486ms182164 KiB
84Accepted439ms182164 KiB
85Accepted483ms182172 KiB
86Accepted451ms182312 KiB
87Accepted296ms182412 KiB
88Accepted481ms175184 KiB
89Accepted432ms175096 KiB
90Accepted476ms175080 KiB
91Accepted444ms175076 KiB
92Accepted291ms175084 KiB
93Wrong answer397ms152440 KiB
94Wrong answer400ms151020 KiB
95Wrong answer495ms150960 KiB
96Wrong answer532ms150476 KiB
97Wrong answer178ms150456 KiB
98Wrong answer136ms48828 KiB
99Accepted500ms152448 KiB
100Accepted908ms205656 KiB
101Accepted913ms216836 KiB
102Accepted833ms172784 KiB
103Accepted773ms216856 KiB
104Accepted629ms216820 KiB
105Wrong answer777ms216788 KiB
106Wrong answer764ms216784 KiB
107Wrong answer746ms216976 KiB
108Wrong answer921ms217040 KiB
109Accepted226ms217144 KiB
110Accepted467ms217256 KiB
111Accepted493ms217240 KiB
112Accepted395ms217232 KiB
113Accepted416ms217000 KiB
114Wrong answer754ms171904 KiB
115Wrong answer486ms149200 KiB
116Wrong answer501ms149212 KiB
117Wrong answer541ms171624 KiB
118Wrong answer241ms171116 KiB