160622025-03-30 11:00:30szilAutó-tortúracpp17Time limit exceeded 0/1001.605s70900 KiB
#include <bits/stdc++.h>

using namespace std;
using ll = long long;

const int MAXN = 3001;
const int MAXB = 151;
const ll INF = 1e14;

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++) {
            dist[i][j] = INF;
        }
        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++) {
            bdist[i][j] = INF;
        }
        bdist[i][i] = 0;
    }

    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.59s47928 KiB
subtask20/12
3Wrong answer1ms316 KiB
4Accepted1ms316 KiB
5Accepted1ms316 KiB
6Accepted1ms316 KiB
7Accepted1ms316 KiB
8Accepted1ms316 KiB
9Accepted1ms316 KiB
10Wrong answer1ms316 KiB
subtask30/18
11Wrong answer1ms316 KiB
12Accepted1ms316 KiB
13Accepted1ms316 KiB
14Accepted1ms316 KiB
15Accepted1ms316 KiB
16Accepted1ms316 KiB
17Accepted1ms316 KiB
18Wrong answer1ms316 KiB
19Accepted4ms824 KiB
20Accepted34ms1928 KiB
21Accepted34ms2036 KiB
22Accepted34ms1932 KiB
23Accepted25ms1932 KiB
24Accepted24ms1936 KiB
25Accepted37ms1844 KiB
26Accepted34ms1928 KiB
27Accepted27ms1928 KiB
subtask40/25
28Wrong answer1ms316 KiB
29Accepted1ms316 KiB
30Accepted1ms316 KiB
31Accepted1ms316 KiB
32Accepted1ms316 KiB
33Accepted1ms316 KiB
34Accepted1ms316 KiB
35Wrong answer1ms316 KiB
36Time limit exceeded1.587s70704 KiB
37Time limit exceeded1.587s70900 KiB
38Time limit exceeded1.587s70752 KiB
39Time limit exceeded1.587s70900 KiB
40Time limit exceeded1.582s70708 KiB
41Time limit exceeded1.582s70712 KiB
42Time limit exceeded1.58s70828 KiB
43Time limit exceeded1.582s70856 KiB
44Time limit exceeded1.587s70708 KiB
45Time limit exceeded1.587s70736 KiB
subtask50/24
46Time limit exceeded1.578s70784 KiB
47Time limit exceeded1.577s70860 KiB
48Time limit exceeded1.577s70708 KiB
49Time limit exceeded1.578s70708 KiB
50Time limit exceeded1.593s70708 KiB
51Time limit exceeded1.593s70872 KiB
52Time limit exceeded1.593s70852 KiB
53Time limit exceeded1.595s70708 KiB
54Time limit exceeded1.595s70708 KiB
55Time limit exceeded1.595s70708 KiB
subtask60/21
56Wrong answer1ms316 KiB
57Accepted1ms316 KiB
58Accepted1ms316 KiB
59Accepted1ms316 KiB
60Accepted1ms316 KiB
61Accepted1ms316 KiB
62Accepted1ms316 KiB
63Wrong answer1ms316 KiB
64Accepted4ms824 KiB
65Accepted34ms1928 KiB
66Accepted34ms2036 KiB
67Accepted34ms1932 KiB
68Accepted25ms1932 KiB
69Accepted24ms1936 KiB
70Accepted37ms1844 KiB
71Accepted34ms1928 KiB
72Accepted27ms1928 KiB
73Time limit exceeded1.587s70704 KiB
74Time limit exceeded1.587s70900 KiB
75Time limit exceeded1.587s70752 KiB
76Time limit exceeded1.587s70900 KiB
77Time limit exceeded1.582s70708 KiB
78Time limit exceeded1.582s70712 KiB
79Time limit exceeded1.58s70828 KiB
80Time limit exceeded1.582s70856 KiB
81Time limit exceeded1.587s70708 KiB
82Time limit exceeded1.587s70736 KiB
83Time limit exceeded1.578s70784 KiB
84Time limit exceeded1.577s70860 KiB
85Time limit exceeded1.577s70708 KiB
86Time limit exceeded1.578s70708 KiB
87Time limit exceeded1.593s70708 KiB
88Time limit exceeded1.593s70872 KiB
89Time limit exceeded1.593s70852 KiB
90Time limit exceeded1.595s70708 KiB
91Time limit exceeded1.595s70708 KiB
92Time limit exceeded1.595s70708 KiB
93Time limit exceeded1.58s70888 KiB
94Time limit exceeded1.58s70708 KiB
95Time limit exceeded1.58s70708 KiB
96Time limit exceeded1.582s70856 KiB
97Time limit exceeded1.588s70720 KiB
98Time limit exceeded1.583s12340 KiB
99Time limit exceeded1.59s70648 KiB
100Time limit exceeded1.588s70708 KiB
101Time limit exceeded1.588s70724 KiB
102Time limit exceeded1.605s70704 KiB
103Time limit exceeded1.605s70784 KiB
104Time limit exceeded1.605s70884 KiB
105Time limit exceeded1.588s70668 KiB
106Time limit exceeded1.605s70836 KiB
107Time limit exceeded1.605s70868 KiB
108Time limit exceeded1.605s70708 KiB
109Time limit exceeded1.588s70796 KiB
110Time limit exceeded1.605s70700 KiB
111Time limit exceeded1.605s70840 KiB
112Time limit exceeded1.605s70892 KiB
113Time limit exceeded1.593s70760 KiB
114Time limit exceeded1.605s70888 KiB
115Time limit exceeded1.605s70824 KiB
116Time limit exceeded1.605s70824 KiB
117Time limit exceeded1.585s70828 KiB
118Time limit exceeded1.605s70844 KiB