#include "dreaming.h"
#include <vector>
#include <algorithm>
using namespace std;
pair<int, int> furthest(int v, int p, vector<bool> &used, vector<vector<pair<int, int>>> &adj)
{
used[v] = true;
pair<int, int> res = {v, 0};
for (auto [u, w] : adj[v])
{
if (u == p) continue;
pair<int, int> ret = furthest(u, v, used, adj);
ret.second += w;
if (ret.second > res.second) res = ret;
}
return res;
}
bool findPath(int v, int p, int e, vector<vector<pair<int, int>>> &adj, vector<int> &path)
{
if (v == e) return true;
for (auto [u, w] : adj[v])
{
if (u == p) continue;
bool ret = findPath(u, v, e, adj, path);
if (ret)
{
path.push_back(w);
return true;
}
}
return false;
}
int travelTime(int n, int m, int l, int A[], int B[], int T[])
{
vector<vector<pair<int, int>>> adj(n);
for (int i = 0; i < m; ++i)
{
adj[A[i]].emplace_back(B[i], T[i]);
adj[B[i]].emplace_back(A[i], T[i]);
}
vector<bool> used(n);
vector<int> weights;
for (int i = 0; i < n; ++i)
{
if (!used[i])
{
int a = furthest(i, -1, used, adj).first;
int b = furthest(a, -1, used, adj).first;
if (a == b)
{
weights.push_back(0);
continue;
}
vector<int> path;
findPath(a, -1, b, adj, path);
int minRes = 1e9 + 1, suff = 0, pref = 0;
for (int j = (int)path.size() - 1; j >= 0; --j) suff += path[j];
for (int j = 0; j < path.size(); ++j)
{
minRes = min(minRes, max(suff, pref));
pref += path[j];
suff -= path[j];
}
weights.push_back(minRes);
}
}
sort(weights.begin(), weights.end(), greater<>());
if (weights.size() == 1) return weights[0];
if (weights.size() == 2) return weights[0] + weights[1] + l;
return max(weights[0] + weights[1] + l, weights[1] + weights[2] + 2 * l);
}
Compilation message
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:62:31: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | for (int j = 0; j < path.size(); ++j)
| ~~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
50 ms |
16732 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
50 ms |
16732 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
5592 KB |
Output is correct |
2 |
Correct |
28 ms |
5588 KB |
Output is correct |
3 |
Correct |
34 ms |
5588 KB |
Output is correct |
4 |
Correct |
23 ms |
5556 KB |
Output is correct |
5 |
Correct |
25 ms |
5604 KB |
Output is correct |
6 |
Correct |
37 ms |
6136 KB |
Output is correct |
7 |
Correct |
21 ms |
5848 KB |
Output is correct |
8 |
Correct |
20 ms |
5464 KB |
Output is correct |
9 |
Correct |
22 ms |
5452 KB |
Output is correct |
10 |
Correct |
24 ms |
5720 KB |
Output is correct |
11 |
Correct |
1 ms |
292 KB |
Output is correct |
12 |
Correct |
5 ms |
3280 KB |
Output is correct |
13 |
Correct |
6 ms |
3280 KB |
Output is correct |
14 |
Correct |
7 ms |
3240 KB |
Output is correct |
15 |
Correct |
5 ms |
3280 KB |
Output is correct |
16 |
Correct |
5 ms |
3324 KB |
Output is correct |
17 |
Correct |
4 ms |
3280 KB |
Output is correct |
18 |
Correct |
5 ms |
3280 KB |
Output is correct |
19 |
Correct |
4 ms |
3280 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 |
4 ms |
3280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
50 ms |
16732 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |