#include "dreaming.h"
#include <vector>
#include <unordered_map>
#include <deque>
#include <iostream>
#include <set>
using namespace std;
const int maxn = 1e5;
const int inf = 2e9;
bool used[maxn];
int cnt = 0;
unordered_map<int, int> dst(int v, vector<vector<pair<int, int>>> &g) {
unordered_map<int, int> d;
deque<int> q;
q.push_back(v);
d[v] = 0;
while (!q.empty()) {
int v = q.front();
q.pop_front();
for (auto [u, w] : g[v]) {
if (!d.count(u)) {
d[u] = d[v] + w;
q.push_back(u);
}
}
}
return d;
}
pair<int, int> goFar(int v, vector<vector<pair<int, int>>> &g) {
auto d1 = dst(v, g);
int best = v;
for (auto [u, x] : d1) {
if (d1[best] < x) {
best = u;
}
++cnt;
used[u] = true;
}
return { best, d1[best] };
}
pair<int, int> findDiam(int v, vector<vector<pair<int, int>>> &g) {
int leafe = goFar(v, g).first;
int best = goFar(leafe, g).first;
int best1 = goFar(best, g).first;
return { best, best1 };
}
int findDiamLen(int v, vector<vector<pair<int, int>>> &g) {
int leafe = goFar(v, g).first;
int best = goFar(leafe, g).first;
return goFar(best, g).second;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
vector<vector<pair<int, int>>> g(N);
for (int i = 0; i < M; ++i) {
int u = A[i], v = B[i];
g[u].push_back({ v, T[i] });
g[v].push_back({ u, T[i] });
}
vector<pair<int, int>> dims;
multiset<pair<int, int>> s;
for (int i = 0; i < N; ++i) {
if (!used[i]) {
cnt = 0;
auto [u, v] = findDiam(i, g);
auto d1 = dst(u, g);
auto d2 = dst(v, g);
int vs = -1;
int b1 = inf;
for (auto [u, l] : d1) {
if (b1 > max(l, d2[u])) {
b1 = max(l, d2[u]);
vs = u;
}
}
s.insert({ vs, b1 });
}
}
for (int i = 1; i < s.size(); ++i) {
auto [u1, l1] = *s.begin();
s.erase(s.begin());
auto [u2, l2] = *s.begin();
s.erase(s.begin());
g[u1].push_back({ u2, L });
g[u2].push_back({ u1, L });
if (l1 < l2) {
swap(l1, l2);
swap(u1, u2);
}
auto [u, v] = findDiam(u1, g);
auto d1 = dst(u, g);
auto d2 = dst(v, g);
int vs = -1;
int b1 = inf;
for (auto [u, l] : d1) {
if (b1 > max(l, d2[u])) {
b1 = max(l, d2[u]);
vs = u;
}
}
s.insert({ vs, b1 });
}
return findDiamLen(0, g);
}
Compilation message
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:85:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::multiset<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
85 | for (int i = 1; i < s.size(); ++i) {
| ~~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
252 ms |
15364 KB |
Output is correct |
2 |
Correct |
233 ms |
15320 KB |
Output is correct |
3 |
Correct |
146 ms |
9300 KB |
Output is correct |
4 |
Correct |
33 ms |
2352 KB |
Output is correct |
5 |
Correct |
21 ms |
1744 KB |
Output is correct |
6 |
Correct |
43 ms |
3256 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
91 ms |
6052 KB |
Output is correct |
9 |
Correct |
130 ms |
8180 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
217 ms |
10748 KB |
Output is correct |
12 |
Correct |
240 ms |
11928 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
252 ms |
15364 KB |
Output is correct |
2 |
Correct |
233 ms |
15320 KB |
Output is correct |
3 |
Correct |
146 ms |
9300 KB |
Output is correct |
4 |
Correct |
33 ms |
2352 KB |
Output is correct |
5 |
Correct |
21 ms |
1744 KB |
Output is correct |
6 |
Correct |
43 ms |
3256 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
91 ms |
6052 KB |
Output is correct |
9 |
Correct |
130 ms |
8180 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
217 ms |
10748 KB |
Output is correct |
12 |
Correct |
240 ms |
11928 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
340 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
246 ms |
15364 KB |
Output is correct |
40 |
Correct |
220 ms |
15296 KB |
Output is correct |
41 |
Correct |
129 ms |
9412 KB |
Output is correct |
42 |
Correct |
29 ms |
2480 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
328 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
212 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
212 KB |
Output is correct |
54 |
Correct |
1 ms |
212 KB |
Output is correct |
55 |
Correct |
1 ms |
212 KB |
Output is correct |
56 |
Correct |
0 ms |
340 KB |
Output is correct |
57 |
Correct |
445 ms |
16684 KB |
Output is correct |
58 |
Correct |
381 ms |
16692 KB |
Output is correct |
59 |
Correct |
461 ms |
16564 KB |
Output is correct |
60 |
Correct |
379 ms |
16708 KB |
Output is correct |
61 |
Correct |
363 ms |
16800 KB |
Output is correct |
62 |
Correct |
331 ms |
16764 KB |
Output is correct |
63 |
Correct |
371 ms |
16912 KB |
Output is correct |
64 |
Correct |
429 ms |
16800 KB |
Output is correct |
65 |
Correct |
350 ms |
16792 KB |
Output is correct |
66 |
Correct |
384 ms |
16784 KB |
Output is correct |
67 |
Correct |
386 ms |
16656 KB |
Output is correct |
68 |
Correct |
409 ms |
16660 KB |
Output is correct |
69 |
Correct |
396 ms |
16608 KB |
Output is correct |
70 |
Correct |
430 ms |
16636 KB |
Output is correct |
71 |
Correct |
1 ms |
212 KB |
Output is correct |
72 |
Correct |
9 ms |
808 KB |
Output is correct |
73 |
Correct |
8 ms |
724 KB |
Output is correct |
74 |
Correct |
7 ms |
724 KB |
Output is correct |
75 |
Correct |
7 ms |
804 KB |
Output is correct |
76 |
Correct |
7 ms |
760 KB |
Output is correct |
77 |
Correct |
11 ms |
760 KB |
Output is correct |
78 |
Correct |
9 ms |
728 KB |
Output is correct |
79 |
Correct |
12 ms |
812 KB |
Output is correct |
80 |
Correct |
419 ms |
16704 KB |
Output is correct |
81 |
Correct |
384 ms |
16576 KB |
Output is correct |
82 |
Correct |
370 ms |
16780 KB |
Output is correct |
83 |
Correct |
496 ms |
16672 KB |
Output is correct |
84 |
Correct |
4 ms |
724 KB |
Output is correct |
85 |
Correct |
5 ms |
596 KB |
Output is correct |
86 |
Correct |
5 ms |
724 KB |
Output is correct |
87 |
Correct |
8 ms |
724 KB |
Output is correct |
88 |
Correct |
5 ms |
724 KB |
Output is correct |
89 |
Correct |
6 ms |
724 KB |
Output is correct |
90 |
Correct |
6 ms |
724 KB |
Output is correct |
91 |
Correct |
9 ms |
724 KB |
Output is correct |
92 |
Correct |
9 ms |
792 KB |
Output is correct |
93 |
Correct |
8 ms |
724 KB |
Output is correct |
94 |
Correct |
1 ms |
340 KB |
Output is correct |
95 |
Correct |
1 ms |
340 KB |
Output is correct |
96 |
Correct |
1 ms |
212 KB |
Output is correct |
97 |
Correct |
1 ms |
212 KB |
Output is correct |
98 |
Correct |
1 ms |
212 KB |
Output is correct |
99 |
Correct |
0 ms |
212 KB |
Output is correct |
100 |
Correct |
1 ms |
340 KB |
Output is correct |
101 |
Correct |
1 ms |
340 KB |
Output is correct |
102 |
Correct |
1 ms |
212 KB |
Output is correct |
103 |
Correct |
1 ms |
212 KB |
Output is correct |
104 |
Correct |
21 ms |
1768 KB |
Output is correct |
105 |
Correct |
45 ms |
3172 KB |
Output is correct |
106 |
Correct |
1 ms |
340 KB |
Output is correct |
107 |
Correct |
94 ms |
6132 KB |
Output is correct |
108 |
Correct |
170 ms |
8120 KB |
Output is correct |
109 |
Correct |
2 ms |
412 KB |
Output is correct |
110 |
Correct |
246 ms |
10696 KB |
Output is correct |
111 |
Correct |
289 ms |
11932 KB |
Output is correct |
112 |
Correct |
2 ms |
340 KB |
Output is correct |
113 |
Correct |
467 ms |
16228 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1067 ms |
8068 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
340 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Incorrect |
27 ms |
340 KB |
Output isn't correct |
27 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
252 ms |
15364 KB |
Output is correct |
2 |
Correct |
233 ms |
15320 KB |
Output is correct |
3 |
Correct |
146 ms |
9300 KB |
Output is correct |
4 |
Correct |
33 ms |
2352 KB |
Output is correct |
5 |
Correct |
21 ms |
1744 KB |
Output is correct |
6 |
Correct |
43 ms |
3256 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
91 ms |
6052 KB |
Output is correct |
9 |
Correct |
130 ms |
8180 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
217 ms |
10748 KB |
Output is correct |
12 |
Correct |
240 ms |
11928 KB |
Output is correct |
13 |
Correct |
2 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
340 KB |
Output is correct |
17 |
Correct |
0 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
0 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
0 ms |
212 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
1 ms |
212 KB |
Output is correct |
32 |
Correct |
0 ms |
340 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
212 KB |
Output is correct |
36 |
Correct |
0 ms |
340 KB |
Output is correct |
37 |
Correct |
1 ms |
340 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
246 ms |
15364 KB |
Output is correct |
40 |
Correct |
220 ms |
15296 KB |
Output is correct |
41 |
Correct |
129 ms |
9412 KB |
Output is correct |
42 |
Correct |
29 ms |
2480 KB |
Output is correct |
43 |
Correct |
1 ms |
340 KB |
Output is correct |
44 |
Correct |
1 ms |
212 KB |
Output is correct |
45 |
Correct |
1 ms |
340 KB |
Output is correct |
46 |
Correct |
1 ms |
340 KB |
Output is correct |
47 |
Correct |
0 ms |
212 KB |
Output is correct |
48 |
Correct |
1 ms |
328 KB |
Output is correct |
49 |
Correct |
1 ms |
340 KB |
Output is correct |
50 |
Correct |
1 ms |
212 KB |
Output is correct |
51 |
Correct |
1 ms |
212 KB |
Output is correct |
52 |
Correct |
1 ms |
340 KB |
Output is correct |
53 |
Correct |
1 ms |
212 KB |
Output is correct |
54 |
Correct |
1 ms |
212 KB |
Output is correct |
55 |
Correct |
1 ms |
212 KB |
Output is correct |
56 |
Correct |
0 ms |
340 KB |
Output is correct |
57 |
Correct |
445 ms |
16684 KB |
Output is correct |
58 |
Correct |
381 ms |
16692 KB |
Output is correct |
59 |
Correct |
461 ms |
16564 KB |
Output is correct |
60 |
Correct |
379 ms |
16708 KB |
Output is correct |
61 |
Correct |
363 ms |
16800 KB |
Output is correct |
62 |
Correct |
331 ms |
16764 KB |
Output is correct |
63 |
Correct |
371 ms |
16912 KB |
Output is correct |
64 |
Correct |
429 ms |
16800 KB |
Output is correct |
65 |
Correct |
350 ms |
16792 KB |
Output is correct |
66 |
Correct |
384 ms |
16784 KB |
Output is correct |
67 |
Correct |
386 ms |
16656 KB |
Output is correct |
68 |
Correct |
409 ms |
16660 KB |
Output is correct |
69 |
Correct |
396 ms |
16608 KB |
Output is correct |
70 |
Correct |
430 ms |
16636 KB |
Output is correct |
71 |
Correct |
1 ms |
212 KB |
Output is correct |
72 |
Correct |
9 ms |
808 KB |
Output is correct |
73 |
Correct |
8 ms |
724 KB |
Output is correct |
74 |
Correct |
7 ms |
724 KB |
Output is correct |
75 |
Correct |
7 ms |
804 KB |
Output is correct |
76 |
Correct |
7 ms |
760 KB |
Output is correct |
77 |
Correct |
11 ms |
760 KB |
Output is correct |
78 |
Correct |
9 ms |
728 KB |
Output is correct |
79 |
Correct |
12 ms |
812 KB |
Output is correct |
80 |
Correct |
419 ms |
16704 KB |
Output is correct |
81 |
Correct |
384 ms |
16576 KB |
Output is correct |
82 |
Correct |
370 ms |
16780 KB |
Output is correct |
83 |
Correct |
496 ms |
16672 KB |
Output is correct |
84 |
Correct |
4 ms |
724 KB |
Output is correct |
85 |
Correct |
5 ms |
596 KB |
Output is correct |
86 |
Correct |
5 ms |
724 KB |
Output is correct |
87 |
Correct |
8 ms |
724 KB |
Output is correct |
88 |
Correct |
5 ms |
724 KB |
Output is correct |
89 |
Correct |
6 ms |
724 KB |
Output is correct |
90 |
Correct |
6 ms |
724 KB |
Output is correct |
91 |
Correct |
9 ms |
724 KB |
Output is correct |
92 |
Correct |
9 ms |
792 KB |
Output is correct |
93 |
Correct |
8 ms |
724 KB |
Output is correct |
94 |
Correct |
1 ms |
340 KB |
Output is correct |
95 |
Correct |
1 ms |
340 KB |
Output is correct |
96 |
Correct |
1 ms |
212 KB |
Output is correct |
97 |
Correct |
1 ms |
212 KB |
Output is correct |
98 |
Correct |
1 ms |
212 KB |
Output is correct |
99 |
Correct |
0 ms |
212 KB |
Output is correct |
100 |
Correct |
1 ms |
340 KB |
Output is correct |
101 |
Correct |
1 ms |
340 KB |
Output is correct |
102 |
Correct |
1 ms |
212 KB |
Output is correct |
103 |
Correct |
1 ms |
212 KB |
Output is correct |
104 |
Correct |
21 ms |
1768 KB |
Output is correct |
105 |
Correct |
45 ms |
3172 KB |
Output is correct |
106 |
Correct |
1 ms |
340 KB |
Output is correct |
107 |
Correct |
94 ms |
6132 KB |
Output is correct |
108 |
Correct |
170 ms |
8120 KB |
Output is correct |
109 |
Correct |
2 ms |
412 KB |
Output is correct |
110 |
Correct |
246 ms |
10696 KB |
Output is correct |
111 |
Correct |
289 ms |
11932 KB |
Output is correct |
112 |
Correct |
2 ms |
340 KB |
Output is correct |
113 |
Correct |
467 ms |
16228 KB |
Output is correct |
114 |
Execution timed out |
1067 ms |
8068 KB |
Time limit exceeded |
115 |
Halted |
0 ms |
0 KB |
- |