// BEGIN BOILERPLATE CODE
#include <bits/stdc++.h>
using namespace std;
#ifdef KAMIRULEZ
const bool kami_loc = true;
const long long kami_seed = 69420;
#else
const bool kami_loc = false;
const long long kami_seed = chrono::steady_clock::now().time_since_epoch().count();
#endif
const string kami_fi = "kamirulez.inp";
const string kami_fo = "kamirulez.out";
mt19937_64 kami_gen(kami_seed);
long long rand_range(long long rmin, long long rmax) {
uniform_int_distribution<long long> rdist(rmin, rmax);
return rdist(kami_gen);
}
long double rand_real(long double rmin, long double rmax) {
uniform_real_distribution<long double> rdist(rmin, rmax);
return rdist(kami_gen);
}
void file_io(string fi, string fo) {
if (fi != "" && (!kami_loc || fi == kami_fi)) {
freopen(fi.c_str(), "r", stdin);
}
if (fo != "" && (!kami_loc || fo == kami_fo)) {
freopen(fo.c_str(), "w", stdout);
}
}
void set_up() {
if (kami_loc) {
file_io(kami_fi, kami_fo);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
}
void just_do_it();
void just_exec_it() {
if (kami_loc) {
auto pstart = chrono::steady_clock::now();
just_do_it();
auto pend = chrono::steady_clock::now();
long long ptime = chrono::duration_cast<chrono::milliseconds>(pend - pstart).count();
string bar(50, '=');
cout << '\n' << bar << '\n';
cout << "Time: " << ptime << " ms" << '\n';
}
else {
just_do_it();
}
}
int main() {
set_up();
just_exec_it();
return 0;
}
// END BOILERPLATE CODE
// BEGIN MAIN CODE
const int ms = 1e5 + 20;
const int inf = 1e9 + 20;
vector<pair<int, int>> adj[ms];
int ds[ms];
int du[ms];
int dv[ms];
int h[ms];
int g[ms];
bool f[ms];
int n;
void dijk(int d[]) {
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
for (int i = 1; i <= n; i++) {
if (d[i] == 0) {
h[i] = i;
q.push({0, i});
}
}
while (!q.empty()) {
int cd = q.top().first;
int u = q.top().second;
q.pop();
if (d[u] != cd) {
continue;
}
for (auto x: adj[u]) {
int v = x.first;
if (f[v]) {
continue;
}
int w = x.second;
if (d[u] + w < d[v]) {
d[v] = d[u] + w;
h[v] = h[u];
q.push({d[v], v});
}
}
}
}
bool cmp(int u, int v) {
return dv[u] < dv[v];
}
void just_do_it() {
//file_io("4special.inp", "4special.out");
int m, k;
cin >> n >> m >> k;
for (int i = 1; i <= n; i++) {
ds[i] = inf;
du[i] = inf;
dv[i] = inf;
}
for (int i = 0; i < m; i++) {
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
for (int i = 0; i < k; i++) {
int u;
cin >> u;
g[i] = u;
ds[u] = 0;
}
dijk(ds);
int mi = inf;
int fu = -1;
int fv = -1;
for (int i = 1; i <= n; i++) {
for (auto x: adj[i]) {
int j = x.first;
int w = x.second;
if (h[i] != h[j]) {
if (ds[i] + ds[j] + w < mi) {
mi = ds[i] + ds[j] + w;
fu = h[i];
fv = h[j];
}
}
}
}
du[fu] = 0;
dijk(du);
dv[fv] = 0;
dijk(dv);
sort(g, g + k, cmp);
int res = inf;
for (int i = 0; i < k; i++) {
if (g[i] == fu || g[i] == fv) {
continue;
}
for (int j = 0; j < k; j++) {
if (g[j] != g[i] && g[j] != fu && g[j] != fv) {
res = min(res, du[g[i]] + dv[g[j]]);
break;
}
}
}
for (int i = 1; i <= n; i++) {
ds[i] = inf;
}
for (int i = 0; i < k; i++) {
ds[g[i]] = 0;
}
ds[fu] = inf;
ds[fv] = inf;
f[fu] = true;
f[fv] = true;
dijk(ds);
for (int i = 1; i <= n; i++) {
for (auto x: adj[i]) {
int j = x.first;
int w = x.second;
if (h[i] != h[j]) {
res = min(res, mi + ds[i] + ds[j] + w);
}
}
}
cout << res;
}
// END MAIN CODE
Compilation message
RelayMarathon.cpp: In function 'void file_io(std::string, std::string)':
RelayMarathon.cpp:30:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
30 | freopen(fi.c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
RelayMarathon.cpp:33:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | freopen(fo.c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2684 KB |
Output is correct |
5 |
Correct |
3 ms |
2676 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
3 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
1 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
3 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2684 KB |
Output is correct |
16 |
Correct |
2 ms |
2684 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2664 KB |
Output is correct |
20 |
Correct |
2 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2644 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
2 ms |
2644 KB |
Output is correct |
24 |
Correct |
2 ms |
2644 KB |
Output is correct |
25 |
Correct |
2 ms |
2644 KB |
Output is correct |
26 |
Correct |
2 ms |
2644 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
4 ms |
2648 KB |
Output is correct |
29 |
Correct |
2 ms |
2644 KB |
Output is correct |
30 |
Correct |
2 ms |
2688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2684 KB |
Output is correct |
5 |
Correct |
3 ms |
2676 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
3 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
1 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
3 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2684 KB |
Output is correct |
16 |
Correct |
2 ms |
2684 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2664 KB |
Output is correct |
20 |
Correct |
2 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2644 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
2 ms |
2644 KB |
Output is correct |
24 |
Correct |
2 ms |
2644 KB |
Output is correct |
25 |
Correct |
2 ms |
2644 KB |
Output is correct |
26 |
Correct |
2 ms |
2644 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
4 ms |
2648 KB |
Output is correct |
29 |
Correct |
2 ms |
2644 KB |
Output is correct |
30 |
Correct |
2 ms |
2688 KB |
Output is correct |
31 |
Correct |
3 ms |
2684 KB |
Output is correct |
32 |
Correct |
2 ms |
2644 KB |
Output is correct |
33 |
Correct |
2 ms |
2644 KB |
Output is correct |
34 |
Correct |
2 ms |
2688 KB |
Output is correct |
35 |
Correct |
2 ms |
2644 KB |
Output is correct |
36 |
Correct |
3 ms |
2768 KB |
Output is correct |
37 |
Correct |
4 ms |
2900 KB |
Output is correct |
38 |
Correct |
2 ms |
2688 KB |
Output is correct |
39 |
Correct |
56 ms |
5948 KB |
Output is correct |
40 |
Correct |
8 ms |
3464 KB |
Output is correct |
41 |
Correct |
3 ms |
2644 KB |
Output is correct |
42 |
Correct |
10 ms |
3412 KB |
Output is correct |
43 |
Correct |
4 ms |
2772 KB |
Output is correct |
44 |
Correct |
4 ms |
2644 KB |
Output is correct |
45 |
Correct |
3 ms |
2644 KB |
Output is correct |
46 |
Correct |
35 ms |
6228 KB |
Output is correct |
47 |
Correct |
7 ms |
3156 KB |
Output is correct |
48 |
Correct |
22 ms |
5752 KB |
Output is correct |
49 |
Correct |
24 ms |
5936 KB |
Output is correct |
50 |
Correct |
2 ms |
2644 KB |
Output is correct |
51 |
Correct |
3 ms |
2796 KB |
Output is correct |
52 |
Correct |
3 ms |
2644 KB |
Output is correct |
53 |
Correct |
15 ms |
4372 KB |
Output is correct |
54 |
Correct |
34 ms |
5936 KB |
Output is correct |
55 |
Correct |
2 ms |
2644 KB |
Output is correct |
56 |
Correct |
2 ms |
2644 KB |
Output is correct |
57 |
Correct |
3 ms |
2680 KB |
Output is correct |
58 |
Correct |
46 ms |
6232 KB |
Output is correct |
59 |
Correct |
2 ms |
2684 KB |
Output is correct |
60 |
Correct |
2 ms |
2772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
8092 KB |
Output is correct |
2 |
Correct |
7 ms |
4436 KB |
Output is correct |
3 |
Correct |
1228 ms |
64456 KB |
Output is correct |
4 |
Correct |
565 ms |
35180 KB |
Output is correct |
5 |
Correct |
177 ms |
12032 KB |
Output is correct |
6 |
Correct |
114 ms |
10616 KB |
Output is correct |
7 |
Correct |
167 ms |
12668 KB |
Output is correct |
8 |
Correct |
74 ms |
8140 KB |
Output is correct |
9 |
Correct |
127 ms |
10160 KB |
Output is correct |
10 |
Correct |
79 ms |
9020 KB |
Output is correct |
11 |
Correct |
1238 ms |
67908 KB |
Output is correct |
12 |
Correct |
85 ms |
9328 KB |
Output is correct |
13 |
Correct |
366 ms |
23304 KB |
Output is correct |
14 |
Correct |
164 ms |
12796 KB |
Output is correct |
15 |
Correct |
1255 ms |
66792 KB |
Output is correct |
16 |
Correct |
48 ms |
7616 KB |
Output is correct |
17 |
Correct |
895 ms |
51244 KB |
Output is correct |
18 |
Correct |
7 ms |
4644 KB |
Output is correct |
19 |
Correct |
1361 ms |
72028 KB |
Output is correct |
20 |
Correct |
147 ms |
12200 KB |
Output is correct |
21 |
Correct |
157 ms |
11592 KB |
Output is correct |
22 |
Correct |
67 ms |
8812 KB |
Output is correct |
23 |
Correct |
21 ms |
6796 KB |
Output is correct |
24 |
Correct |
869 ms |
55528 KB |
Output is correct |
25 |
Correct |
104 ms |
10180 KB |
Output is correct |
26 |
Correct |
75 ms |
8192 KB |
Output is correct |
27 |
Correct |
91 ms |
9592 KB |
Output is correct |
28 |
Correct |
13 ms |
5652 KB |
Output is correct |
29 |
Correct |
166 ms |
12332 KB |
Output is correct |
30 |
Correct |
317 ms |
19332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2644 KB |
Output is correct |
2 |
Correct |
2 ms |
2688 KB |
Output is correct |
3 |
Correct |
2 ms |
2644 KB |
Output is correct |
4 |
Correct |
2 ms |
2684 KB |
Output is correct |
5 |
Correct |
3 ms |
2676 KB |
Output is correct |
6 |
Correct |
2 ms |
2644 KB |
Output is correct |
7 |
Correct |
3 ms |
2644 KB |
Output is correct |
8 |
Correct |
2 ms |
2644 KB |
Output is correct |
9 |
Correct |
1 ms |
2644 KB |
Output is correct |
10 |
Correct |
2 ms |
2644 KB |
Output is correct |
11 |
Correct |
2 ms |
2644 KB |
Output is correct |
12 |
Correct |
2 ms |
2644 KB |
Output is correct |
13 |
Correct |
2 ms |
2644 KB |
Output is correct |
14 |
Correct |
3 ms |
2644 KB |
Output is correct |
15 |
Correct |
2 ms |
2684 KB |
Output is correct |
16 |
Correct |
2 ms |
2684 KB |
Output is correct |
17 |
Correct |
2 ms |
2644 KB |
Output is correct |
18 |
Correct |
2 ms |
2644 KB |
Output is correct |
19 |
Correct |
2 ms |
2664 KB |
Output is correct |
20 |
Correct |
2 ms |
2644 KB |
Output is correct |
21 |
Correct |
2 ms |
2644 KB |
Output is correct |
22 |
Correct |
2 ms |
2644 KB |
Output is correct |
23 |
Correct |
2 ms |
2644 KB |
Output is correct |
24 |
Correct |
2 ms |
2644 KB |
Output is correct |
25 |
Correct |
2 ms |
2644 KB |
Output is correct |
26 |
Correct |
2 ms |
2644 KB |
Output is correct |
27 |
Correct |
2 ms |
2644 KB |
Output is correct |
28 |
Correct |
4 ms |
2648 KB |
Output is correct |
29 |
Correct |
2 ms |
2644 KB |
Output is correct |
30 |
Correct |
2 ms |
2688 KB |
Output is correct |
31 |
Correct |
3 ms |
2684 KB |
Output is correct |
32 |
Correct |
2 ms |
2644 KB |
Output is correct |
33 |
Correct |
2 ms |
2644 KB |
Output is correct |
34 |
Correct |
2 ms |
2688 KB |
Output is correct |
35 |
Correct |
2 ms |
2644 KB |
Output is correct |
36 |
Correct |
3 ms |
2768 KB |
Output is correct |
37 |
Correct |
4 ms |
2900 KB |
Output is correct |
38 |
Correct |
2 ms |
2688 KB |
Output is correct |
39 |
Correct |
56 ms |
5948 KB |
Output is correct |
40 |
Correct |
8 ms |
3464 KB |
Output is correct |
41 |
Correct |
3 ms |
2644 KB |
Output is correct |
42 |
Correct |
10 ms |
3412 KB |
Output is correct |
43 |
Correct |
4 ms |
2772 KB |
Output is correct |
44 |
Correct |
4 ms |
2644 KB |
Output is correct |
45 |
Correct |
3 ms |
2644 KB |
Output is correct |
46 |
Correct |
35 ms |
6228 KB |
Output is correct |
47 |
Correct |
7 ms |
3156 KB |
Output is correct |
48 |
Correct |
22 ms |
5752 KB |
Output is correct |
49 |
Correct |
24 ms |
5936 KB |
Output is correct |
50 |
Correct |
2 ms |
2644 KB |
Output is correct |
51 |
Correct |
3 ms |
2796 KB |
Output is correct |
52 |
Correct |
3 ms |
2644 KB |
Output is correct |
53 |
Correct |
15 ms |
4372 KB |
Output is correct |
54 |
Correct |
34 ms |
5936 KB |
Output is correct |
55 |
Correct |
2 ms |
2644 KB |
Output is correct |
56 |
Correct |
2 ms |
2644 KB |
Output is correct |
57 |
Correct |
3 ms |
2680 KB |
Output is correct |
58 |
Correct |
46 ms |
6232 KB |
Output is correct |
59 |
Correct |
2 ms |
2684 KB |
Output is correct |
60 |
Correct |
2 ms |
2772 KB |
Output is correct |
61 |
Correct |
69 ms |
8092 KB |
Output is correct |
62 |
Correct |
7 ms |
4436 KB |
Output is correct |
63 |
Correct |
1228 ms |
64456 KB |
Output is correct |
64 |
Correct |
565 ms |
35180 KB |
Output is correct |
65 |
Correct |
177 ms |
12032 KB |
Output is correct |
66 |
Correct |
114 ms |
10616 KB |
Output is correct |
67 |
Correct |
167 ms |
12668 KB |
Output is correct |
68 |
Correct |
74 ms |
8140 KB |
Output is correct |
69 |
Correct |
127 ms |
10160 KB |
Output is correct |
70 |
Correct |
79 ms |
9020 KB |
Output is correct |
71 |
Correct |
1238 ms |
67908 KB |
Output is correct |
72 |
Correct |
85 ms |
9328 KB |
Output is correct |
73 |
Correct |
366 ms |
23304 KB |
Output is correct |
74 |
Correct |
164 ms |
12796 KB |
Output is correct |
75 |
Correct |
1255 ms |
66792 KB |
Output is correct |
76 |
Correct |
48 ms |
7616 KB |
Output is correct |
77 |
Correct |
895 ms |
51244 KB |
Output is correct |
78 |
Correct |
7 ms |
4644 KB |
Output is correct |
79 |
Correct |
1361 ms |
72028 KB |
Output is correct |
80 |
Correct |
147 ms |
12200 KB |
Output is correct |
81 |
Correct |
157 ms |
11592 KB |
Output is correct |
82 |
Correct |
67 ms |
8812 KB |
Output is correct |
83 |
Correct |
21 ms |
6796 KB |
Output is correct |
84 |
Correct |
869 ms |
55528 KB |
Output is correct |
85 |
Correct |
104 ms |
10180 KB |
Output is correct |
86 |
Correct |
75 ms |
8192 KB |
Output is correct |
87 |
Correct |
91 ms |
9592 KB |
Output is correct |
88 |
Correct |
13 ms |
5652 KB |
Output is correct |
89 |
Correct |
166 ms |
12332 KB |
Output is correct |
90 |
Correct |
317 ms |
19332 KB |
Output is correct |
91 |
Correct |
143 ms |
10260 KB |
Output is correct |
92 |
Correct |
1719 ms |
119648 KB |
Output is correct |
93 |
Correct |
220 ms |
14308 KB |
Output is correct |
94 |
Correct |
965 ms |
70124 KB |
Output is correct |
95 |
Correct |
6 ms |
4564 KB |
Output is correct |
96 |
Correct |
10 ms |
4740 KB |
Output is correct |
97 |
Correct |
309 ms |
18284 KB |
Output is correct |
98 |
Correct |
146 ms |
10196 KB |
Output is correct |
99 |
Correct |
136 ms |
10972 KB |
Output is correct |
100 |
Correct |
1689 ms |
127664 KB |
Output is correct |
101 |
Correct |
714 ms |
48032 KB |
Output is correct |
102 |
Correct |
723 ms |
47508 KB |
Output is correct |
103 |
Correct |
1129 ms |
87712 KB |
Output is correct |
104 |
Correct |
1456 ms |
100448 KB |
Output is correct |
105 |
Correct |
81 ms |
8308 KB |
Output is correct |
106 |
Correct |
1155 ms |
88056 KB |
Output is correct |
107 |
Correct |
452 ms |
26260 KB |
Output is correct |
108 |
Correct |
246 ms |
13172 KB |
Output is correct |
109 |
Correct |
17 ms |
6072 KB |
Output is correct |
110 |
Correct |
58 ms |
7808 KB |
Output is correct |
111 |
Correct |
250 ms |
13552 KB |
Output is correct |
112 |
Correct |
1761 ms |
128020 KB |
Output is correct |
113 |
Correct |
1719 ms |
128940 KB |
Output is correct |
114 |
Correct |
220 ms |
13192 KB |
Output is correct |
115 |
Correct |
662 ms |
45852 KB |
Output is correct |
116 |
Correct |
1600 ms |
117260 KB |
Output is correct |
117 |
Correct |
1193 ms |
85596 KB |
Output is correct |
118 |
Correct |
1047 ms |
76520 KB |
Output is correct |
119 |
Correct |
1499 ms |
104392 KB |
Output is correct |
120 |
Correct |
996 ms |
62336 KB |
Output is correct |