This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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)
| ~~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |