#pragma GCC optimize("O2")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#define FOR(i, a, b) for (int i = (a); i <= (b); i++)
#define FOD(i, a, b) for (int i = (a); i >= (b); i--)
#define REP(i, n) for (int i = 0; i < (n); i++)
#define ALL(x) (x).begin(), (x).end()
#define TIME (1.0 * clock() / CLOCKS_PER_SEC)
#define file(name) if (fopen(name".inp", "r")) { freopen(name".inp", "r", stdin); freopen(name".out", "w", stdout); }
#define ii pair <int, int>
#define fi first
#define se second
using namespace std;
const int MAXN = 1e5 + 5;
const int INF = 1e9 + 7;
int N, M, K;
vector <ii> ke[MAXN];
vector <int> Special;
struct SUB2{
int dist[505], C[505][505];
vector <vector <ii>> Dist;
void Dijsktra(int id) {
FOR(i, 1, N) dist[i] = INF;
priority_queue <ii, vector <ii>, greater <ii>> q;
q.emplace(dist[Special[id]] = 0, Special[id]);
while (q.size()) {
int du = q.top().fi, u = q.top().se; q.pop();
if (du != dist[u]) continue;
for (ii x : ke[u]) {
int v = x.fi, w = x.se;
if (dist[v] > du + w) {
dist[v] = du + w;
q.emplace(du + w, v);
}
}
}
FOR(i, 1, N) C[id][i] = dist[i];
for (int x : Special) if (x != Special[id])
Dist[id].emplace_back(dist[x], x);
sort(ALL(Dist[id]));
}
void solve() {
if (N > 500) return;
Dist.resize(K);
REP(i, K) Dijsktra(i);
int ans = INF;
REP(i, K) REP(j, K) if (i != j && C[i][Special[j]] != INF)
REP(t, K) if (i != t && j != t) {
int tmp = C[i][Special[j]];
for (ii x : Dist[t]) if (x.se != Special[i] && x.se != Special[j]) {
tmp += x.fi;
break;
}
ans = min(ans, tmp);
}
cout << ans;
exit(0);
}
}sub2;
struct SUB3{
bool dd[MAXN];
vector <int> Gate;
ii ans[MAXN];
int dist[MAXN], trace[MAXN];
void Dijsktra(int k, int c) {
priority_queue <ii, vector <ii>, greater <ii>> q;
FOR(i, 1, N) dist[i] = 1e18, trace[i] = 0;
for (auto x : Gate) if (((x >> k) & 1) == c)
q.emplace(dist[x] = 0, x), trace[x] = x;
while (q.size()) {
long long du = q.top().fi;
int u = q.top().se;
q.pop();
if (du != dist[u]) continue;
for (auto x : ke[u]) {
int v = x.fi, w = x.se;
if (dist[v] > du + w) {
dist[v] = du + w;
trace[v] = trace[u];
q.emplace(du + w, v);
} else if (dist[v] == du + w) {
if (trace[v] > trace[u]) {
trace[v] = trace[u];
q.emplace(du + w, v);
}
}
}
}
for (auto x : Gate) if (((x >> k) & 1) != c)
ans[x] = min(ans[x], {dist[x], trace[x]});
}
void solve() {
bool ok = 0;
for (int x : Special) dd[x] = 1;
for (int x : Special)
if (ke[x].size() == 1 && dd[ke[x][0].fi] && ke[x][0].se == 1) {
if (ke[ke[x][0].fi].size() != 1) continue;
dd[x] = dd[ke[x][0].fi] = 0;
ok = 1;
break;
}
if (!ok) return;
for (int x : Special) if (dd[x]) Gate.emplace_back(x);
for (int x : Gate) ans[x] = {INF, -1};
FOD(i, 18, 0) REP(c, 2)
Dijsktra(i, c);
int res = INF;
for (int x : Gate) res = min(res, ans[x].fi);
cout << res + 1; exit(0);
}
}sub3;
signed main(void) {
ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
//file("TASK");
cin >> N >> M >> K;
while (M--) {
int u, v, w; cin >> u >> v >> w;
ke[u].emplace_back(v, w);
ke[v].emplace_back(u, w);
}
Special.resize(K);
for (int &x : Special) cin >> x;
sub2.solve();
sub3.solve();
cerr << "Time elapsed: " << TIME << " s.\n";
return (0 ^ 0);
}
Compilation message
RelayMarathon.cpp: In member function 'void SUB3::Dijsktra(int, int)':
RelayMarathon.cpp:72:32: warning: overflow in conversion from 'double' to 'int' changes value from '1.0e+18' to '2147483647' [-Woverflow]
72 | FOR(i, 1, N) dist[i] = 1e18, trace[i] = 0;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
3 ms |
3540 KB |
Output is correct |
3 |
Correct |
2 ms |
3472 KB |
Output is correct |
4 |
Correct |
2 ms |
3412 KB |
Output is correct |
5 |
Correct |
2 ms |
3412 KB |
Output is correct |
6 |
Correct |
3 ms |
3476 KB |
Output is correct |
7 |
Correct |
3 ms |
3540 KB |
Output is correct |
8 |
Correct |
3 ms |
3480 KB |
Output is correct |
9 |
Correct |
3 ms |
3464 KB |
Output is correct |
10 |
Correct |
2 ms |
3412 KB |
Output is correct |
11 |
Correct |
3 ms |
3540 KB |
Output is correct |
12 |
Correct |
2 ms |
3540 KB |
Output is correct |
13 |
Correct |
3 ms |
3608 KB |
Output is correct |
14 |
Correct |
2 ms |
3472 KB |
Output is correct |
15 |
Correct |
3 ms |
3608 KB |
Output is correct |
16 |
Correct |
2 ms |
3412 KB |
Output is correct |
17 |
Correct |
3 ms |
3540 KB |
Output is correct |
18 |
Correct |
2 ms |
3412 KB |
Output is correct |
19 |
Correct |
3 ms |
3608 KB |
Output is correct |
20 |
Correct |
3 ms |
3540 KB |
Output is correct |
21 |
Correct |
3 ms |
3540 KB |
Output is correct |
22 |
Correct |
2 ms |
3472 KB |
Output is correct |
23 |
Correct |
3 ms |
3604 KB |
Output is correct |
24 |
Correct |
2 ms |
3472 KB |
Output is correct |
25 |
Correct |
2 ms |
3412 KB |
Output is correct |
26 |
Correct |
2 ms |
3412 KB |
Output is correct |
27 |
Correct |
3 ms |
3412 KB |
Output is correct |
28 |
Correct |
3 ms |
3540 KB |
Output is correct |
29 |
Correct |
3 ms |
3540 KB |
Output is correct |
30 |
Correct |
3 ms |
3540 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
3 ms |
3540 KB |
Output is correct |
3 |
Correct |
2 ms |
3472 KB |
Output is correct |
4 |
Correct |
2 ms |
3412 KB |
Output is correct |
5 |
Correct |
2 ms |
3412 KB |
Output is correct |
6 |
Correct |
3 ms |
3476 KB |
Output is correct |
7 |
Correct |
3 ms |
3540 KB |
Output is correct |
8 |
Correct |
3 ms |
3480 KB |
Output is correct |
9 |
Correct |
3 ms |
3464 KB |
Output is correct |
10 |
Correct |
2 ms |
3412 KB |
Output is correct |
11 |
Correct |
3 ms |
3540 KB |
Output is correct |
12 |
Correct |
2 ms |
3540 KB |
Output is correct |
13 |
Correct |
3 ms |
3608 KB |
Output is correct |
14 |
Correct |
2 ms |
3472 KB |
Output is correct |
15 |
Correct |
3 ms |
3608 KB |
Output is correct |
16 |
Correct |
2 ms |
3412 KB |
Output is correct |
17 |
Correct |
3 ms |
3540 KB |
Output is correct |
18 |
Correct |
2 ms |
3412 KB |
Output is correct |
19 |
Correct |
3 ms |
3608 KB |
Output is correct |
20 |
Correct |
3 ms |
3540 KB |
Output is correct |
21 |
Correct |
3 ms |
3540 KB |
Output is correct |
22 |
Correct |
2 ms |
3472 KB |
Output is correct |
23 |
Correct |
3 ms |
3604 KB |
Output is correct |
24 |
Correct |
2 ms |
3472 KB |
Output is correct |
25 |
Correct |
2 ms |
3412 KB |
Output is correct |
26 |
Correct |
2 ms |
3412 KB |
Output is correct |
27 |
Correct |
3 ms |
3412 KB |
Output is correct |
28 |
Correct |
3 ms |
3540 KB |
Output is correct |
29 |
Correct |
3 ms |
3540 KB |
Output is correct |
30 |
Correct |
3 ms |
3540 KB |
Output is correct |
31 |
Correct |
2 ms |
3412 KB |
Output is correct |
32 |
Correct |
4 ms |
3412 KB |
Output is correct |
33 |
Correct |
3 ms |
3412 KB |
Output is correct |
34 |
Correct |
2 ms |
3472 KB |
Output is correct |
35 |
Correct |
2 ms |
3468 KB |
Output is correct |
36 |
Correct |
126 ms |
5316 KB |
Output is correct |
37 |
Correct |
356 ms |
6608 KB |
Output is correct |
38 |
Correct |
4 ms |
3480 KB |
Output is correct |
39 |
Correct |
37 ms |
6728 KB |
Output is correct |
40 |
Correct |
136 ms |
5948 KB |
Output is correct |
41 |
Correct |
3 ms |
3540 KB |
Output is correct |
42 |
Correct |
243 ms |
6320 KB |
Output is correct |
43 |
Correct |
95 ms |
5244 KB |
Output is correct |
44 |
Correct |
3 ms |
3480 KB |
Output is correct |
45 |
Correct |
2 ms |
3412 KB |
Output is correct |
46 |
Correct |
359 ms |
9348 KB |
Output is correct |
47 |
Correct |
161 ms |
5612 KB |
Output is correct |
48 |
Correct |
50 ms |
6784 KB |
Output is correct |
49 |
Correct |
177 ms |
8204 KB |
Output is correct |
50 |
Correct |
3 ms |
3480 KB |
Output is correct |
51 |
Correct |
108 ms |
5428 KB |
Output is correct |
52 |
Correct |
4 ms |
3540 KB |
Output is correct |
53 |
Correct |
36 ms |
5216 KB |
Output is correct |
54 |
Correct |
50 ms |
6832 KB |
Output is correct |
55 |
Correct |
3 ms |
3412 KB |
Output is correct |
56 |
Correct |
2 ms |
3472 KB |
Output is correct |
57 |
Correct |
3 ms |
3476 KB |
Output is correct |
58 |
Correct |
140 ms |
7668 KB |
Output is correct |
59 |
Correct |
3 ms |
3412 KB |
Output is correct |
60 |
Correct |
141 ms |
5448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
371 ms |
7092 KB |
Output is correct |
2 |
Correct |
7 ms |
4692 KB |
Output is correct |
3 |
Correct |
4083 ms |
66736 KB |
Output is correct |
4 |
Correct |
2896 ms |
34532 KB |
Output is correct |
5 |
Correct |
1217 ms |
10684 KB |
Output is correct |
6 |
Correct |
943 ms |
9488 KB |
Output is correct |
7 |
Correct |
1417 ms |
11308 KB |
Output is correct |
8 |
Correct |
410 ms |
7168 KB |
Output is correct |
9 |
Correct |
1062 ms |
8960 KB |
Output is correct |
10 |
Correct |
761 ms |
7752 KB |
Output is correct |
11 |
Correct |
4540 ms |
68952 KB |
Output is correct |
12 |
Correct |
733 ms |
8008 KB |
Output is correct |
13 |
Correct |
2066 ms |
23948 KB |
Output is correct |
14 |
Correct |
1457 ms |
11628 KB |
Output is correct |
15 |
Correct |
4727 ms |
67848 KB |
Output is correct |
16 |
Correct |
276 ms |
6684 KB |
Output is correct |
17 |
Correct |
3931 ms |
51432 KB |
Output is correct |
18 |
Correct |
12 ms |
4820 KB |
Output is correct |
19 |
Correct |
5259 ms |
72332 KB |
Output is correct |
20 |
Correct |
1479 ms |
11152 KB |
Output is correct |
21 |
Correct |
1365 ms |
10272 KB |
Output is correct |
22 |
Correct |
646 ms |
7676 KB |
Output is correct |
23 |
Correct |
28 ms |
6152 KB |
Output is correct |
24 |
Correct |
3530 ms |
57816 KB |
Output is correct |
25 |
Correct |
1004 ms |
8988 KB |
Output is correct |
26 |
Correct |
418 ms |
7132 KB |
Output is correct |
27 |
Correct |
694 ms |
8296 KB |
Output is correct |
28 |
Correct |
17 ms |
5460 KB |
Output is correct |
29 |
Correct |
1292 ms |
11200 KB |
Output is correct |
30 |
Correct |
2072 ms |
18244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
3412 KB |
Output is correct |
2 |
Correct |
3 ms |
3540 KB |
Output is correct |
3 |
Correct |
2 ms |
3472 KB |
Output is correct |
4 |
Correct |
2 ms |
3412 KB |
Output is correct |
5 |
Correct |
2 ms |
3412 KB |
Output is correct |
6 |
Correct |
3 ms |
3476 KB |
Output is correct |
7 |
Correct |
3 ms |
3540 KB |
Output is correct |
8 |
Correct |
3 ms |
3480 KB |
Output is correct |
9 |
Correct |
3 ms |
3464 KB |
Output is correct |
10 |
Correct |
2 ms |
3412 KB |
Output is correct |
11 |
Correct |
3 ms |
3540 KB |
Output is correct |
12 |
Correct |
2 ms |
3540 KB |
Output is correct |
13 |
Correct |
3 ms |
3608 KB |
Output is correct |
14 |
Correct |
2 ms |
3472 KB |
Output is correct |
15 |
Correct |
3 ms |
3608 KB |
Output is correct |
16 |
Correct |
2 ms |
3412 KB |
Output is correct |
17 |
Correct |
3 ms |
3540 KB |
Output is correct |
18 |
Correct |
2 ms |
3412 KB |
Output is correct |
19 |
Correct |
3 ms |
3608 KB |
Output is correct |
20 |
Correct |
3 ms |
3540 KB |
Output is correct |
21 |
Correct |
3 ms |
3540 KB |
Output is correct |
22 |
Correct |
2 ms |
3472 KB |
Output is correct |
23 |
Correct |
3 ms |
3604 KB |
Output is correct |
24 |
Correct |
2 ms |
3472 KB |
Output is correct |
25 |
Correct |
2 ms |
3412 KB |
Output is correct |
26 |
Correct |
2 ms |
3412 KB |
Output is correct |
27 |
Correct |
3 ms |
3412 KB |
Output is correct |
28 |
Correct |
3 ms |
3540 KB |
Output is correct |
29 |
Correct |
3 ms |
3540 KB |
Output is correct |
30 |
Correct |
3 ms |
3540 KB |
Output is correct |
31 |
Correct |
2 ms |
3412 KB |
Output is correct |
32 |
Correct |
4 ms |
3412 KB |
Output is correct |
33 |
Correct |
3 ms |
3412 KB |
Output is correct |
34 |
Correct |
2 ms |
3472 KB |
Output is correct |
35 |
Correct |
2 ms |
3468 KB |
Output is correct |
36 |
Correct |
126 ms |
5316 KB |
Output is correct |
37 |
Correct |
356 ms |
6608 KB |
Output is correct |
38 |
Correct |
4 ms |
3480 KB |
Output is correct |
39 |
Correct |
37 ms |
6728 KB |
Output is correct |
40 |
Correct |
136 ms |
5948 KB |
Output is correct |
41 |
Correct |
3 ms |
3540 KB |
Output is correct |
42 |
Correct |
243 ms |
6320 KB |
Output is correct |
43 |
Correct |
95 ms |
5244 KB |
Output is correct |
44 |
Correct |
3 ms |
3480 KB |
Output is correct |
45 |
Correct |
2 ms |
3412 KB |
Output is correct |
46 |
Correct |
359 ms |
9348 KB |
Output is correct |
47 |
Correct |
161 ms |
5612 KB |
Output is correct |
48 |
Correct |
50 ms |
6784 KB |
Output is correct |
49 |
Correct |
177 ms |
8204 KB |
Output is correct |
50 |
Correct |
3 ms |
3480 KB |
Output is correct |
51 |
Correct |
108 ms |
5428 KB |
Output is correct |
52 |
Correct |
4 ms |
3540 KB |
Output is correct |
53 |
Correct |
36 ms |
5216 KB |
Output is correct |
54 |
Correct |
50 ms |
6832 KB |
Output is correct |
55 |
Correct |
3 ms |
3412 KB |
Output is correct |
56 |
Correct |
2 ms |
3472 KB |
Output is correct |
57 |
Correct |
3 ms |
3476 KB |
Output is correct |
58 |
Correct |
140 ms |
7668 KB |
Output is correct |
59 |
Correct |
3 ms |
3412 KB |
Output is correct |
60 |
Correct |
141 ms |
5448 KB |
Output is correct |
61 |
Correct |
371 ms |
7092 KB |
Output is correct |
62 |
Correct |
7 ms |
4692 KB |
Output is correct |
63 |
Correct |
4083 ms |
66736 KB |
Output is correct |
64 |
Correct |
2896 ms |
34532 KB |
Output is correct |
65 |
Correct |
1217 ms |
10684 KB |
Output is correct |
66 |
Correct |
943 ms |
9488 KB |
Output is correct |
67 |
Correct |
1417 ms |
11308 KB |
Output is correct |
68 |
Correct |
410 ms |
7168 KB |
Output is correct |
69 |
Correct |
1062 ms |
8960 KB |
Output is correct |
70 |
Correct |
761 ms |
7752 KB |
Output is correct |
71 |
Correct |
4540 ms |
68952 KB |
Output is correct |
72 |
Correct |
733 ms |
8008 KB |
Output is correct |
73 |
Correct |
2066 ms |
23948 KB |
Output is correct |
74 |
Correct |
1457 ms |
11628 KB |
Output is correct |
75 |
Correct |
4727 ms |
67848 KB |
Output is correct |
76 |
Correct |
276 ms |
6684 KB |
Output is correct |
77 |
Correct |
3931 ms |
51432 KB |
Output is correct |
78 |
Correct |
12 ms |
4820 KB |
Output is correct |
79 |
Correct |
5259 ms |
72332 KB |
Output is correct |
80 |
Correct |
1479 ms |
11152 KB |
Output is correct |
81 |
Correct |
1365 ms |
10272 KB |
Output is correct |
82 |
Correct |
646 ms |
7676 KB |
Output is correct |
83 |
Correct |
28 ms |
6152 KB |
Output is correct |
84 |
Correct |
3530 ms |
57816 KB |
Output is correct |
85 |
Correct |
1004 ms |
8988 KB |
Output is correct |
86 |
Correct |
418 ms |
7132 KB |
Output is correct |
87 |
Correct |
694 ms |
8296 KB |
Output is correct |
88 |
Correct |
17 ms |
5460 KB |
Output is correct |
89 |
Correct |
1292 ms |
11200 KB |
Output is correct |
90 |
Correct |
2072 ms |
18244 KB |
Output is correct |
91 |
Incorrect |
72 ms |
9044 KB |
Output isn't correct |
92 |
Halted |
0 ms |
0 KB |
- |