#include <bits/stdc++.h>
using namespace std;
const long long MAX = 0x3f3f3f3f3f3f3f3f;
template<typename T>
bool minimize(T&x,T y) {
return x > y ? x = y, true : false;
}
struct FourMin {
pair<long long, int> v[4];
FourMin() {
for (int i = 0; i < 4; ++i) {
v[i] = make_pair(MAX, -1);
}
}
FourMin operator + (long long x) const {
FourMin tmp;
for (int i = 0; i < 4; ++i) {
if (v[i].second != -1) {
tmp.v[i].first = v[i].first + x;
tmp.v[i].second = v[i].second;
}
}
return tmp;
}
bool update(const FourMin& val) {
vector<pair<long long, int>> tmp;
for (int i = 0; i < 4; ++i) {
tmp.push_back(v[i]);
tmp.push_back(val.v[i]);
}
sort(tmp.begin(),tmp.end());
bool flag = false;
int j = 0;
for (int i = 0; i < (int)tmp.size(); ++i) {
bool found = false;
for (int k = 0; k < j; ++k) {
if (v[k].second == tmp[i].second && tmp[i].second != -1) {
found = true;
break;
}
}
if (!found) {
flag |= v[j] > tmp[i];
v[j++] = tmp[i];
if (j == 4) {
break;
}
}
}
return flag;
}
void update(const pair<long long, int>& x) {
vector<pair<long long, int>> tmp;
for (int i = 0; i < 4; ++i) {
tmp.push_back(v[i]);
}
tmp.push_back(x);
sort(tmp.begin(),tmp.end());
int j = 0;
for (int i = 0; i < (int)tmp.size(); ++i) {
bool found = false;
for (int k = 0; k < j; ++k) {
if (v[k].second == tmp[i].second && tmp[i].second != -1) {
found = true;
break;
}
}
if (!found) {
v[j++] = tmp[i];
if (j == 4) {
break;
}
}
}
}
bool operator < (const FourMin& val) const {
for (int i = 0; i < 4; ++i) {
if (v[i].first != val.v[i].first) {
return v[i].first > val.v[i].first;
}
}
return true;
}
bool operator != (const FourMin& val) const {
for (int i = 0; i < 4; ++i) {
if (v[i] != val.v[i]) {
return true;
}
}
return false;
}
friend ostream& operator << (ostream& os, const FourMin& val) {
for (int i = 0; i < 4; ++i) {
os << i << " -> " << val.v[i].first << ' ' << val.v[i].second << '\n';
os << "------\n";
}
return os;
}
};
const int N = 1e5+7;
int numNode, numEdge, numSpec;
vector<pair<int,int>> adj[N];
vector<int> specs;
FourMin dist[N];
long long res;
int src[2], snk[2];
vector<pair<long long, pair<int,int>>> candidates;
void dijkstra() {
priority_queue<pair<FourMin, int>> heap;
for (int x : specs) {
dist[x].v[0] = make_pair(0, x);
heap.emplace(dist[x], x);
}
while (!heap.empty()) {
int u = heap.top().second;
FourMin w = heap.top().first;
// cerr << " >> " << u << '\n';
heap.pop();
if (dist[u] != w) {
continue;
}
for (pair<int,int> e : adj[u]) {
int v = e.first;
int w = e.second;
// cerr << u << ": \n" << (dist[u]);
// cerr << v << ": \n" << dist[v];
if (dist[v].update(dist[u] + w)) {
// cerr << v << ": \n" << dist[v];
heap.emplace(dist[v], v);
}
}
}
}
bool deepdiff(const pair<int,int>& a, const pair<int,int>& b) {
return a.first != b.first && a.first != b.second && a.second != b.first && a.second != b.second;
}
void processCaseOne() {
pair<int,int> clos = candidates[0].second;
// cerr << " >> ";
// cerr << clos.first << ' ' << clos.second << '\n';
for (int i = 1; i < (int)candidates.size(); ++i) {
if (deepdiff(clos, candidates[i].second)) {
res = candidates[0].first + candidates[i].first;
src[0] = clos.first;
snk[0] = clos.second;
src[1] = candidates[i].second.first;
snk[1] = candidates[i].second.second;
return;
}
}
}
void updateResult(long long r, int _src1, int _snk1, int _src2, int _snk2) {
if (minimize(res, r)) {
src[0] = _src1;
snk[0] = _snk1;
src[1] = _src2;
snk[1] = _snk2;
}
}
void processCaseTwo() {
int beg[2];
beg[0] = candidates[0].second.first;
beg[1] = candidates[0].second.second;
FourMin fm[2];
for (int i = 0; i < (int)candidates.size(); ++i) {
for (int j = 0; j < 2; ++j) {
if (candidates[i].second.first == beg[j] && candidates[i].second.second != beg[!j]) {
fm[j].update(make_pair(candidates[i].first, candidates[i].second.second));
} else if (candidates[i].second.second == beg[j] && candidates[i].second.first != beg[!j]) {
fm[j].update(make_pair(candidates[i].first, candidates[i].second.first));
}
}
}
for (int i = 0; i < 4; ++i) {
for (int j = 0; j < 4; ++j) {
if (fm[0].v[i].second != fm[1].v[j].second) {
updateResult(fm[0].v[i].first+fm[1].v[j].first, beg[0], fm[0].v[i].second, beg[1], fm[1].v[j].second);
}
}
}
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
#ifdef NHPHUCQT
freopen("marathon.inp", "r", stdin);
freopen("marathon.out", "w", stdout);
#endif
cin >> numNode >> numEdge >> numSpec;
for (int i = 0; i < numEdge; ++i) {
int x, y, w;
cin >> x >> y >> w;
adj[x].emplace_back(y, w);
adj[y].emplace_back(x, w);
}
for (int i = 0; i < numSpec; ++i) {
int x;
cin >> x;
specs.push_back(x);
}
dijkstra();
// for (int i = 1; i <= numNode; ++i) {
// for (int j = 0; j < 4; ++j) {
// cerr << i << " : " << j << " -> " << dist[i].v[j].first << ' ' << dist[i].v[j].second << '\n';
// }
// }
long long mins = MAX;
int clos[2];
for (int i = 1; i <= numNode; ++i) {
for (int j = 0; j < 4; ++j)
for (int k = j+1; k < 4; ++k) {
if (dist[i].v[j].second != -1 && dist[i].v[k].second != -1) {
candidates.emplace_back(dist[i].v[j].first+dist[i].v[k].first, make_pair(dist[i].v[j].second, dist[i].v[k].second));
}
}
}
sort(candidates.begin(), candidates.end());
processCaseOne();
processCaseTwo();
cout << res;
// for (int i = 0; i < 2; ++i) {
// cerr << " >>> " << src[i] << ' ' << snk[i] << '\n';
// }
return 0;
}
Compilation message
RelayMarathon.cpp: In function 'int main()':
RelayMarathon.cpp:227:15: warning: unused variable 'mins' [-Wunused-variable]
227 | long long mins = MAX;
| ^~~~
RelayMarathon.cpp:228:9: warning: unused variable 'clos' [-Wunused-variable]
228 | int clos[2];
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8916 KB |
Output is correct |
2 |
Correct |
8 ms |
8932 KB |
Output is correct |
3 |
Correct |
5 ms |
8932 KB |
Output is correct |
4 |
Correct |
6 ms |
8892 KB |
Output is correct |
5 |
Correct |
5 ms |
8936 KB |
Output is correct |
6 |
Correct |
5 ms |
8916 KB |
Output is correct |
7 |
Correct |
6 ms |
9044 KB |
Output is correct |
8 |
Correct |
6 ms |
8928 KB |
Output is correct |
9 |
Correct |
5 ms |
8916 KB |
Output is correct |
10 |
Correct |
4 ms |
8916 KB |
Output is correct |
11 |
Correct |
5 ms |
8916 KB |
Output is correct |
12 |
Correct |
4 ms |
8940 KB |
Output is correct |
13 |
Correct |
5 ms |
8944 KB |
Output is correct |
14 |
Correct |
7 ms |
8940 KB |
Output is correct |
15 |
Correct |
5 ms |
8960 KB |
Output is correct |
16 |
Correct |
5 ms |
8936 KB |
Output is correct |
17 |
Correct |
5 ms |
8944 KB |
Output is correct |
18 |
Correct |
5 ms |
8916 KB |
Output is correct |
19 |
Correct |
6 ms |
9044 KB |
Output is correct |
20 |
Correct |
5 ms |
8916 KB |
Output is correct |
21 |
Correct |
6 ms |
8916 KB |
Output is correct |
22 |
Correct |
5 ms |
8916 KB |
Output is correct |
23 |
Correct |
6 ms |
8916 KB |
Output is correct |
24 |
Correct |
5 ms |
8916 KB |
Output is correct |
25 |
Correct |
5 ms |
8836 KB |
Output is correct |
26 |
Correct |
5 ms |
8916 KB |
Output is correct |
27 |
Correct |
5 ms |
8884 KB |
Output is correct |
28 |
Correct |
8 ms |
8964 KB |
Output is correct |
29 |
Correct |
8 ms |
8948 KB |
Output is correct |
30 |
Correct |
6 ms |
9044 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8916 KB |
Output is correct |
2 |
Correct |
8 ms |
8932 KB |
Output is correct |
3 |
Correct |
5 ms |
8932 KB |
Output is correct |
4 |
Correct |
6 ms |
8892 KB |
Output is correct |
5 |
Correct |
5 ms |
8936 KB |
Output is correct |
6 |
Correct |
5 ms |
8916 KB |
Output is correct |
7 |
Correct |
6 ms |
9044 KB |
Output is correct |
8 |
Correct |
6 ms |
8928 KB |
Output is correct |
9 |
Correct |
5 ms |
8916 KB |
Output is correct |
10 |
Correct |
4 ms |
8916 KB |
Output is correct |
11 |
Correct |
5 ms |
8916 KB |
Output is correct |
12 |
Correct |
4 ms |
8940 KB |
Output is correct |
13 |
Correct |
5 ms |
8944 KB |
Output is correct |
14 |
Correct |
7 ms |
8940 KB |
Output is correct |
15 |
Correct |
5 ms |
8960 KB |
Output is correct |
16 |
Correct |
5 ms |
8936 KB |
Output is correct |
17 |
Correct |
5 ms |
8944 KB |
Output is correct |
18 |
Correct |
5 ms |
8916 KB |
Output is correct |
19 |
Correct |
6 ms |
9044 KB |
Output is correct |
20 |
Correct |
5 ms |
8916 KB |
Output is correct |
21 |
Correct |
6 ms |
8916 KB |
Output is correct |
22 |
Correct |
5 ms |
8916 KB |
Output is correct |
23 |
Correct |
6 ms |
8916 KB |
Output is correct |
24 |
Correct |
5 ms |
8916 KB |
Output is correct |
25 |
Correct |
5 ms |
8836 KB |
Output is correct |
26 |
Correct |
5 ms |
8916 KB |
Output is correct |
27 |
Correct |
5 ms |
8884 KB |
Output is correct |
28 |
Correct |
8 ms |
8964 KB |
Output is correct |
29 |
Correct |
8 ms |
8948 KB |
Output is correct |
30 |
Correct |
6 ms |
9044 KB |
Output is correct |
31 |
Correct |
6 ms |
8916 KB |
Output is correct |
32 |
Correct |
6 ms |
9068 KB |
Output is correct |
33 |
Correct |
6 ms |
9044 KB |
Output is correct |
34 |
Correct |
5 ms |
8916 KB |
Output is correct |
35 |
Correct |
5 ms |
8916 KB |
Output is correct |
36 |
Correct |
10 ms |
9416 KB |
Output is correct |
37 |
Correct |
12 ms |
9544 KB |
Output is correct |
38 |
Correct |
6 ms |
9044 KB |
Output is correct |
39 |
Correct |
94 ms |
12752 KB |
Output is correct |
40 |
Correct |
26 ms |
10052 KB |
Output is correct |
41 |
Correct |
8 ms |
9192 KB |
Output is correct |
42 |
Correct |
24 ms |
10080 KB |
Output is correct |
43 |
Correct |
9 ms |
9320 KB |
Output is correct |
44 |
Correct |
7 ms |
9036 KB |
Output is correct |
45 |
Correct |
5 ms |
8916 KB |
Output is correct |
46 |
Correct |
109 ms |
13128 KB |
Output is correct |
47 |
Correct |
20 ms |
9800 KB |
Output is correct |
48 |
Correct |
99 ms |
12668 KB |
Output is correct |
49 |
Correct |
92 ms |
12864 KB |
Output is correct |
50 |
Correct |
6 ms |
9080 KB |
Output is correct |
51 |
Correct |
6 ms |
9044 KB |
Output is correct |
52 |
Correct |
7 ms |
9044 KB |
Output is correct |
53 |
Correct |
51 ms |
11296 KB |
Output is correct |
54 |
Correct |
92 ms |
12852 KB |
Output is correct |
55 |
Correct |
7 ms |
8996 KB |
Output is correct |
56 |
Correct |
6 ms |
8932 KB |
Output is correct |
57 |
Correct |
6 ms |
9060 KB |
Output is correct |
58 |
Correct |
124 ms |
13232 KB |
Output is correct |
59 |
Correct |
5 ms |
8932 KB |
Output is correct |
60 |
Correct |
7 ms |
9204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
150 ms |
21844 KB |
Output is correct |
2 |
Correct |
9 ms |
9428 KB |
Output is correct |
3 |
Correct |
3817 ms |
147784 KB |
Output is correct |
4 |
Correct |
1835 ms |
87168 KB |
Output is correct |
5 |
Correct |
578 ms |
42464 KB |
Output is correct |
6 |
Correct |
440 ms |
40116 KB |
Output is correct |
7 |
Correct |
589 ms |
43660 KB |
Output is correct |
8 |
Correct |
173 ms |
22056 KB |
Output is correct |
9 |
Correct |
346 ms |
39412 KB |
Output is correct |
10 |
Correct |
236 ms |
25732 KB |
Output is correct |
11 |
Correct |
4215 ms |
150668 KB |
Output is correct |
12 |
Correct |
276 ms |
26172 KB |
Output is correct |
13 |
Correct |
1206 ms |
71504 KB |
Output is correct |
14 |
Correct |
541 ms |
43696 KB |
Output is correct |
15 |
Correct |
4179 ms |
149268 KB |
Output is correct |
16 |
Correct |
121 ms |
16532 KB |
Output is correct |
17 |
Correct |
3432 ms |
130264 KB |
Output is correct |
18 |
Correct |
11 ms |
9584 KB |
Output is correct |
19 |
Correct |
5303 ms |
152196 KB |
Output is correct |
20 |
Correct |
668 ms |
42912 KB |
Output is correct |
21 |
Correct |
578 ms |
41696 KB |
Output is correct |
22 |
Correct |
273 ms |
23580 KB |
Output is correct |
23 |
Correct |
36 ms |
11384 KB |
Output is correct |
24 |
Correct |
3285 ms |
136996 KB |
Output is correct |
25 |
Correct |
433 ms |
39588 KB |
Output is correct |
26 |
Correct |
215 ms |
22036 KB |
Output is correct |
27 |
Correct |
296 ms |
26384 KB |
Output is correct |
28 |
Correct |
23 ms |
10452 KB |
Output is correct |
29 |
Correct |
612 ms |
43192 KB |
Output is correct |
30 |
Correct |
1271 ms |
64296 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8916 KB |
Output is correct |
2 |
Correct |
8 ms |
8932 KB |
Output is correct |
3 |
Correct |
5 ms |
8932 KB |
Output is correct |
4 |
Correct |
6 ms |
8892 KB |
Output is correct |
5 |
Correct |
5 ms |
8936 KB |
Output is correct |
6 |
Correct |
5 ms |
8916 KB |
Output is correct |
7 |
Correct |
6 ms |
9044 KB |
Output is correct |
8 |
Correct |
6 ms |
8928 KB |
Output is correct |
9 |
Correct |
5 ms |
8916 KB |
Output is correct |
10 |
Correct |
4 ms |
8916 KB |
Output is correct |
11 |
Correct |
5 ms |
8916 KB |
Output is correct |
12 |
Correct |
4 ms |
8940 KB |
Output is correct |
13 |
Correct |
5 ms |
8944 KB |
Output is correct |
14 |
Correct |
7 ms |
8940 KB |
Output is correct |
15 |
Correct |
5 ms |
8960 KB |
Output is correct |
16 |
Correct |
5 ms |
8936 KB |
Output is correct |
17 |
Correct |
5 ms |
8944 KB |
Output is correct |
18 |
Correct |
5 ms |
8916 KB |
Output is correct |
19 |
Correct |
6 ms |
9044 KB |
Output is correct |
20 |
Correct |
5 ms |
8916 KB |
Output is correct |
21 |
Correct |
6 ms |
8916 KB |
Output is correct |
22 |
Correct |
5 ms |
8916 KB |
Output is correct |
23 |
Correct |
6 ms |
8916 KB |
Output is correct |
24 |
Correct |
5 ms |
8916 KB |
Output is correct |
25 |
Correct |
5 ms |
8836 KB |
Output is correct |
26 |
Correct |
5 ms |
8916 KB |
Output is correct |
27 |
Correct |
5 ms |
8884 KB |
Output is correct |
28 |
Correct |
8 ms |
8964 KB |
Output is correct |
29 |
Correct |
8 ms |
8948 KB |
Output is correct |
30 |
Correct |
6 ms |
9044 KB |
Output is correct |
31 |
Correct |
6 ms |
8916 KB |
Output is correct |
32 |
Correct |
6 ms |
9068 KB |
Output is correct |
33 |
Correct |
6 ms |
9044 KB |
Output is correct |
34 |
Correct |
5 ms |
8916 KB |
Output is correct |
35 |
Correct |
5 ms |
8916 KB |
Output is correct |
36 |
Correct |
10 ms |
9416 KB |
Output is correct |
37 |
Correct |
12 ms |
9544 KB |
Output is correct |
38 |
Correct |
6 ms |
9044 KB |
Output is correct |
39 |
Correct |
94 ms |
12752 KB |
Output is correct |
40 |
Correct |
26 ms |
10052 KB |
Output is correct |
41 |
Correct |
8 ms |
9192 KB |
Output is correct |
42 |
Correct |
24 ms |
10080 KB |
Output is correct |
43 |
Correct |
9 ms |
9320 KB |
Output is correct |
44 |
Correct |
7 ms |
9036 KB |
Output is correct |
45 |
Correct |
5 ms |
8916 KB |
Output is correct |
46 |
Correct |
109 ms |
13128 KB |
Output is correct |
47 |
Correct |
20 ms |
9800 KB |
Output is correct |
48 |
Correct |
99 ms |
12668 KB |
Output is correct |
49 |
Correct |
92 ms |
12864 KB |
Output is correct |
50 |
Correct |
6 ms |
9080 KB |
Output is correct |
51 |
Correct |
6 ms |
9044 KB |
Output is correct |
52 |
Correct |
7 ms |
9044 KB |
Output is correct |
53 |
Correct |
51 ms |
11296 KB |
Output is correct |
54 |
Correct |
92 ms |
12852 KB |
Output is correct |
55 |
Correct |
7 ms |
8996 KB |
Output is correct |
56 |
Correct |
6 ms |
8932 KB |
Output is correct |
57 |
Correct |
6 ms |
9060 KB |
Output is correct |
58 |
Correct |
124 ms |
13232 KB |
Output is correct |
59 |
Correct |
5 ms |
8932 KB |
Output is correct |
60 |
Correct |
7 ms |
9204 KB |
Output is correct |
61 |
Correct |
150 ms |
21844 KB |
Output is correct |
62 |
Correct |
9 ms |
9428 KB |
Output is correct |
63 |
Correct |
3817 ms |
147784 KB |
Output is correct |
64 |
Correct |
1835 ms |
87168 KB |
Output is correct |
65 |
Correct |
578 ms |
42464 KB |
Output is correct |
66 |
Correct |
440 ms |
40116 KB |
Output is correct |
67 |
Correct |
589 ms |
43660 KB |
Output is correct |
68 |
Correct |
173 ms |
22056 KB |
Output is correct |
69 |
Correct |
346 ms |
39412 KB |
Output is correct |
70 |
Correct |
236 ms |
25732 KB |
Output is correct |
71 |
Correct |
4215 ms |
150668 KB |
Output is correct |
72 |
Correct |
276 ms |
26172 KB |
Output is correct |
73 |
Correct |
1206 ms |
71504 KB |
Output is correct |
74 |
Correct |
541 ms |
43696 KB |
Output is correct |
75 |
Correct |
4179 ms |
149268 KB |
Output is correct |
76 |
Correct |
121 ms |
16532 KB |
Output is correct |
77 |
Correct |
3432 ms |
130264 KB |
Output is correct |
78 |
Correct |
11 ms |
9584 KB |
Output is correct |
79 |
Correct |
5303 ms |
152196 KB |
Output is correct |
80 |
Correct |
668 ms |
42912 KB |
Output is correct |
81 |
Correct |
578 ms |
41696 KB |
Output is correct |
82 |
Correct |
273 ms |
23580 KB |
Output is correct |
83 |
Correct |
36 ms |
11384 KB |
Output is correct |
84 |
Correct |
3285 ms |
136996 KB |
Output is correct |
85 |
Correct |
433 ms |
39588 KB |
Output is correct |
86 |
Correct |
215 ms |
22036 KB |
Output is correct |
87 |
Correct |
296 ms |
26384 KB |
Output is correct |
88 |
Correct |
23 ms |
10452 KB |
Output is correct |
89 |
Correct |
612 ms |
43192 KB |
Output is correct |
90 |
Correct |
1271 ms |
64296 KB |
Output is correct |
91 |
Correct |
470 ms |
30684 KB |
Output is correct |
92 |
Correct |
4657 ms |
157268 KB |
Output is correct |
93 |
Correct |
681 ms |
43684 KB |
Output is correct |
94 |
Correct |
3138 ms |
126412 KB |
Output is correct |
95 |
Correct |
9 ms |
9556 KB |
Output is correct |
96 |
Correct |
13 ms |
9684 KB |
Output is correct |
97 |
Correct |
781 ms |
47080 KB |
Output is correct |
98 |
Correct |
316 ms |
30612 KB |
Output is correct |
99 |
Correct |
421 ms |
39888 KB |
Output is correct |
100 |
Correct |
5196 ms |
163432 KB |
Output is correct |
101 |
Correct |
2053 ms |
123164 KB |
Output is correct |
102 |
Correct |
1979 ms |
85808 KB |
Output is correct |
103 |
Correct |
2913 ms |
139076 KB |
Output is correct |
104 |
Correct |
3869 ms |
144784 KB |
Output is correct |
105 |
Correct |
193 ms |
23096 KB |
Output is correct |
106 |
Correct |
3267 ms |
139372 KB |
Output is correct |
107 |
Correct |
1117 ms |
66124 KB |
Output is correct |
108 |
Correct |
506 ms |
42464 KB |
Output is correct |
109 |
Correct |
23 ms |
10964 KB |
Output is correct |
110 |
Correct |
158 ms |
21728 KB |
Output is correct |
111 |
Correct |
510 ms |
43028 KB |
Output is correct |
112 |
Correct |
4646 ms |
163524 KB |
Output is correct |
113 |
Correct |
4696 ms |
164284 KB |
Output is correct |
114 |
Correct |
612 ms |
42472 KB |
Output is correct |
115 |
Correct |
1728 ms |
84376 KB |
Output is correct |
116 |
Correct |
4372 ms |
155456 KB |
Output is correct |
117 |
Correct |
2873 ms |
137564 KB |
Output is correct |
118 |
Correct |
2669 ms |
131048 KB |
Output is correct |
119 |
Correct |
4104 ms |
146436 KB |
Output is correct |
120 |
Correct |
2603 ms |
120608 KB |
Output is correct |