160612025-03-30 10:55:50szilAutó-tortúracpp17Time limit exceeded 0/1001.606s71160 KiB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int MAXN = 3001;
const int MAXB = 151;
const int INF = 1e9;

ll dist[MAXN][MAXN], bdist[MAXB][MAXB];

void solve() {
    int n, m, k; cin >> n >> m >> k;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            bdist[i][j] = dist[i][j] = INF;

        }
        bdist[i][i] = dist[i][i] = 0;
    }
    for (int i = 0; i < m; i++) {
        int a, b; cin >> a >> b;
        dist[a][b] = dist[b][a] = 1;
    }
    for (int c = 1; c <= n; c++) {
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= n; j++) {
                dist[i][j] = min(dist[i][j], dist[i][c] + dist[c][j]);
            }
        }
    }
    int l; cin >> l;
    vector<int> terv(l+1);
    for (int i = 1; i <= l; i++) cin >> terv[i];
    int b; cin >> b;
    vector<int> ben(b+1);
    for (int i = 1; i <= b; i++) cin >> ben[i];

    for (int i = 1; i <= b; i++) {
        for (int j = 1; j <= b; j++) {
            int u = ben[i];
            int v = ben[j];

            if (dist[u][v] <= k) {
                bdist[i][j] = min(bdist[i][j], dist[u][v]);
            }
        }
    }

    for (int c = 1; c <= b; c++) {
        for (int i = 1; i <= b; i++) {
            for (int j = 1; j <= b; j++) {
                bdist[i][j] = min(bdist[i][j], bdist[i][c] + bdist[c][j]);
            }
        }
    }

    vector<ll> dp_last(k+1);
    for (int a = 2; a <= l; a++) {
        vector<ll> dp_curr(k+1, INF);

        int start = terv[a-1];
        int goal = terv[a];

        for (int i = 1; i <= b; i++) {
            for (int j = 1; j <= b; j++) {
                ll d = dist[start][ben[i]] + bdist[i][j] + dist[ben[j]][goal];
                ll need = dist[start][ben[i]];
                ll left = k - dist[ben[j]][goal];

                
                if (need > k) continue;
                if (left < 0) continue;

                d += dp_last[need];
                dp_curr[left] = min(dp_curr[left], d);
            }
        }

        if (dist[start][goal] <= k) {
            for (int i = 0; i <= k - dist[start][goal]; i++) {
                dp_curr[i] = min(dp_curr[i], dist[start][goal] + dp_last[i + dist[start][goal]]);
            }
        }

        for (int i = k-1; i >= 0; i--) {
            dp_curr[i] = min(dp_curr[i], dp_curr[i+1]);
        }

        dp_last = dp_curr;
    }

    cout << (*min_element(dp_last.begin(), dp_last.end())) << "\n";
}

int main() {
    ios::sync_with_stdio(0); cin.tie(0);
    int t = 1; 
    // cin >> t;
    while (t--) solve();
    return 0;
}
SubtaskSumTestVerdictTimeMemory
subtask10/0
1Accepted1ms316 KiB
2Time limit exceeded1.588s48276 KiB
subtask20/12
3Wrong answer1ms316 KiB
4Accepted1ms316 KiB
5Accepted1ms316 KiB
6Accepted1ms316 KiB
7Accepted1ms524 KiB
8Accepted1ms316 KiB
9Accepted1ms316 KiB
10Wrong answer2ms316 KiB
subtask30/18
11Wrong answer1ms316 KiB
12Accepted1ms316 KiB
13Accepted1ms316 KiB
14Accepted1ms316 KiB
15Accepted1ms524 KiB
16Accepted1ms316 KiB
17Accepted1ms316 KiB
18Wrong answer2ms316 KiB
19Accepted4ms820 KiB
20Wrong answer32ms2200 KiB
21Accepted32ms2100 KiB
22Wrong answer32ms2088 KiB
23Wrong answer28ms2200 KiB
24Wrong answer24ms2100 KiB
25Wrong answer35ms2200 KiB
26Wrong answer32ms2100 KiB
27Wrong answer26ms2100 KiB
subtask40/25
28Wrong answer1ms316 KiB
29Accepted1ms316 KiB
30Accepted1ms316 KiB
31Accepted1ms316 KiB
32Accepted1ms524 KiB
33Accepted1ms316 KiB
34Accepted1ms316 KiB
35Wrong answer2ms316 KiB
36Time limit exceeded1.59s71160 KiB
37Time limit exceeded1.59s70964 KiB
38Time limit exceeded1.59s70964 KiB
39Time limit exceeded1.59s70972 KiB
40Time limit exceeded1.58s70984 KiB
41Time limit exceeded1.582s70968 KiB
42Time limit exceeded1.582s70936 KiB
43Time limit exceeded1.582s71148 KiB
44Time limit exceeded1.585s70888 KiB
45Time limit exceeded1.585s70964 KiB
subtask50/24
46Time limit exceeded1.588s70964 KiB
47Time limit exceeded1.59s70964 KiB
48Time limit exceeded1.59s70964 KiB
49Time limit exceeded1.588s70976 KiB
50Time limit exceeded1.595s70984 KiB
51Time limit exceeded1.595s70988 KiB
52Time limit exceeded1.595s71016 KiB
53Time limit exceeded1.595s70964 KiB
54Time limit exceeded1.59s70964 KiB
55Time limit exceeded1.59s70996 KiB
subtask60/21
56Wrong answer1ms316 KiB
57Accepted1ms316 KiB
58Accepted1ms316 KiB
59Accepted1ms316 KiB
60Accepted1ms524 KiB
61Accepted1ms316 KiB
62Accepted1ms316 KiB
63Wrong answer2ms316 KiB
64Accepted4ms820 KiB
65Wrong answer32ms2200 KiB
66Accepted32ms2100 KiB
67Wrong answer32ms2088 KiB
68Wrong answer28ms2200 KiB
69Wrong answer24ms2100 KiB
70Wrong answer35ms2200 KiB
71Wrong answer32ms2100 KiB
72Wrong answer26ms2100 KiB
73Time limit exceeded1.59s71160 KiB
74Time limit exceeded1.59s70964 KiB
75Time limit exceeded1.59s70964 KiB
76Time limit exceeded1.59s70972 KiB
77Time limit exceeded1.58s70984 KiB
78Time limit exceeded1.582s70968 KiB
79Time limit exceeded1.582s70936 KiB
80Time limit exceeded1.582s71148 KiB
81Time limit exceeded1.585s70888 KiB
82Time limit exceeded1.585s70964 KiB
83Time limit exceeded1.588s70964 KiB
84Time limit exceeded1.59s70964 KiB
85Time limit exceeded1.59s70964 KiB
86Time limit exceeded1.588s70976 KiB
87Time limit exceeded1.595s70984 KiB
88Time limit exceeded1.595s70988 KiB
89Time limit exceeded1.595s71016 KiB
90Time limit exceeded1.595s70964 KiB
91Time limit exceeded1.59s70964 KiB
92Time limit exceeded1.59s70996 KiB
93Time limit exceeded1.588s70908 KiB
94Time limit exceeded1.59s70964 KiB
95Time limit exceeded1.588s71076 KiB
96Time limit exceeded1.588s70988 KiB
97Time limit exceeded1.592s70956 KiB
98Time limit exceeded1.588s12856 KiB
99Time limit exceeded1.593s70964 KiB
100Time limit exceeded1.593s70924 KiB
101Time limit exceeded1.588s70900 KiB
102Time limit exceeded1.605s70872 KiB
103Time limit exceeded1.605s70980 KiB
104Time limit exceeded1.605s70964 KiB
105Time limit exceeded1.597s71000 KiB
106Time limit exceeded1.605s70964 KiB
107Time limit exceeded1.605s71044 KiB
108Time limit exceeded1.605s70964 KiB
109Time limit exceeded1.59s70964 KiB
110Time limit exceeded1.605s71012 KiB
111Time limit exceeded1.605s70860 KiB
112Time limit exceeded1.605s70908 KiB
113Time limit exceeded1.578s70960 KiB
114Time limit exceeded1.606s70980 KiB
115Time limit exceeded1.605s70892 KiB
116Time limit exceeded1.605s71076 KiB
117Time limit exceeded1.588s71080 KiB
118Time limit exceeded1.582s71016 KiB