160602025-03-30 10:54:00szilAutó-tortúracpp17Time limit exceeded 0/1001.605s71184 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);
    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.593s48296 KiB
subtask20/12
3Wrong answer1ms316 KiB
4Accepted1ms512 KiB
5Accepted1ms328 KiB
6Accepted1ms316 KiB
7Accepted1ms316 KiB
8Accepted1ms316 KiB
9Runtime error1ms564 KiB
10Wrong answer1ms316 KiB
subtask30/18
11Wrong answer1ms316 KiB
12Accepted1ms512 KiB
13Accepted1ms328 KiB
14Accepted1ms316 KiB
15Accepted1ms316 KiB
16Accepted1ms316 KiB
17Runtime error1ms564 KiB
18Wrong answer1ms316 KiB
19Runtime error3ms1076 KiB
20Wrong answer32ms2292 KiB
21Accepted32ms2100 KiB
22Wrong answer32ms2100 KiB
23Runtime error21ms2292 KiB
24Wrong answer24ms2104 KiB
25Wrong answer35ms2100 KiB
26Wrong answer32ms2204 KiB
27Wrong answer27ms2100 KiB
subtask40/25
28Wrong answer1ms316 KiB
29Accepted1ms512 KiB
30Accepted1ms328 KiB
31Accepted1ms316 KiB
32Accepted1ms316 KiB
33Accepted1ms316 KiB
34Runtime error1ms564 KiB
35Wrong answer1ms316 KiB
36Time limit exceeded1.593s70964 KiB
37Time limit exceeded1.593s70972 KiB
38Time limit exceeded1.593s70900 KiB
39Time limit exceeded1.593s70964 KiB
40Time limit exceeded1.59s70964 KiB
41Time limit exceeded1.59s70972 KiB
42Time limit exceeded1.59s70928 KiB
43Time limit exceeded1.592s71080 KiB
44Time limit exceeded1.601s71112 KiB
45Time limit exceeded1.6s71080 KiB
subtask50/24
46Time limit exceeded1.588s71184 KiB
47Time limit exceeded1.59s70976 KiB
48Time limit exceeded1.587s71132 KiB
49Time limit exceeded1.59s71148 KiB
50Time limit exceeded1.582s71104 KiB
51Time limit exceeded1.583s70964 KiB
52Time limit exceeded1.585s70964 KiB
53Time limit exceeded1.582s71152 KiB
54Time limit exceeded1.593s70964 KiB
55Time limit exceeded1.595s70964 KiB
subtask60/21
56Wrong answer1ms316 KiB
57Accepted1ms512 KiB
58Accepted1ms328 KiB
59Accepted1ms316 KiB
60Accepted1ms316 KiB
61Accepted1ms316 KiB
62Runtime error1ms564 KiB
63Wrong answer1ms316 KiB
64Runtime error3ms1076 KiB
65Wrong answer32ms2292 KiB
66Accepted32ms2100 KiB
67Wrong answer32ms2100 KiB
68Runtime error21ms2292 KiB
69Wrong answer24ms2104 KiB
70Wrong answer35ms2100 KiB
71Wrong answer32ms2204 KiB
72Wrong answer27ms2100 KiB
73Time limit exceeded1.593s70964 KiB
74Time limit exceeded1.593s70972 KiB
75Time limit exceeded1.593s70900 KiB
76Time limit exceeded1.593s70964 KiB
77Time limit exceeded1.59s70964 KiB
78Time limit exceeded1.59s70972 KiB
79Time limit exceeded1.59s70928 KiB
80Time limit exceeded1.592s71080 KiB
81Time limit exceeded1.601s71112 KiB
82Time limit exceeded1.6s71080 KiB
83Time limit exceeded1.588s71184 KiB
84Time limit exceeded1.59s70976 KiB
85Time limit exceeded1.587s71132 KiB
86Time limit exceeded1.59s71148 KiB
87Time limit exceeded1.582s71104 KiB
88Time limit exceeded1.583s70964 KiB
89Time limit exceeded1.585s70964 KiB
90Time limit exceeded1.582s71152 KiB
91Time limit exceeded1.593s70964 KiB
92Time limit exceeded1.595s70964 KiB
93Time limit exceeded1.582s70964 KiB
94Time limit exceeded1.58s70964 KiB
95Time limit exceeded1.582s70964 KiB
96Time limit exceeded1.583s70964 KiB
97Time limit exceeded1.595s70964 KiB
98Time limit exceeded1.504s12976 KiB
99Time limit exceeded1.598s70964 KiB
100Time limit exceeded1.598s70968 KiB
101Time limit exceeded1.593s70964 KiB
102Time limit exceeded1.605s71104 KiB
103Time limit exceeded1.605s70964 KiB
104Time limit exceeded1.605s70960 KiB
105Time limit exceeded1.597s71104 KiB
106Time limit exceeded1.605s71004 KiB
107Time limit exceeded1.605s70964 KiB
108Time limit exceeded1.605s71016 KiB
109Time limit exceeded1.585s70964 KiB
110Time limit exceeded1.605s70964 KiB
111Time limit exceeded1.605s71144 KiB
112Time limit exceeded1.605s71020 KiB
113Time limit exceeded1.59s70904 KiB
114Time limit exceeded1.605s70964 KiB
115Time limit exceeded1.605s71140 KiB
116Time limit exceeded1.605s71064 KiB
117Time limit exceeded1.588s70964 KiB
118Time limit exceeded1.603s70916 KiB