#include<bits/stdc++.h>
using namespace std;
using ll = long long;
#define int ll
const int N = 1e5 + 5, inf = 1e16;
vector<pair<int, int>> adj[N];
int special[N];
int n, m, k;
priority_queue<pair<int, int>> q;
int dis[N], src[N];
int dijkstra(int s, int t) {
memset(dis, 0x3f, sizeof(dis));
q.emplace(0, s); dis[s] = 0;
while (!q.empty()) {
int d, u; tie(d, u) = q.top(); q.pop();
if (-d != dis[u]) continue;
if (special[u] && u != s && u != t) return dis[u];
for (auto& p : adj[u]) {
int v, w; tie(v, w) = p;
if (dis[u] + w < dis[v])
dis[v] = dis[u] + w, q.emplace(-dis[v], v);
}
}
return inf;
}
tuple<int, int, int> dijkstra0(int x = -1, int y = -1) {
memset(dis, 0x3f, sizeof(dis));
for (int v = 0; v < n; ++v) if (special[v] && v != x && v != y)
dis[v] = 0, src[v] = v, q.emplace(0, v);
while (!q.empty()) {
int d, u; tie(d, u) = q.top(); q.pop();
if (-d != dis[u]) continue;
for (auto& p : adj[u]) {
int v, w; tie(v, w) = p;
if (dis[u] + w < dis[v])
dis[v] = dis[u] + w, src[v] = src[u], q.emplace(-dis[v], v);
}
}
int res = inf, s = -1, t = -1;
for (int u = 0; u < n; ++u) for (auto& p : adj[u]) {
int v, w; tie(v, w) = p;
if (src[u] != src[v] && dis[u] + dis[v] + w < res)
res = dis[u] + dis[v] + w, s = src[u], t = src[v];
}
return make_tuple(res, s, t);
}
signed main() {
cin.tie(0)->sync_with_stdio(0); cout.tie(0);
cin >> n >> m >> k;
for (int i = 0, u, v, w; i < m; ++i)
cin >> u >> v >> w, u--, v--,
adj[u].emplace_back(v, w),
adj[v].emplace_back(u, w);
for (int i = 0, v; i < k; ++i)
cin >> v, v--, special[v] = 1;
int val1, val2, s, t, tmp1, tmp2;
tie(val1, s, t) = dijkstra0(); tie(val2, tmp1, tmp2) = dijkstra0(s, t);
cout << min(val1 + val2, dijkstra(s, t) + dijkstra(t, s));
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
3412 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
3412 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
52 ms |
8300 KB |
Output is correct |
2 |
Correct |
6 ms |
4180 KB |
Output is correct |
3 |
Correct |
1211 ms |
120212 KB |
Output is correct |
4 |
Correct |
549 ms |
59360 KB |
Output is correct |
5 |
Correct |
155 ms |
14276 KB |
Output is correct |
6 |
Correct |
126 ms |
12136 KB |
Output is correct |
7 |
Correct |
181 ms |
15560 KB |
Output is correct |
8 |
Correct |
57 ms |
8312 KB |
Output is correct |
9 |
Correct |
116 ms |
11336 KB |
Output is correct |
10 |
Correct |
81 ms |
9408 KB |
Output is correct |
11 |
Correct |
1210 ms |
127020 KB |
Output is correct |
12 |
Correct |
85 ms |
9808 KB |
Output is correct |
13 |
Correct |
346 ms |
36204 KB |
Output is correct |
14 |
Correct |
190 ms |
16212 KB |
Output is correct |
15 |
Correct |
1232 ms |
124780 KB |
Output is correct |
16 |
Correct |
39 ms |
7636 KB |
Output is correct |
17 |
Correct |
857 ms |
93164 KB |
Output is correct |
18 |
Correct |
6 ms |
4436 KB |
Output is correct |
19 |
Correct |
1344 ms |
133852 KB |
Output is correct |
20 |
Correct |
168 ms |
15352 KB |
Output is correct |
21 |
Correct |
137 ms |
13628 KB |
Output is correct |
22 |
Correct |
78 ms |
9264 KB |
Output is correct |
23 |
Correct |
19 ms |
6860 KB |
Output is correct |
24 |
Correct |
842 ms |
102728 KB |
Output is correct |
25 |
Correct |
119 ms |
11500 KB |
Output is correct |
26 |
Correct |
55 ms |
8288 KB |
Output is correct |
27 |
Correct |
107 ms |
10432 KB |
Output is correct |
28 |
Correct |
14 ms |
5460 KB |
Output is correct |
29 |
Correct |
189 ms |
15228 KB |
Output is correct |
30 |
Correct |
316 ms |
29176 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
2 ms |
3412 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |