#include "dreaming.h"
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pp pop_back
#define mp make_pair
#define bb back
#define ff first
#define ss second
using namespace std;
int build(int now, int par, vector<int>& dist, vector<vector<pair<int, int> > >& path) {
dist[now] = 0;
for (auto next : path[now]) {
if (next.ff == par) continue;
dist[now] = max(dist[now], build(next.ff, now, dist, path)+next.ss);
}
return dist[now];
}
int find(int now, int par, int up, vector<int>& dist, vector<vector<pair<int, int> > >& path) {
vector<pair<int, pair<int, int> > > child;
for (auto next : path[now]) {
if (next.ff == par) continue;
child.pb({dist[next.ff]+next.ss, next});
}
sort(child.begin(), child.end());
int ans = max(up, dist[now]);
for (int i = 0; i < child.size(); i++) {
ans = min(ans, find(child[i].ss.ff, now, (i != child.size()-1 ? max(up, child.back().ff) : (i != 0 ? max(up, child[child.size()-2].ff) : up))+child[i].ss.ss, dist, path));
}
return ans;
}
int findmax(int now, vector<int>& dist, vector<vector<pair<int, int> > >& path) {
vector<int> child;
for (auto next : path[now]) {
child.pb(dist[next.ff]+next.ss);
}
sort(child.begin(), child.end());
if (child.size() == 0) return 0;
if (child.size() == 1) return child[0];
return child[child.size()-1]+child[child.size()-2];
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
vector<int> dist(N, -1), centre;
vector<vector<pair<int, int> > > path(N);
for (int i = 0; i < M; i++) {
path[A[i]].pb({B[i], T[i]});
path[B[i]].pb({A[i], T[i]});
}
int ans = 0;
for (int i = 0; i < N; i++) {
if (dist[i] == -1) {
build(i, -1, dist, path);
centre.pb(find(i, -1, 0, dist, path));
ans = max(ans, findmax(i, dist, path));
}
}
sort(centre.begin(), centre.end());
// for (int a : centre)
// cout << a << ' ';
// cout << '\n';
int p = centre.back();
for (int i = 0; i < centre.size()-1; i++) {
ans = max(ans, p+centre[i]+L);
if (centre[i] < p)
p = max(p, centre[i]+L);
else
p = max(centre[i], p+L);
}
return ans;
}
Compilation message
dreaming.cpp: In function 'int find(int, int, int, std::vector<int>&, std::vector<std::vector<std::pair<int, int> > >&)':
dreaming.cpp:30:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for (int i = 0; i < child.size(); i++) {
| ~~^~~~~~~~~~~~~~
dreaming.cpp:31:53: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
31 | ans = min(ans, find(child[i].ss.ff, now, (i != child.size()-1 ? max(up, child.back().ff) : (i != 0 ? max(up, child[child.size()-2].ff) : up))+child[i].ss.ss, dist, path));
| ~~^~~~~~~~~~~~~~~~~
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:68:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
68 | for (int i = 0; i < centre.size()-1; i++) {
| ~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
15356 KB |
Output is correct |
2 |
Correct |
48 ms |
14680 KB |
Output is correct |
3 |
Correct |
34 ms |
13308 KB |
Output is correct |
4 |
Correct |
7 ms |
2388 KB |
Output is correct |
5 |
Correct |
5 ms |
1492 KB |
Output is correct |
6 |
Correct |
10 ms |
3536 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
21 ms |
6464 KB |
Output is correct |
9 |
Correct |
30 ms |
10440 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
52 ms |
10864 KB |
Output is correct |
12 |
Correct |
54 ms |
13004 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
15356 KB |
Output is correct |
2 |
Correct |
48 ms |
14680 KB |
Output is correct |
3 |
Correct |
34 ms |
13308 KB |
Output is correct |
4 |
Correct |
7 ms |
2388 KB |
Output is correct |
5 |
Correct |
5 ms |
1492 KB |
Output is correct |
6 |
Correct |
10 ms |
3536 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
21 ms |
6464 KB |
Output is correct |
9 |
Correct |
30 ms |
10440 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
52 ms |
10864 KB |
Output is correct |
12 |
Correct |
54 ms |
13004 KB |
Output is correct |
13 |
Correct |
1 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 |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
5588 KB |
Output is correct |
2 |
Correct |
22 ms |
5524 KB |
Output is correct |
3 |
Correct |
23 ms |
5560 KB |
Output is correct |
4 |
Correct |
32 ms |
5704 KB |
Output is correct |
5 |
Correct |
21 ms |
5520 KB |
Output is correct |
6 |
Correct |
22 ms |
6156 KB |
Output is correct |
7 |
Correct |
22 ms |
5720 KB |
Output is correct |
8 |
Correct |
20 ms |
5464 KB |
Output is correct |
9 |
Correct |
22 ms |
5460 KB |
Output is correct |
10 |
Correct |
21 ms |
5704 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
5 ms |
3664 KB |
Output is correct |
13 |
Correct |
5 ms |
3664 KB |
Output is correct |
14 |
Correct |
5 ms |
3668 KB |
Output is correct |
15 |
Correct |
5 ms |
3664 KB |
Output is correct |
16 |
Correct |
5 ms |
3664 KB |
Output is correct |
17 |
Correct |
5 ms |
3664 KB |
Output is correct |
18 |
Correct |
6 ms |
3664 KB |
Output is correct |
19 |
Correct |
5 ms |
3664 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
6 ms |
3664 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
49 ms |
15356 KB |
Output is correct |
2 |
Correct |
48 ms |
14680 KB |
Output is correct |
3 |
Correct |
34 ms |
13308 KB |
Output is correct |
4 |
Correct |
7 ms |
2388 KB |
Output is correct |
5 |
Correct |
5 ms |
1492 KB |
Output is correct |
6 |
Correct |
10 ms |
3536 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
21 ms |
6464 KB |
Output is correct |
9 |
Correct |
30 ms |
10440 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
52 ms |
10864 KB |
Output is correct |
12 |
Correct |
54 ms |
13004 KB |
Output is correct |
13 |
Correct |
1 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 |
212 KB |
Output is correct |
17 |
Correct |
1 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |