/**
* author: Haunted_Cpp
**/
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("unroll-loops")
template<typename T> ostream &operator << (ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; }
template<typename T, size_t size> ostream &operator << (ostream &os, const array<T, size> &arr) { os << '{'; string sep; for (const auto &x : arr) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream &operator << (ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
void debug_out() { cerr << endl; }
template<typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << ' ' << H; debug_out(T...); }
#ifdef LOCAL
#define debug(...) cerr << "(" << #__VA_ARGS__ << "):", debug_out(__VA_ARGS__)
#else
#define debug(...) 47
#endif
const int MAX_N = 1e5 + 5;
const int INF = 1e9 + 5;
vector<vector<pair<int, int>>> g(MAX_N);
int main () {
ios::sync_with_stdio(0);
cin.tie(0);
int n, m, k;
cin >> n >> m >> k;
for (int i = 0; i < m; i++) {
int st, et, w;
cin >> st >> et >> w;
--st; --et;
g[st].emplace_back(et, w);
g[et].emplace_back(st, w);
}
vector<bool> is_special(n);
for (int i = 0; i < k; i++) {
int foo;
cin >> foo;
--foo;
is_special[foo] = true;
}
int res = 0;
int add = 0;
auto Dijkstra = [&](bool basic_graph) {
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> pq;
vector<int> dist(n, INF);
vector<int> root(n, -1);
vector<pair<int, int>> closest_vertex(n, {INF, -1});
for (int i = 0; i < n; i++) {
if (is_special[i]) {
dist[i] = 0;
root[i] = i;
pq.push({dist[i], i});
}
}
while(!pq.empty()) {
int node = pq.top().second;
int w = pq.top().first;
pq.pop();
if (w != dist[node]) continue;
for (auto to : g[node]) {
if (root[to.first] != -1 && root[to.first] != root[node]) {
pair<int, int> best_way = {dist[node] + to.second + dist[to.first], root[to.first]};
closest_vertex[root[node]] = min(closest_vertex[root[node]], best_way);
}
if (dist[to.first] > dist[node] + to.second) {
dist[to.first] = dist[node] + to.second;
root[to.first] = root[node];
pq.push({dist[to.first], to.first});
}
}
}
int mn = 1e9;
for (int i = 0; i < n; i++) {
if (!is_special[i]) continue;
mn = min(mn, closest_vertex[i].first);
}
add = mn;
if (!basic_graph) {
for (int i = 0; i < n; i++) {
if (!is_special[i]) continue;
if (closest_vertex[i].first == mn) {
res += closest_vertex[i].first;
is_special[i] = false;
is_special[closest_vertex[i].second] = false;
break;
}
}
}
return closest_vertex;
};
if (n > 500) {
Dijkstra(false);
Dijkstra(false);
cout << res << '\n';
} else {
int mn = 1e9;
vector<pair<int, int>> dist = Dijkstra(true);
for (int i = 0; i < n; i++) {
if (!is_special[i]) continue;
if (dist[i].second == -1) continue;
assert(is_special[dist[i].second]);
int cur = dist[i].first;
is_special[i] = false;
is_special[dist[i].second] = false;
add = 0;
Dijkstra(true);
cur += add;
mn = min(mn, cur);
is_special[i] = true;
is_special[dist[i].second] = true;
}
cout << mn << '\n';
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
3 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Correct |
2 ms |
2688 KB |
Output is correct |
5 |
Correct |
2 ms |
2688 KB |
Output is correct |
6 |
Correct |
2 ms |
2688 KB |
Output is correct |
7 |
Correct |
3 ms |
2688 KB |
Output is correct |
8 |
Correct |
3 ms |
2688 KB |
Output is correct |
9 |
Correct |
2 ms |
2688 KB |
Output is correct |
10 |
Correct |
2 ms |
2688 KB |
Output is correct |
11 |
Correct |
2 ms |
2720 KB |
Output is correct |
12 |
Correct |
3 ms |
2688 KB |
Output is correct |
13 |
Correct |
4 ms |
2688 KB |
Output is correct |
14 |
Correct |
3 ms |
2688 KB |
Output is correct |
15 |
Correct |
3 ms |
2688 KB |
Output is correct |
16 |
Correct |
2 ms |
2688 KB |
Output is correct |
17 |
Correct |
3 ms |
2688 KB |
Output is correct |
18 |
Correct |
2 ms |
2688 KB |
Output is correct |
19 |
Correct |
3 ms |
2688 KB |
Output is correct |
20 |
Correct |
3 ms |
2688 KB |
Output is correct |
21 |
Correct |
3 ms |
2688 KB |
Output is correct |
22 |
Correct |
2 ms |
2688 KB |
Output is correct |
23 |
Correct |
4 ms |
2688 KB |
Output is correct |
24 |
Correct |
2 ms |
2688 KB |
Output is correct |
25 |
Correct |
2 ms |
2688 KB |
Output is correct |
26 |
Correct |
2 ms |
2688 KB |
Output is correct |
27 |
Correct |
2 ms |
2688 KB |
Output is correct |
28 |
Correct |
4 ms |
2688 KB |
Output is correct |
29 |
Correct |
4 ms |
2688 KB |
Output is correct |
30 |
Correct |
4 ms |
2688 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
3 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Correct |
2 ms |
2688 KB |
Output is correct |
5 |
Correct |
2 ms |
2688 KB |
Output is correct |
6 |
Correct |
2 ms |
2688 KB |
Output is correct |
7 |
Correct |
3 ms |
2688 KB |
Output is correct |
8 |
Correct |
3 ms |
2688 KB |
Output is correct |
9 |
Correct |
2 ms |
2688 KB |
Output is correct |
10 |
Correct |
2 ms |
2688 KB |
Output is correct |
11 |
Correct |
2 ms |
2720 KB |
Output is correct |
12 |
Correct |
3 ms |
2688 KB |
Output is correct |
13 |
Correct |
4 ms |
2688 KB |
Output is correct |
14 |
Correct |
3 ms |
2688 KB |
Output is correct |
15 |
Correct |
3 ms |
2688 KB |
Output is correct |
16 |
Correct |
2 ms |
2688 KB |
Output is correct |
17 |
Correct |
3 ms |
2688 KB |
Output is correct |
18 |
Correct |
2 ms |
2688 KB |
Output is correct |
19 |
Correct |
3 ms |
2688 KB |
Output is correct |
20 |
Correct |
3 ms |
2688 KB |
Output is correct |
21 |
Correct |
3 ms |
2688 KB |
Output is correct |
22 |
Correct |
2 ms |
2688 KB |
Output is correct |
23 |
Correct |
4 ms |
2688 KB |
Output is correct |
24 |
Correct |
2 ms |
2688 KB |
Output is correct |
25 |
Correct |
2 ms |
2688 KB |
Output is correct |
26 |
Correct |
2 ms |
2688 KB |
Output is correct |
27 |
Correct |
2 ms |
2688 KB |
Output is correct |
28 |
Correct |
4 ms |
2688 KB |
Output is correct |
29 |
Correct |
4 ms |
2688 KB |
Output is correct |
30 |
Correct |
4 ms |
2688 KB |
Output is correct |
31 |
Correct |
2 ms |
2688 KB |
Output is correct |
32 |
Correct |
3 ms |
2688 KB |
Output is correct |
33 |
Correct |
3 ms |
2688 KB |
Output is correct |
34 |
Correct |
3 ms |
2688 KB |
Output is correct |
35 |
Correct |
2 ms |
2688 KB |
Output is correct |
36 |
Correct |
55 ms |
2892 KB |
Output is correct |
37 |
Correct |
80 ms |
2944 KB |
Output is correct |
38 |
Correct |
5 ms |
2756 KB |
Output is correct |
39 |
Correct |
103 ms |
5984 KB |
Output is correct |
40 |
Correct |
125 ms |
3576 KB |
Output is correct |
41 |
Correct |
4 ms |
2688 KB |
Output is correct |
42 |
Correct |
145 ms |
3456 KB |
Output is correct |
43 |
Correct |
51 ms |
2816 KB |
Output is correct |
44 |
Correct |
3 ms |
2688 KB |
Output is correct |
45 |
Correct |
3 ms |
2688 KB |
Output is correct |
46 |
Correct |
671 ms |
6160 KB |
Output is correct |
47 |
Correct |
103 ms |
3200 KB |
Output is correct |
48 |
Correct |
140 ms |
5888 KB |
Output is correct |
49 |
Correct |
429 ms |
5888 KB |
Output is correct |
50 |
Correct |
4 ms |
2688 KB |
Output is correct |
51 |
Correct |
29 ms |
2688 KB |
Output is correct |
52 |
Correct |
4 ms |
2688 KB |
Output is correct |
53 |
Correct |
85 ms |
4412 KB |
Output is correct |
54 |
Correct |
126 ms |
5944 KB |
Output is correct |
55 |
Correct |
4 ms |
2688 KB |
Output is correct |
56 |
Correct |
2 ms |
2688 KB |
Output is correct |
57 |
Correct |
4 ms |
2688 KB |
Output is correct |
58 |
Correct |
398 ms |
6264 KB |
Output is correct |
59 |
Correct |
2 ms |
2688 KB |
Output is correct |
60 |
Correct |
34 ms |
2816 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
88 ms |
7124 KB |
Output is correct |
2 |
Correct |
21 ms |
4736 KB |
Output is correct |
3 |
Correct |
1871 ms |
64024 KB |
Output is correct |
4 |
Correct |
806 ms |
33476 KB |
Output is correct |
5 |
Correct |
202 ms |
10604 KB |
Output is correct |
6 |
Correct |
159 ms |
9188 KB |
Output is correct |
7 |
Correct |
248 ms |
11320 KB |
Output is correct |
8 |
Correct |
107 ms |
7160 KB |
Output is correct |
9 |
Correct |
162 ms |
8848 KB |
Output is correct |
10 |
Correct |
122 ms |
7672 KB |
Output is correct |
11 |
Correct |
2060 ms |
67476 KB |
Output is correct |
12 |
Correct |
139 ms |
7928 KB |
Output is correct |
13 |
Correct |
504 ms |
21776 KB |
Output is correct |
14 |
Correct |
226 ms |
11272 KB |
Output is correct |
15 |
Correct |
1866 ms |
66612 KB |
Output is correct |
16 |
Correct |
56 ms |
6776 KB |
Output is correct |
17 |
Correct |
1275 ms |
51292 KB |
Output is correct |
18 |
Correct |
9 ms |
4736 KB |
Output is correct |
19 |
Correct |
2208 ms |
71960 KB |
Output is correct |
20 |
Correct |
266 ms |
10924 KB |
Output is correct |
21 |
Correct |
224 ms |
10264 KB |
Output is correct |
22 |
Correct |
112 ms |
7556 KB |
Output is correct |
23 |
Correct |
41 ms |
6140 KB |
Output is correct |
24 |
Correct |
1288 ms |
55108 KB |
Output is correct |
25 |
Correct |
186 ms |
8968 KB |
Output is correct |
26 |
Correct |
87 ms |
7160 KB |
Output is correct |
27 |
Correct |
129 ms |
8268 KB |
Output is correct |
28 |
Correct |
22 ms |
5504 KB |
Output is correct |
29 |
Correct |
235 ms |
11136 KB |
Output is correct |
30 |
Correct |
441 ms |
18388 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
2688 KB |
Output is correct |
2 |
Correct |
3 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2688 KB |
Output is correct |
4 |
Correct |
2 ms |
2688 KB |
Output is correct |
5 |
Correct |
2 ms |
2688 KB |
Output is correct |
6 |
Correct |
2 ms |
2688 KB |
Output is correct |
7 |
Correct |
3 ms |
2688 KB |
Output is correct |
8 |
Correct |
3 ms |
2688 KB |
Output is correct |
9 |
Correct |
2 ms |
2688 KB |
Output is correct |
10 |
Correct |
2 ms |
2688 KB |
Output is correct |
11 |
Correct |
2 ms |
2720 KB |
Output is correct |
12 |
Correct |
3 ms |
2688 KB |
Output is correct |
13 |
Correct |
4 ms |
2688 KB |
Output is correct |
14 |
Correct |
3 ms |
2688 KB |
Output is correct |
15 |
Correct |
3 ms |
2688 KB |
Output is correct |
16 |
Correct |
2 ms |
2688 KB |
Output is correct |
17 |
Correct |
3 ms |
2688 KB |
Output is correct |
18 |
Correct |
2 ms |
2688 KB |
Output is correct |
19 |
Correct |
3 ms |
2688 KB |
Output is correct |
20 |
Correct |
3 ms |
2688 KB |
Output is correct |
21 |
Correct |
3 ms |
2688 KB |
Output is correct |
22 |
Correct |
2 ms |
2688 KB |
Output is correct |
23 |
Correct |
4 ms |
2688 KB |
Output is correct |
24 |
Correct |
2 ms |
2688 KB |
Output is correct |
25 |
Correct |
2 ms |
2688 KB |
Output is correct |
26 |
Correct |
2 ms |
2688 KB |
Output is correct |
27 |
Correct |
2 ms |
2688 KB |
Output is correct |
28 |
Correct |
4 ms |
2688 KB |
Output is correct |
29 |
Correct |
4 ms |
2688 KB |
Output is correct |
30 |
Correct |
4 ms |
2688 KB |
Output is correct |
31 |
Correct |
2 ms |
2688 KB |
Output is correct |
32 |
Correct |
3 ms |
2688 KB |
Output is correct |
33 |
Correct |
3 ms |
2688 KB |
Output is correct |
34 |
Correct |
3 ms |
2688 KB |
Output is correct |
35 |
Correct |
2 ms |
2688 KB |
Output is correct |
36 |
Correct |
55 ms |
2892 KB |
Output is correct |
37 |
Correct |
80 ms |
2944 KB |
Output is correct |
38 |
Correct |
5 ms |
2756 KB |
Output is correct |
39 |
Correct |
103 ms |
5984 KB |
Output is correct |
40 |
Correct |
125 ms |
3576 KB |
Output is correct |
41 |
Correct |
4 ms |
2688 KB |
Output is correct |
42 |
Correct |
145 ms |
3456 KB |
Output is correct |
43 |
Correct |
51 ms |
2816 KB |
Output is correct |
44 |
Correct |
3 ms |
2688 KB |
Output is correct |
45 |
Correct |
3 ms |
2688 KB |
Output is correct |
46 |
Correct |
671 ms |
6160 KB |
Output is correct |
47 |
Correct |
103 ms |
3200 KB |
Output is correct |
48 |
Correct |
140 ms |
5888 KB |
Output is correct |
49 |
Correct |
429 ms |
5888 KB |
Output is correct |
50 |
Correct |
4 ms |
2688 KB |
Output is correct |
51 |
Correct |
29 ms |
2688 KB |
Output is correct |
52 |
Correct |
4 ms |
2688 KB |
Output is correct |
53 |
Correct |
85 ms |
4412 KB |
Output is correct |
54 |
Correct |
126 ms |
5944 KB |
Output is correct |
55 |
Correct |
4 ms |
2688 KB |
Output is correct |
56 |
Correct |
2 ms |
2688 KB |
Output is correct |
57 |
Correct |
4 ms |
2688 KB |
Output is correct |
58 |
Correct |
398 ms |
6264 KB |
Output is correct |
59 |
Correct |
2 ms |
2688 KB |
Output is correct |
60 |
Correct |
34 ms |
2816 KB |
Output is correct |
61 |
Correct |
88 ms |
7124 KB |
Output is correct |
62 |
Correct |
21 ms |
4736 KB |
Output is correct |
63 |
Correct |
1871 ms |
64024 KB |
Output is correct |
64 |
Correct |
806 ms |
33476 KB |
Output is correct |
65 |
Correct |
202 ms |
10604 KB |
Output is correct |
66 |
Correct |
159 ms |
9188 KB |
Output is correct |
67 |
Correct |
248 ms |
11320 KB |
Output is correct |
68 |
Correct |
107 ms |
7160 KB |
Output is correct |
69 |
Correct |
162 ms |
8848 KB |
Output is correct |
70 |
Correct |
122 ms |
7672 KB |
Output is correct |
71 |
Correct |
2060 ms |
67476 KB |
Output is correct |
72 |
Correct |
139 ms |
7928 KB |
Output is correct |
73 |
Correct |
504 ms |
21776 KB |
Output is correct |
74 |
Correct |
226 ms |
11272 KB |
Output is correct |
75 |
Correct |
1866 ms |
66612 KB |
Output is correct |
76 |
Correct |
56 ms |
6776 KB |
Output is correct |
77 |
Correct |
1275 ms |
51292 KB |
Output is correct |
78 |
Correct |
9 ms |
4736 KB |
Output is correct |
79 |
Correct |
2208 ms |
71960 KB |
Output is correct |
80 |
Correct |
266 ms |
10924 KB |
Output is correct |
81 |
Correct |
224 ms |
10264 KB |
Output is correct |
82 |
Correct |
112 ms |
7556 KB |
Output is correct |
83 |
Correct |
41 ms |
6140 KB |
Output is correct |
84 |
Correct |
1288 ms |
55108 KB |
Output is correct |
85 |
Correct |
186 ms |
8968 KB |
Output is correct |
86 |
Correct |
87 ms |
7160 KB |
Output is correct |
87 |
Correct |
129 ms |
8268 KB |
Output is correct |
88 |
Correct |
22 ms |
5504 KB |
Output is correct |
89 |
Correct |
235 ms |
11136 KB |
Output is correct |
90 |
Correct |
441 ms |
18388 KB |
Output is correct |
91 |
Incorrect |
135 ms |
10276 KB |
Output isn't correct |
92 |
Halted |
0 ms |
0 KB |
- |