#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;
}
int buckSiz;
pair<long long, int> bucket[8];
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) {
buckSiz = 0;
for (int i = 0; i < 4; ++i) {
bucket[buckSiz++] = v[i];
bucket[buckSiz++] = val.v[i];
}
sort(bucket, bucket+buckSiz);
bool flag = false;
int j = 0;
for (int i = 0; i < buckSiz; ++i) {
bool found = false;
for (int k = 0; k < j; ++k) {
if (v[k].second == bucket[i].second && bucket[i].second != -1) {
found = true;
break;
}
}
if (!found) {
flag |= v[j] > bucket[i];
v[j++] = bucket[i];
if (j == 4) {
break;
}
}
}
return flag;
}
void update(const pair<long long, int>& x) {
buckSiz = 0;
for (int i = 0; i < 4; ++i) {
bucket[buckSiz++] = v[i];
}
bucket[buckSiz++] = x;
sort(bucket,bucket+buckSiz);
int j = 0;
for (int i = 0; i < buckSiz; ++i) {
bool found = false;
for (int k = 0; k < j; ++k) {
if (v[k].second == bucket[i].second && bucket[i].second != -1) {
found = true;
break;
}
}
if (!found) {
v[j++] = bucket[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];
int candSiz;
pair<long long, pair<int,int>> candidates[N*4];
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 < candSiz; ++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 < candSiz; ++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);
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';
// }
// }
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[candSiz++] = make_pair(dist[i].v[j].first+dist[i].v[k].first, make_pair(dist[i].v[j].second, dist[i].v[k].second));
}
}
}
sort(candidates, candidates+candSiz);
processCaseOne();
processCaseTwo();
cout << res;
// for (int i = 0; i < 2; ++i) {
// cerr << " >>> " << src[i] << ' ' << snk[i] << '\n';
// }
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8948 KB |
Output is correct |
2 |
Correct |
6 ms |
8976 KB |
Output is correct |
3 |
Correct |
5 ms |
8948 KB |
Output is correct |
4 |
Correct |
4 ms |
8940 KB |
Output is correct |
5 |
Correct |
5 ms |
8988 KB |
Output is correct |
6 |
Correct |
5 ms |
8936 KB |
Output is correct |
7 |
Correct |
6 ms |
8952 KB |
Output is correct |
8 |
Correct |
5 ms |
9044 KB |
Output is correct |
9 |
Correct |
5 ms |
8916 KB |
Output is correct |
10 |
Correct |
5 ms |
8916 KB |
Output is correct |
11 |
Correct |
5 ms |
8916 KB |
Output is correct |
12 |
Correct |
5 ms |
8916 KB |
Output is correct |
13 |
Correct |
5 ms |
9044 KB |
Output is correct |
14 |
Correct |
5 ms |
9036 KB |
Output is correct |
15 |
Correct |
5 ms |
8948 KB |
Output is correct |
16 |
Correct |
5 ms |
8916 KB |
Output is correct |
17 |
Correct |
5 ms |
9016 KB |
Output is correct |
18 |
Correct |
4 ms |
8940 KB |
Output is correct |
19 |
Correct |
6 ms |
9044 KB |
Output is correct |
20 |
Correct |
5 ms |
8916 KB |
Output is correct |
21 |
Correct |
5 ms |
9028 KB |
Output is correct |
22 |
Correct |
5 ms |
8916 KB |
Output is correct |
23 |
Correct |
5 ms |
8956 KB |
Output is correct |
24 |
Correct |
4 ms |
8924 KB |
Output is correct |
25 |
Correct |
5 ms |
8940 KB |
Output is correct |
26 |
Correct |
5 ms |
8944 KB |
Output is correct |
27 |
Correct |
4 ms |
8916 KB |
Output is correct |
28 |
Correct |
5 ms |
9044 KB |
Output is correct |
29 |
Correct |
5 ms |
9044 KB |
Output is correct |
30 |
Correct |
5 ms |
8976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8948 KB |
Output is correct |
2 |
Correct |
6 ms |
8976 KB |
Output is correct |
3 |
Correct |
5 ms |
8948 KB |
Output is correct |
4 |
Correct |
4 ms |
8940 KB |
Output is correct |
5 |
Correct |
5 ms |
8988 KB |
Output is correct |
6 |
Correct |
5 ms |
8936 KB |
Output is correct |
7 |
Correct |
6 ms |
8952 KB |
Output is correct |
8 |
Correct |
5 ms |
9044 KB |
Output is correct |
9 |
Correct |
5 ms |
8916 KB |
Output is correct |
10 |
Correct |
5 ms |
8916 KB |
Output is correct |
11 |
Correct |
5 ms |
8916 KB |
Output is correct |
12 |
Correct |
5 ms |
8916 KB |
Output is correct |
13 |
Correct |
5 ms |
9044 KB |
Output is correct |
14 |
Correct |
5 ms |
9036 KB |
Output is correct |
15 |
Correct |
5 ms |
8948 KB |
Output is correct |
16 |
Correct |
5 ms |
8916 KB |
Output is correct |
17 |
Correct |
5 ms |
9016 KB |
Output is correct |
18 |
Correct |
4 ms |
8940 KB |
Output is correct |
19 |
Correct |
6 ms |
9044 KB |
Output is correct |
20 |
Correct |
5 ms |
8916 KB |
Output is correct |
21 |
Correct |
5 ms |
9028 KB |
Output is correct |
22 |
Correct |
5 ms |
8916 KB |
Output is correct |
23 |
Correct |
5 ms |
8956 KB |
Output is correct |
24 |
Correct |
4 ms |
8924 KB |
Output is correct |
25 |
Correct |
5 ms |
8940 KB |
Output is correct |
26 |
Correct |
5 ms |
8944 KB |
Output is correct |
27 |
Correct |
4 ms |
8916 KB |
Output is correct |
28 |
Correct |
5 ms |
9044 KB |
Output is correct |
29 |
Correct |
5 ms |
9044 KB |
Output is correct |
30 |
Correct |
5 ms |
8976 KB |
Output is correct |
31 |
Correct |
5 ms |
8916 KB |
Output is correct |
32 |
Correct |
5 ms |
9044 KB |
Output is correct |
33 |
Correct |
6 ms |
9088 KB |
Output is correct |
34 |
Correct |
5 ms |
8916 KB |
Output is correct |
35 |
Correct |
7 ms |
8944 KB |
Output is correct |
36 |
Correct |
11 ms |
9544 KB |
Output is correct |
37 |
Correct |
13 ms |
9580 KB |
Output is correct |
38 |
Correct |
6 ms |
9044 KB |
Output is correct |
39 |
Correct |
47 ms |
12844 KB |
Output is correct |
40 |
Correct |
14 ms |
10084 KB |
Output is correct |
41 |
Correct |
6 ms |
9080 KB |
Output is correct |
42 |
Correct |
14 ms |
10052 KB |
Output is correct |
43 |
Correct |
7 ms |
9300 KB |
Output is correct |
44 |
Correct |
8 ms |
9044 KB |
Output is correct |
45 |
Correct |
5 ms |
8952 KB |
Output is correct |
46 |
Correct |
56 ms |
13036 KB |
Output is correct |
47 |
Correct |
12 ms |
9832 KB |
Output is correct |
48 |
Correct |
42 ms |
12748 KB |
Output is correct |
49 |
Correct |
44 ms |
12852 KB |
Output is correct |
50 |
Correct |
6 ms |
9080 KB |
Output is correct |
51 |
Correct |
6 ms |
9076 KB |
Output is correct |
52 |
Correct |
6 ms |
9044 KB |
Output is correct |
53 |
Correct |
28 ms |
11332 KB |
Output is correct |
54 |
Correct |
44 ms |
12848 KB |
Output is correct |
55 |
Correct |
7 ms |
9044 KB |
Output is correct |
56 |
Correct |
6 ms |
8916 KB |
Output is correct |
57 |
Correct |
5 ms |
9044 KB |
Output is correct |
58 |
Correct |
61 ms |
13272 KB |
Output is correct |
59 |
Correct |
5 ms |
8940 KB |
Output is correct |
60 |
Correct |
6 ms |
9172 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
135 ms |
17612 KB |
Output is correct |
2 |
Correct |
9 ms |
9428 KB |
Output is correct |
3 |
Correct |
2602 ms |
145332 KB |
Output is correct |
4 |
Correct |
1311 ms |
87516 KB |
Output is correct |
5 |
Correct |
500 ms |
26380 KB |
Output is correct |
6 |
Correct |
322 ms |
21764 KB |
Output is correct |
7 |
Correct |
459 ms |
27308 KB |
Output is correct |
8 |
Correct |
145 ms |
18256 KB |
Output is correct |
9 |
Correct |
298 ms |
21296 KB |
Output is correct |
10 |
Correct |
194 ms |
19844 KB |
Output is correct |
11 |
Correct |
2574 ms |
147820 KB |
Output is correct |
12 |
Correct |
231 ms |
20252 KB |
Output is correct |
13 |
Correct |
907 ms |
71512 KB |
Output is correct |
14 |
Correct |
471 ms |
36728 KB |
Output is correct |
15 |
Correct |
2597 ms |
146412 KB |
Output is correct |
16 |
Correct |
102 ms |
15780 KB |
Output is correct |
17 |
Correct |
2041 ms |
129116 KB |
Output is correct |
18 |
Correct |
10 ms |
9556 KB |
Output is correct |
19 |
Correct |
2828 ms |
149044 KB |
Output is correct |
20 |
Correct |
402 ms |
26816 KB |
Output is correct |
21 |
Correct |
361 ms |
25956 KB |
Output is correct |
22 |
Correct |
191 ms |
19588 KB |
Output is correct |
23 |
Correct |
22 ms |
11440 KB |
Output is correct |
24 |
Correct |
1750 ms |
135796 KB |
Output is correct |
25 |
Correct |
288 ms |
21468 KB |
Output is correct |
26 |
Correct |
136 ms |
18136 KB |
Output is correct |
27 |
Correct |
231 ms |
20472 KB |
Output is correct |
28 |
Correct |
20 ms |
10444 KB |
Output is correct |
29 |
Correct |
445 ms |
27056 KB |
Output is correct |
30 |
Correct |
837 ms |
64284 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
8948 KB |
Output is correct |
2 |
Correct |
6 ms |
8976 KB |
Output is correct |
3 |
Correct |
5 ms |
8948 KB |
Output is correct |
4 |
Correct |
4 ms |
8940 KB |
Output is correct |
5 |
Correct |
5 ms |
8988 KB |
Output is correct |
6 |
Correct |
5 ms |
8936 KB |
Output is correct |
7 |
Correct |
6 ms |
8952 KB |
Output is correct |
8 |
Correct |
5 ms |
9044 KB |
Output is correct |
9 |
Correct |
5 ms |
8916 KB |
Output is correct |
10 |
Correct |
5 ms |
8916 KB |
Output is correct |
11 |
Correct |
5 ms |
8916 KB |
Output is correct |
12 |
Correct |
5 ms |
8916 KB |
Output is correct |
13 |
Correct |
5 ms |
9044 KB |
Output is correct |
14 |
Correct |
5 ms |
9036 KB |
Output is correct |
15 |
Correct |
5 ms |
8948 KB |
Output is correct |
16 |
Correct |
5 ms |
8916 KB |
Output is correct |
17 |
Correct |
5 ms |
9016 KB |
Output is correct |
18 |
Correct |
4 ms |
8940 KB |
Output is correct |
19 |
Correct |
6 ms |
9044 KB |
Output is correct |
20 |
Correct |
5 ms |
8916 KB |
Output is correct |
21 |
Correct |
5 ms |
9028 KB |
Output is correct |
22 |
Correct |
5 ms |
8916 KB |
Output is correct |
23 |
Correct |
5 ms |
8956 KB |
Output is correct |
24 |
Correct |
4 ms |
8924 KB |
Output is correct |
25 |
Correct |
5 ms |
8940 KB |
Output is correct |
26 |
Correct |
5 ms |
8944 KB |
Output is correct |
27 |
Correct |
4 ms |
8916 KB |
Output is correct |
28 |
Correct |
5 ms |
9044 KB |
Output is correct |
29 |
Correct |
5 ms |
9044 KB |
Output is correct |
30 |
Correct |
5 ms |
8976 KB |
Output is correct |
31 |
Correct |
5 ms |
8916 KB |
Output is correct |
32 |
Correct |
5 ms |
9044 KB |
Output is correct |
33 |
Correct |
6 ms |
9088 KB |
Output is correct |
34 |
Correct |
5 ms |
8916 KB |
Output is correct |
35 |
Correct |
7 ms |
8944 KB |
Output is correct |
36 |
Correct |
11 ms |
9544 KB |
Output is correct |
37 |
Correct |
13 ms |
9580 KB |
Output is correct |
38 |
Correct |
6 ms |
9044 KB |
Output is correct |
39 |
Correct |
47 ms |
12844 KB |
Output is correct |
40 |
Correct |
14 ms |
10084 KB |
Output is correct |
41 |
Correct |
6 ms |
9080 KB |
Output is correct |
42 |
Correct |
14 ms |
10052 KB |
Output is correct |
43 |
Correct |
7 ms |
9300 KB |
Output is correct |
44 |
Correct |
8 ms |
9044 KB |
Output is correct |
45 |
Correct |
5 ms |
8952 KB |
Output is correct |
46 |
Correct |
56 ms |
13036 KB |
Output is correct |
47 |
Correct |
12 ms |
9832 KB |
Output is correct |
48 |
Correct |
42 ms |
12748 KB |
Output is correct |
49 |
Correct |
44 ms |
12852 KB |
Output is correct |
50 |
Correct |
6 ms |
9080 KB |
Output is correct |
51 |
Correct |
6 ms |
9076 KB |
Output is correct |
52 |
Correct |
6 ms |
9044 KB |
Output is correct |
53 |
Correct |
28 ms |
11332 KB |
Output is correct |
54 |
Correct |
44 ms |
12848 KB |
Output is correct |
55 |
Correct |
7 ms |
9044 KB |
Output is correct |
56 |
Correct |
6 ms |
8916 KB |
Output is correct |
57 |
Correct |
5 ms |
9044 KB |
Output is correct |
58 |
Correct |
61 ms |
13272 KB |
Output is correct |
59 |
Correct |
5 ms |
8940 KB |
Output is correct |
60 |
Correct |
6 ms |
9172 KB |
Output is correct |
61 |
Correct |
135 ms |
17612 KB |
Output is correct |
62 |
Correct |
9 ms |
9428 KB |
Output is correct |
63 |
Correct |
2602 ms |
145332 KB |
Output is correct |
64 |
Correct |
1311 ms |
87516 KB |
Output is correct |
65 |
Correct |
500 ms |
26380 KB |
Output is correct |
66 |
Correct |
322 ms |
21764 KB |
Output is correct |
67 |
Correct |
459 ms |
27308 KB |
Output is correct |
68 |
Correct |
145 ms |
18256 KB |
Output is correct |
69 |
Correct |
298 ms |
21296 KB |
Output is correct |
70 |
Correct |
194 ms |
19844 KB |
Output is correct |
71 |
Correct |
2574 ms |
147820 KB |
Output is correct |
72 |
Correct |
231 ms |
20252 KB |
Output is correct |
73 |
Correct |
907 ms |
71512 KB |
Output is correct |
74 |
Correct |
471 ms |
36728 KB |
Output is correct |
75 |
Correct |
2597 ms |
146412 KB |
Output is correct |
76 |
Correct |
102 ms |
15780 KB |
Output is correct |
77 |
Correct |
2041 ms |
129116 KB |
Output is correct |
78 |
Correct |
10 ms |
9556 KB |
Output is correct |
79 |
Correct |
2828 ms |
149044 KB |
Output is correct |
80 |
Correct |
402 ms |
26816 KB |
Output is correct |
81 |
Correct |
361 ms |
25956 KB |
Output is correct |
82 |
Correct |
191 ms |
19588 KB |
Output is correct |
83 |
Correct |
22 ms |
11440 KB |
Output is correct |
84 |
Correct |
1750 ms |
135796 KB |
Output is correct |
85 |
Correct |
288 ms |
21468 KB |
Output is correct |
86 |
Correct |
136 ms |
18136 KB |
Output is correct |
87 |
Correct |
231 ms |
20472 KB |
Output is correct |
88 |
Correct |
20 ms |
10444 KB |
Output is correct |
89 |
Correct |
445 ms |
27056 KB |
Output is correct |
90 |
Correct |
837 ms |
64284 KB |
Output is correct |
91 |
Correct |
271 ms |
20960 KB |
Output is correct |
92 |
Correct |
2670 ms |
153744 KB |
Output is correct |
93 |
Correct |
460 ms |
27372 KB |
Output is correct |
94 |
Correct |
1966 ms |
125344 KB |
Output is correct |
95 |
Correct |
11 ms |
9496 KB |
Output is correct |
96 |
Correct |
10 ms |
9684 KB |
Output is correct |
97 |
Correct |
570 ms |
39920 KB |
Output is correct |
98 |
Correct |
262 ms |
20860 KB |
Output is correct |
99 |
Correct |
298 ms |
21508 KB |
Output is correct |
100 |
Correct |
2871 ms |
159672 KB |
Output is correct |
101 |
Correct |
1527 ms |
123348 KB |
Output is correct |
102 |
Correct |
1351 ms |
85696 KB |
Output is correct |
103 |
Correct |
2148 ms |
137316 KB |
Output is correct |
104 |
Correct |
2659 ms |
142676 KB |
Output is correct |
105 |
Correct |
171 ms |
18384 KB |
Output is correct |
106 |
Correct |
2202 ms |
137556 KB |
Output is correct |
107 |
Correct |
888 ms |
66092 KB |
Output is correct |
108 |
Correct |
418 ms |
26448 KB |
Output is correct |
109 |
Correct |
19 ms |
10952 KB |
Output is correct |
110 |
Correct |
117 ms |
16852 KB |
Output is correct |
111 |
Correct |
442 ms |
26704 KB |
Output is correct |
112 |
Correct |
2931 ms |
159880 KB |
Output is correct |
113 |
Correct |
2782 ms |
160400 KB |
Output is correct |
114 |
Correct |
423 ms |
26216 KB |
Output is correct |
115 |
Correct |
1166 ms |
84560 KB |
Output is correct |
116 |
Correct |
2678 ms |
151868 KB |
Output is correct |
117 |
Correct |
1890 ms |
136188 KB |
Output is correct |
118 |
Correct |
1815 ms |
129560 KB |
Output is correct |
119 |
Correct |
2461 ms |
144016 KB |
Output is correct |
120 |
Correct |
1906 ms |
119584 KB |
Output is correct |