#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];
}
pair<int, 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});
}
if (child.size() == 0)
return {up, up};
sort(child.begin(), child.end());
for (int i = 0; i < child.size()-1; i++) {
up = max(up, child[i].ff);
}
if (dist[child.back().ss.ff] >= child.back().ss.ss + up)
return find(child.back().ss.ff, now, up+child.back().ss.ss, dist, path);
// cout << now << ' ' << up << ' ' << child.back().ff << ' ' << par << '\n';
return {max(up, child.back().ff), child.back().ff+up};
}
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);
pair<int, int> ret = find(i, -1, 0, dist, path);
centre.pb(ret.ff);
ans = max(ans, ret.ss);
}
}
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);
p = min(p+L, centre[i]+L);
}
return ans;
}
Compilation message
dreaming.cpp: In function 'std::pair<int, int> find(int, int, int, std::vector<int>&, std::vector<std::vector<std::pair<int, int> > >&)':
dreaming.cpp:31: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]
31 | for (int i = 0; i < child.size()-1; i++) {
| ~~^~~~~~~~~~~~~~~~
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:62:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for (int i = 0; i < centre.size()-1; i++) {
| ~~^~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
10796 KB |
Output is correct |
2 |
Correct |
36 ms |
10360 KB |
Output is correct |
3 |
Correct |
30 ms |
9172 KB |
Output is correct |
4 |
Correct |
6 ms |
1748 KB |
Output is correct |
5 |
Correct |
4 ms |
1180 KB |
Output is correct |
6 |
Correct |
8 ms |
2516 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
19 ms |
4880 KB |
Output is correct |
9 |
Correct |
32 ms |
7548 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
36 ms |
8032 KB |
Output is correct |
12 |
Correct |
39 ms |
9436 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
10796 KB |
Output is correct |
2 |
Correct |
36 ms |
10360 KB |
Output is correct |
3 |
Correct |
30 ms |
9172 KB |
Output is correct |
4 |
Correct |
6 ms |
1748 KB |
Output is correct |
5 |
Correct |
4 ms |
1180 KB |
Output is correct |
6 |
Correct |
8 ms |
2516 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
19 ms |
4880 KB |
Output is correct |
9 |
Correct |
32 ms |
7548 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
36 ms |
8032 KB |
Output is correct |
12 |
Correct |
39 ms |
9436 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
21 ms |
5528 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
5 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
10796 KB |
Output is correct |
2 |
Correct |
36 ms |
10360 KB |
Output is correct |
3 |
Correct |
30 ms |
9172 KB |
Output is correct |
4 |
Correct |
6 ms |
1748 KB |
Output is correct |
5 |
Correct |
4 ms |
1180 KB |
Output is correct |
6 |
Correct |
8 ms |
2516 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
19 ms |
4880 KB |
Output is correct |
9 |
Correct |
32 ms |
7548 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
36 ms |
8032 KB |
Output is correct |
12 |
Correct |
39 ms |
9436 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
0 ms |
212 KB |
Output is correct |
17 |
Incorrect |
0 ms |
212 KB |
Output isn't correct |
18 |
Halted |
0 ms |
0 KB |
- |