#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);
}
}
}
for (int ver = 0; ver < n; ver++) {
for (int e = 0; e < deg[ver]; e++) {
vector<vector<long long>> d(n);
for (int i = 0; i < n; i++) {
d[i] = vector<long long>(deg[i], inf);
}
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);
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<long long> dp(deg[seq[1]], 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]);
}
}
for (int p = 2; p < l; p++) {
vector<long long> new_dp(deg[seq[p]], inf);
vector<long long> val(deg[seq[p - 1]], inf);
for (int i = 0; i < (int) val.size(); i++) {
for (int j = 0; j < (int) val.size(); j++) {
if (i != j) {
val[i] = min(val[i], dp[j]);
}
}
}
for (int i = 0; i < deg[seq[p - 1]]; i++) {
for (int j = 0; j < deg[seq[p]]; j++) {
new_dp[j] = min(new_dp[j], val[i] + dist[seq[p - 1]][seq[p]][i][j]);
}
}
swap(dp, new_dp);
}
long long res = *min_element(dp.begin(), dp.end());
cout << (res == inf ? -1 : res) << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
456 KB |
Output is correct |
16 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
456 KB |
Output is correct |
16 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 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 |
348 KB |
Output is correct |
27 |
Correct |
15 ms |
2652 KB |
Output is correct |
28 |
Correct |
15 ms |
2652 KB |
Output is correct |
29 |
Correct |
89 ms |
6492 KB |
Output is correct |
30 |
Correct |
191 ms |
6236 KB |
Output is correct |
31 |
Correct |
1050 ms |
6236 KB |
Output is correct |
32 |
Correct |
1002 ms |
6236 KB |
Output is correct |
33 |
Correct |
83 ms |
8028 KB |
Output is correct |
34 |
Correct |
84 ms |
8028 KB |
Output is correct |
35 |
Correct |
1239 ms |
7904 KB |
Output is correct |
36 |
Correct |
607 ms |
8028 KB |
Output is correct |
37 |
Correct |
83 ms |
8028 KB |
Output is correct |
38 |
Correct |
82 ms |
9924 KB |
Output is correct |
39 |
Correct |
317 ms |
9820 KB |
Output is correct |
40 |
Correct |
87 ms |
10076 KB |
Output is correct |
41 |
Correct |
101 ms |
9820 KB |
Output is correct |
42 |
Correct |
354 ms |
11100 KB |
Output is correct |
43 |
Correct |
92 ms |
11976 KB |
Output is correct |
44 |
Correct |
84 ms |
12124 KB |
Output is correct |
45 |
Correct |
42 ms |
14940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
456 KB |
Output is correct |
16 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 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 |
348 KB |
Output is correct |
27 |
Correct |
15 ms |
2652 KB |
Output is correct |
28 |
Correct |
15 ms |
2652 KB |
Output is correct |
29 |
Correct |
89 ms |
6492 KB |
Output is correct |
30 |
Correct |
191 ms |
6236 KB |
Output is correct |
31 |
Correct |
1050 ms |
6236 KB |
Output is correct |
32 |
Correct |
1002 ms |
6236 KB |
Output is correct |
33 |
Correct |
83 ms |
8028 KB |
Output is correct |
34 |
Correct |
84 ms |
8028 KB |
Output is correct |
35 |
Correct |
1239 ms |
7904 KB |
Output is correct |
36 |
Correct |
607 ms |
8028 KB |
Output is correct |
37 |
Correct |
83 ms |
8028 KB |
Output is correct |
38 |
Correct |
82 ms |
9924 KB |
Output is correct |
39 |
Correct |
317 ms |
9820 KB |
Output is correct |
40 |
Correct |
87 ms |
10076 KB |
Output is correct |
41 |
Correct |
101 ms |
9820 KB |
Output is correct |
42 |
Correct |
354 ms |
11100 KB |
Output is correct |
43 |
Correct |
92 ms |
11976 KB |
Output is correct |
44 |
Correct |
84 ms |
12124 KB |
Output is correct |
45 |
Correct |
42 ms |
14940 KB |
Output is correct |
46 |
Correct |
209 ms |
17048 KB |
Output is correct |
47 |
Execution timed out |
18075 ms |
206392 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
456 KB |
Output is correct |
16 |
Correct |
1 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 |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
344 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
1 ms |
348 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 |
348 KB |
Output is correct |
27 |
Correct |
15 ms |
2652 KB |
Output is correct |
28 |
Correct |
15 ms |
2652 KB |
Output is correct |
29 |
Correct |
89 ms |
6492 KB |
Output is correct |
30 |
Correct |
191 ms |
6236 KB |
Output is correct |
31 |
Correct |
1050 ms |
6236 KB |
Output is correct |
32 |
Correct |
1002 ms |
6236 KB |
Output is correct |
33 |
Correct |
83 ms |
8028 KB |
Output is correct |
34 |
Correct |
84 ms |
8028 KB |
Output is correct |
35 |
Correct |
1239 ms |
7904 KB |
Output is correct |
36 |
Correct |
607 ms |
8028 KB |
Output is correct |
37 |
Correct |
83 ms |
8028 KB |
Output is correct |
38 |
Correct |
82 ms |
9924 KB |
Output is correct |
39 |
Correct |
317 ms |
9820 KB |
Output is correct |
40 |
Correct |
87 ms |
10076 KB |
Output is correct |
41 |
Correct |
101 ms |
9820 KB |
Output is correct |
42 |
Correct |
354 ms |
11100 KB |
Output is correct |
43 |
Correct |
92 ms |
11976 KB |
Output is correct |
44 |
Correct |
84 ms |
12124 KB |
Output is correct |
45 |
Correct |
42 ms |
14940 KB |
Output is correct |
46 |
Correct |
209 ms |
17048 KB |
Output is correct |
47 |
Execution timed out |
18075 ms |
206392 KB |
Time limit exceeded |
48 |
Halted |
0 ms |
0 KB |
- |