#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int n, m, t, l;
cin >> n >> m >> t >> l;
vector<vector<pair<int, int>>> g(n);
for (int i = 0; i < m; i++) {
int x, y, z;
cin >> x >> y >> z;
--x; --y;
g[x].emplace_back(y, z);
g[y].emplace_back(x, z);
}
vector<int> seq(l);
for (int i = 0; i < l; i++) {
cin >> seq[i];
--seq[i];
}
{
int p, x;
cin >> p >> x;
--p; --x;
seq[p] = x;
}
vector<int> deg(n);
for (int i = 0; i < n; i++) {
deg[i] = (int) g[i].size();
}
vector<vector<int>> idx(n, vector<int>(n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < deg[i]; j++) {
idx[i][g[i][j].first] = j;
}
}
const long long inf = (long long) 1e18;
vector<vector<vector<vector<long long>>>> dist(n, vector<vector<vector<long long>>>(n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
dist[i][j].resize(deg[i]);
for (int k = 0; k < deg[i]; k++) {
dist[i][j][k] = vector<long long>(deg[j], inf);
}
}
}
vector<vector<long long>> d(n);
vector<int> cnt(n);
for (int ver = 0; ver < n; ver++) {
for (int e = 0; e < deg[ver]; e++) {
for (int i = 0; i < n; i++) {
d[i] = vector<long long>(deg[i], inf);
cnt[i] = 0;
}
int to = g[ver][e].first;
int w = g[ver][e].second;
d[to][idx[to][ver]] = w;
set<tuple<long long, int, int>> st;
st.emplace(d[to][idx[to][ver]], to, idx[to][ver]);
while (!st.empty()) {
auto it = st.begin();
int v = get<1>(*it);
int lst = get<2>(*it);
st.erase(it);
cnt[v] += 1;
if (cnt[v] >= 3) {
continue;
}
for (int i = 0; i < deg[v]; i++) {
if (i == lst) {
continue;
}
int to = g[v][i].first;
int w = g[v][i].second;
if (d[to][idx[to][v]] > d[v][lst] + w) {
if (d[to][idx[to][v]] != inf) {
st.erase(st.find({d[to][idx[to][v]], to, idx[to][v]}));
}
d[to][idx[to][v]] = d[v][lst] + w;
st.emplace(d[to][idx[to][v]], to, idx[to][v]);
}
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < deg[i]; j++) {
dist[ver][i][e][j] = d[i][j];
}
}
}
}
vector<vector<vector<vector<int>>>> good(n, vector<vector<vector<int>>>(n));
int cnt_edges = 0;
for (int from = 0; from < n; from++) {
for (int to = 0; to < n; to++) {
if (from == to) {
continue;
}
good[from][to].resize(deg[from]);
vector<int> order(deg[to]);
iota(order.begin(), order.end(), 0);
for (int i = 0; i < deg[from]; i++) {
sort(order.begin(), order.end(), [&](int a, int b) {
return dist[from][to][i][a] < dist[from][to][i][b];
});
for (int j = 0; j < min(deg[to], 2); j++) {
good[from][to][i].push_back(order[j]);
cnt_edges++;
}
}
}
}
assert(cnt_edges <= n * m * 6);
vector<long long> dp(n, inf);
for (int i = 0; i < deg[seq[0]]; i++) {
for (int j = 0; j < deg[seq[1]]; j++) {
dp[j] = min(dp[j], dist[seq[0]][seq[1]][i][j]);
}
}
vector<long long> new_dp(n);
vector<long long> val(n);
for (int p = 2; p < l; p++) {
for (int i = 0; i < deg[seq[p]]; i++) {
new_dp[i] = inf;
}
for (int i = 0; i < deg[seq[p - 1]]; i++) {
val[i] = inf;
}
for (int i = 0; i < deg[seq[p - 1]]; i++) {
for (int j = 0; j < deg[seq[p - 1]]; j++) {
if (i != j) {
val[i] = min(val[i], dp[j]);
}
}
}
for (int i = 0; i < deg[seq[p - 1]]; i++) {
for (int j : good[seq[p - 1]][seq[p]][i]) {
new_dp[j] = min(new_dp[j], val[i] + dist[seq[p - 1]][seq[p]][i][j]);
}
}
for (int i = 0; i < deg[seq[p]]; i++) {
dp[i] = new_dp[i];
}
}
long long res = inf;
for (int i = 0; i < deg[seq.back()]; i++) {
res = min(res, dp[i]);
}
cout << (res == inf ? -1 : res) << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
344 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
344 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
15 ms |
3928 KB |
Output is correct |
28 |
Correct |
17 ms |
4052 KB |
Output is correct |
29 |
Correct |
86 ms |
10092 KB |
Output is correct |
30 |
Correct |
141 ms |
9812 KB |
Output is correct |
31 |
Correct |
588 ms |
9740 KB |
Output is correct |
32 |
Correct |
578 ms |
9812 KB |
Output is correct |
33 |
Correct |
86 ms |
13648 KB |
Output is correct |
34 |
Correct |
88 ms |
13692 KB |
Output is correct |
35 |
Correct |
694 ms |
13396 KB |
Output is correct |
36 |
Correct |
364 ms |
13704 KB |
Output is correct |
37 |
Correct |
86 ms |
13652 KB |
Output is correct |
38 |
Correct |
93 ms |
18156 KB |
Output is correct |
39 |
Correct |
230 ms |
17912 KB |
Output is correct |
40 |
Correct |
96 ms |
17748 KB |
Output is correct |
41 |
Correct |
120 ms |
17784 KB |
Output is correct |
42 |
Correct |
244 ms |
20636 KB |
Output is correct |
43 |
Correct |
105 ms |
22608 KB |
Output is correct |
44 |
Correct |
98 ms |
22608 KB |
Output is correct |
45 |
Correct |
71 ms |
28252 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
344 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
15 ms |
3928 KB |
Output is correct |
28 |
Correct |
17 ms |
4052 KB |
Output is correct |
29 |
Correct |
86 ms |
10092 KB |
Output is correct |
30 |
Correct |
141 ms |
9812 KB |
Output is correct |
31 |
Correct |
588 ms |
9740 KB |
Output is correct |
32 |
Correct |
578 ms |
9812 KB |
Output is correct |
33 |
Correct |
86 ms |
13648 KB |
Output is correct |
34 |
Correct |
88 ms |
13692 KB |
Output is correct |
35 |
Correct |
694 ms |
13396 KB |
Output is correct |
36 |
Correct |
364 ms |
13704 KB |
Output is correct |
37 |
Correct |
86 ms |
13652 KB |
Output is correct |
38 |
Correct |
93 ms |
18156 KB |
Output is correct |
39 |
Correct |
230 ms |
17912 KB |
Output is correct |
40 |
Correct |
96 ms |
17748 KB |
Output is correct |
41 |
Correct |
120 ms |
17784 KB |
Output is correct |
42 |
Correct |
244 ms |
20636 KB |
Output is correct |
43 |
Correct |
105 ms |
22608 KB |
Output is correct |
44 |
Correct |
98 ms |
22608 KB |
Output is correct |
45 |
Correct |
71 ms |
28252 KB |
Output is correct |
46 |
Correct |
207 ms |
29240 KB |
Output is correct |
47 |
Correct |
12991 ms |
324564 KB |
Output is correct |
48 |
Correct |
13119 ms |
464052 KB |
Output is correct |
49 |
Correct |
8223 ms |
563068 KB |
Output is correct |
50 |
Correct |
11382 ms |
563272 KB |
Output is correct |
51 |
Correct |
16896 ms |
560980 KB |
Output is correct |
52 |
Correct |
3988 ms |
561452 KB |
Output is correct |
53 |
Correct |
3958 ms |
560044 KB |
Output is correct |
54 |
Correct |
3934 ms |
562176 KB |
Output is correct |
55 |
Correct |
3907 ms |
560896 KB |
Output is correct |
56 |
Correct |
4133 ms |
613724 KB |
Output is correct |
57 |
Correct |
4207 ms |
667740 KB |
Output is correct |
58 |
Correct |
4287 ms |
725652 KB |
Output is correct |
59 |
Correct |
4507 ms |
783136 KB |
Output is correct |
60 |
Correct |
4454 ms |
845468 KB |
Output is correct |
61 |
Correct |
4482 ms |
909596 KB |
Output is correct |
62 |
Correct |
4513 ms |
978580 KB |
Output is correct |
63 |
Runtime error |
4123 ms |
1048576 KB |
Execution killed with signal 9 |
64 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
348 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
344 KB |
Output is correct |
23 |
Correct |
0 ms |
344 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
26 |
Correct |
1 ms |
604 KB |
Output is correct |
27 |
Correct |
15 ms |
3928 KB |
Output is correct |
28 |
Correct |
17 ms |
4052 KB |
Output is correct |
29 |
Correct |
86 ms |
10092 KB |
Output is correct |
30 |
Correct |
141 ms |
9812 KB |
Output is correct |
31 |
Correct |
588 ms |
9740 KB |
Output is correct |
32 |
Correct |
578 ms |
9812 KB |
Output is correct |
33 |
Correct |
86 ms |
13648 KB |
Output is correct |
34 |
Correct |
88 ms |
13692 KB |
Output is correct |
35 |
Correct |
694 ms |
13396 KB |
Output is correct |
36 |
Correct |
364 ms |
13704 KB |
Output is correct |
37 |
Correct |
86 ms |
13652 KB |
Output is correct |
38 |
Correct |
93 ms |
18156 KB |
Output is correct |
39 |
Correct |
230 ms |
17912 KB |
Output is correct |
40 |
Correct |
96 ms |
17748 KB |
Output is correct |
41 |
Correct |
120 ms |
17784 KB |
Output is correct |
42 |
Correct |
244 ms |
20636 KB |
Output is correct |
43 |
Correct |
105 ms |
22608 KB |
Output is correct |
44 |
Correct |
98 ms |
22608 KB |
Output is correct |
45 |
Correct |
71 ms |
28252 KB |
Output is correct |
46 |
Correct |
207 ms |
29240 KB |
Output is correct |
47 |
Correct |
12991 ms |
324564 KB |
Output is correct |
48 |
Correct |
13119 ms |
464052 KB |
Output is correct |
49 |
Correct |
8223 ms |
563068 KB |
Output is correct |
50 |
Correct |
11382 ms |
563272 KB |
Output is correct |
51 |
Correct |
16896 ms |
560980 KB |
Output is correct |
52 |
Correct |
3988 ms |
561452 KB |
Output is correct |
53 |
Correct |
3958 ms |
560044 KB |
Output is correct |
54 |
Correct |
3934 ms |
562176 KB |
Output is correct |
55 |
Correct |
3907 ms |
560896 KB |
Output is correct |
56 |
Correct |
4133 ms |
613724 KB |
Output is correct |
57 |
Correct |
4207 ms |
667740 KB |
Output is correct |
58 |
Correct |
4287 ms |
725652 KB |
Output is correct |
59 |
Correct |
4507 ms |
783136 KB |
Output is correct |
60 |
Correct |
4454 ms |
845468 KB |
Output is correct |
61 |
Correct |
4482 ms |
909596 KB |
Output is correct |
62 |
Correct |
4513 ms |
978580 KB |
Output is correct |
63 |
Runtime error |
4123 ms |
1048576 KB |
Execution killed with signal 9 |
64 |
Halted |
0 ms |
0 KB |
- |