#include <iostream>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <array>
#include <stack>
#include <queue>
#include <random>
#include <numeric>
#include <functional>
#include <chrono>
#include <utility>
#include <iomanip>
#include <assert.h>
using namespace std;
void dbg_out() { cerr << endl; }
template<typename Head, typename... Tail>
void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); }
#define dbg(...) cerr << "(" << #__VA_ARGS__ << "):", dbg_out(__VA_ARGS__)
#define rng_init mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define rng_seed(x) mt19937 rng(x)
#define all(x) (x).begin(), (x).end()
#define sz(x) (int) (x).size()
// #define int long long
rng_init;
const int MXN = 1e5 + 5, INF = 1e9 + 5;
int N, M, K;
vector<pair<int, int>> g[MXN];
vector<array<int, 3>> edges;
void dijkstra(const vector<int> &src, vector<int> &dist, vector<int> &par) {
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;
for (const auto &x : src) {
dist[x] = 0;
pq.emplace(0, x);
}
for (int i = 0; i < N; i++)
par[i] = i;
while (!pq.empty()) {
auto cur = pq.top();
pq.pop();
int u = cur.second, cur_dist = cur.first;
if (cur_dist != dist[u]) continue;
for (const auto &[v, wt] : g[u]) {
int new_dist = cur_dist + wt;
if (new_dist < dist[v]) {
dist[v] = new_dist;
pq.emplace(new_dist, v);
par[v] = par[u];
}
}
}
}
int closest_pair(const vector<int> &dist, const vector<int> &par) {
int pair_dist = INF;
for (const auto &[u, v, wt] : edges) {
if (par[u] == par[v]) continue;
pair_dist = min(pair_dist, dist[u] + wt + dist[v]);
}
return pair_dist;
}
void solve() {
cin >> N >> M >> K;
for (int i = 0; i < M; i++) {
int u, v, w;
cin >> u >> v >> w;
u--, v--;
edges.push_back({u, v, w});
g[u].emplace_back(v, w);
g[v].emplace_back(u, w);
}
vector<int> special(K);
for (auto &x : special) {
cin >> x;
x--;
}
int ans = INF;
for (int iter = 0; iter < 30; iter++) {
vector<int> src[2];
for (const auto &x : special)
src[rng() % 2].push_back(x);
vector<int> dist1(N, INF), dist2(N, INF);
vector<int> par1(N), par2(N);
dijkstra(src[0], dist1, par1);
dijkstra(src[1], dist2, par2);
ans = min(ans, closest_pair(dist1, par1) + closest_pair(dist2, par2));
}
cout << ans;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int TC = 1;
// cin >> TC;
while (TC--) solve();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
4 ms |
2796 KB |
Output is correct |
3 |
Correct |
2 ms |
2668 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
2 ms |
2668 KB |
Output is correct |
6 |
Correct |
2 ms |
2668 KB |
Output is correct |
7 |
Correct |
4 ms |
2796 KB |
Output is correct |
8 |
Correct |
4 ms |
2796 KB |
Output is correct |
9 |
Correct |
2 ms |
2668 KB |
Output is correct |
10 |
Correct |
3 ms |
2668 KB |
Output is correct |
11 |
Correct |
2 ms |
2668 KB |
Output is correct |
12 |
Correct |
2 ms |
2668 KB |
Output is correct |
13 |
Correct |
3 ms |
2796 KB |
Output is correct |
14 |
Correct |
3 ms |
2668 KB |
Output is correct |
15 |
Correct |
3 ms |
2796 KB |
Output is correct |
16 |
Correct |
2 ms |
2668 KB |
Output is correct |
17 |
Correct |
3 ms |
2668 KB |
Output is correct |
18 |
Correct |
2 ms |
2668 KB |
Output is correct |
19 |
Correct |
3 ms |
2796 KB |
Output is correct |
20 |
Correct |
3 ms |
2668 KB |
Output is correct |
21 |
Correct |
3 ms |
2668 KB |
Output is correct |
22 |
Correct |
2 ms |
2668 KB |
Output is correct |
23 |
Correct |
3 ms |
2668 KB |
Output is correct |
24 |
Correct |
2 ms |
2668 KB |
Output is correct |
25 |
Correct |
2 ms |
2668 KB |
Output is correct |
26 |
Correct |
2 ms |
2668 KB |
Output is correct |
27 |
Correct |
2 ms |
2668 KB |
Output is correct |
28 |
Correct |
3 ms |
2816 KB |
Output is correct |
29 |
Correct |
5 ms |
2796 KB |
Output is correct |
30 |
Correct |
4 ms |
2796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
4 ms |
2796 KB |
Output is correct |
3 |
Correct |
2 ms |
2668 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
2 ms |
2668 KB |
Output is correct |
6 |
Correct |
2 ms |
2668 KB |
Output is correct |
7 |
Correct |
4 ms |
2796 KB |
Output is correct |
8 |
Correct |
4 ms |
2796 KB |
Output is correct |
9 |
Correct |
2 ms |
2668 KB |
Output is correct |
10 |
Correct |
3 ms |
2668 KB |
Output is correct |
11 |
Correct |
2 ms |
2668 KB |
Output is correct |
12 |
Correct |
2 ms |
2668 KB |
Output is correct |
13 |
Correct |
3 ms |
2796 KB |
Output is correct |
14 |
Correct |
3 ms |
2668 KB |
Output is correct |
15 |
Correct |
3 ms |
2796 KB |
Output is correct |
16 |
Correct |
2 ms |
2668 KB |
Output is correct |
17 |
Correct |
3 ms |
2668 KB |
Output is correct |
18 |
Correct |
2 ms |
2668 KB |
Output is correct |
19 |
Correct |
3 ms |
2796 KB |
Output is correct |
20 |
Correct |
3 ms |
2668 KB |
Output is correct |
21 |
Correct |
3 ms |
2668 KB |
Output is correct |
22 |
Correct |
2 ms |
2668 KB |
Output is correct |
23 |
Correct |
3 ms |
2668 KB |
Output is correct |
24 |
Correct |
2 ms |
2668 KB |
Output is correct |
25 |
Correct |
2 ms |
2668 KB |
Output is correct |
26 |
Correct |
2 ms |
2668 KB |
Output is correct |
27 |
Correct |
2 ms |
2668 KB |
Output is correct |
28 |
Correct |
3 ms |
2816 KB |
Output is correct |
29 |
Correct |
5 ms |
2796 KB |
Output is correct |
30 |
Correct |
4 ms |
2796 KB |
Output is correct |
31 |
Correct |
4 ms |
2668 KB |
Output is correct |
32 |
Correct |
5 ms |
2796 KB |
Output is correct |
33 |
Correct |
6 ms |
2796 KB |
Output is correct |
34 |
Correct |
3 ms |
2668 KB |
Output is correct |
35 |
Correct |
2 ms |
2668 KB |
Output is correct |
36 |
Correct |
14 ms |
2924 KB |
Output is correct |
37 |
Correct |
15 ms |
2924 KB |
Output is correct |
38 |
Correct |
7 ms |
2944 KB |
Output is correct |
39 |
Correct |
74 ms |
6180 KB |
Output is correct |
40 |
Correct |
26 ms |
3564 KB |
Output is correct |
41 |
Correct |
7 ms |
2796 KB |
Output is correct |
42 |
Correct |
25 ms |
3564 KB |
Output is correct |
43 |
Correct |
12 ms |
2796 KB |
Output is correct |
44 |
Correct |
7 ms |
2796 KB |
Output is correct |
45 |
Correct |
2 ms |
2668 KB |
Output is correct |
46 |
Correct |
76 ms |
6308 KB |
Output is correct |
47 |
Correct |
22 ms |
3180 KB |
Output is correct |
48 |
Correct |
67 ms |
6180 KB |
Output is correct |
49 |
Correct |
70 ms |
6268 KB |
Output is correct |
50 |
Correct |
6 ms |
2924 KB |
Output is correct |
51 |
Correct |
8 ms |
2796 KB |
Output is correct |
52 |
Correct |
6 ms |
2796 KB |
Output is correct |
53 |
Correct |
48 ms |
4424 KB |
Output is correct |
54 |
Correct |
75 ms |
6436 KB |
Output is correct |
55 |
Correct |
6 ms |
2668 KB |
Output is correct |
56 |
Correct |
3 ms |
2668 KB |
Output is correct |
57 |
Correct |
6 ms |
2796 KB |
Output is correct |
58 |
Correct |
85 ms |
6432 KB |
Output is correct |
59 |
Correct |
2 ms |
2668 KB |
Output is correct |
60 |
Correct |
9 ms |
2796 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
953 ms |
8004 KB |
Output is correct |
2 |
Correct |
21 ms |
4844 KB |
Output is correct |
3 |
Execution timed out |
6090 ms |
107508 KB |
Time limit exceeded |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2668 KB |
Output is correct |
2 |
Correct |
4 ms |
2796 KB |
Output is correct |
3 |
Correct |
2 ms |
2668 KB |
Output is correct |
4 |
Correct |
2 ms |
2668 KB |
Output is correct |
5 |
Correct |
2 ms |
2668 KB |
Output is correct |
6 |
Correct |
2 ms |
2668 KB |
Output is correct |
7 |
Correct |
4 ms |
2796 KB |
Output is correct |
8 |
Correct |
4 ms |
2796 KB |
Output is correct |
9 |
Correct |
2 ms |
2668 KB |
Output is correct |
10 |
Correct |
3 ms |
2668 KB |
Output is correct |
11 |
Correct |
2 ms |
2668 KB |
Output is correct |
12 |
Correct |
2 ms |
2668 KB |
Output is correct |
13 |
Correct |
3 ms |
2796 KB |
Output is correct |
14 |
Correct |
3 ms |
2668 KB |
Output is correct |
15 |
Correct |
3 ms |
2796 KB |
Output is correct |
16 |
Correct |
2 ms |
2668 KB |
Output is correct |
17 |
Correct |
3 ms |
2668 KB |
Output is correct |
18 |
Correct |
2 ms |
2668 KB |
Output is correct |
19 |
Correct |
3 ms |
2796 KB |
Output is correct |
20 |
Correct |
3 ms |
2668 KB |
Output is correct |
21 |
Correct |
3 ms |
2668 KB |
Output is correct |
22 |
Correct |
2 ms |
2668 KB |
Output is correct |
23 |
Correct |
3 ms |
2668 KB |
Output is correct |
24 |
Correct |
2 ms |
2668 KB |
Output is correct |
25 |
Correct |
2 ms |
2668 KB |
Output is correct |
26 |
Correct |
2 ms |
2668 KB |
Output is correct |
27 |
Correct |
2 ms |
2668 KB |
Output is correct |
28 |
Correct |
3 ms |
2816 KB |
Output is correct |
29 |
Correct |
5 ms |
2796 KB |
Output is correct |
30 |
Correct |
4 ms |
2796 KB |
Output is correct |
31 |
Correct |
4 ms |
2668 KB |
Output is correct |
32 |
Correct |
5 ms |
2796 KB |
Output is correct |
33 |
Correct |
6 ms |
2796 KB |
Output is correct |
34 |
Correct |
3 ms |
2668 KB |
Output is correct |
35 |
Correct |
2 ms |
2668 KB |
Output is correct |
36 |
Correct |
14 ms |
2924 KB |
Output is correct |
37 |
Correct |
15 ms |
2924 KB |
Output is correct |
38 |
Correct |
7 ms |
2944 KB |
Output is correct |
39 |
Correct |
74 ms |
6180 KB |
Output is correct |
40 |
Correct |
26 ms |
3564 KB |
Output is correct |
41 |
Correct |
7 ms |
2796 KB |
Output is correct |
42 |
Correct |
25 ms |
3564 KB |
Output is correct |
43 |
Correct |
12 ms |
2796 KB |
Output is correct |
44 |
Correct |
7 ms |
2796 KB |
Output is correct |
45 |
Correct |
2 ms |
2668 KB |
Output is correct |
46 |
Correct |
76 ms |
6308 KB |
Output is correct |
47 |
Correct |
22 ms |
3180 KB |
Output is correct |
48 |
Correct |
67 ms |
6180 KB |
Output is correct |
49 |
Correct |
70 ms |
6268 KB |
Output is correct |
50 |
Correct |
6 ms |
2924 KB |
Output is correct |
51 |
Correct |
8 ms |
2796 KB |
Output is correct |
52 |
Correct |
6 ms |
2796 KB |
Output is correct |
53 |
Correct |
48 ms |
4424 KB |
Output is correct |
54 |
Correct |
75 ms |
6436 KB |
Output is correct |
55 |
Correct |
6 ms |
2668 KB |
Output is correct |
56 |
Correct |
3 ms |
2668 KB |
Output is correct |
57 |
Correct |
6 ms |
2796 KB |
Output is correct |
58 |
Correct |
85 ms |
6432 KB |
Output is correct |
59 |
Correct |
2 ms |
2668 KB |
Output is correct |
60 |
Correct |
9 ms |
2796 KB |
Output is correct |
61 |
Correct |
953 ms |
8004 KB |
Output is correct |
62 |
Correct |
21 ms |
4844 KB |
Output is correct |
63 |
Execution timed out |
6090 ms |
107508 KB |
Time limit exceeded |
64 |
Halted |
0 ms |
0 KB |
- |