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 <bits/stdc++.h>
using namespace std;
#define print(a) for(auto x : a) cout << x << ' '; cout << endl;
vector<vector<array<int, 2>>> graph;
int travelTime(int n, int m, int l, int a[], int b[], int t[]) {
graph.resize(n);
for(int i = 0; i < m; i ++)
graph[a[i]].push_back({b[i], t[i]}), graph[b[i]].push_back({a[i], t[i]});
vector<int> d;
vector<array<int, 2>> lnk(n, {-1, -1});
vector<bool> used(n);
for(int i = 0; i < n; i ++){
if(!used[i]){
set<array<int, 2>> st;
st.insert({0, i});
used[i] = 1;
int last = -1;
while(!st.empty()){
auto [x, y] = *st.begin();
st.erase(st.begin());
last = y;
for(auto z : graph[y]){
if(!used[z[0]]){
st.insert({x + z[1], z[0]});
used[z[0]] = 1;
}
}
}
used[last] = 0;
st.insert({0, last});
while(!st.empty()){
auto [x, y] = *st.begin();
st.erase(st.begin());
last = y;
for(auto z : graph[y]){
if(used[z[0]]){
lnk[z[0]] = {y, z[1]};
st.insert({x + z[1], z[0]});
used[z[0]] = 0;
}
}
}
used[i] = 1;
st.insert({0, i});
while(!st.empty()){
auto [x, y] = *st.begin();
st.erase(st.begin());
for(auto z : graph[y]){
if(!used[z[0]]){
st.insert({x + z[1], z[0]});
used[z[0]] = 1;
}
}
}
vector<int> p;
int sum = 0;
while(lnk[last][0] != -1){
p.push_back(lnk[last][1]);
sum += p.back();
last = lnk[last][0];
}
p.push_back(0);
int ans = 1e9, s = 0;
for(int i = 0; i < p.size(); i ++)
ans = min(ans, max(s, sum - s)), s += p[i];
d.push_back(ans);
}
}
if(d.size() == 1)
return d[0];
else if(d.size() == 2)
return d[0] + d[1] + l;
else{
sort(d.begin(), d.end());
int ans = 2e9;
int k = d.size();
for(int i = 0; i < k - 2; i ++)
ans = min(ans, max(d[k - 2] + d[k - 1] + 2 * l, d[i] + l + d[k - 1]));
ans = min(ans, max(d[k - 2] + d[k - 1] + l, d[k - 1] + d[k - 3] + 2 * l));
ans = min(ans, max(d[k - 2] + d[k - 1] + l, d[k - 2] + d[k - 3] + 2 * l));
return ans;
}
}
Compilation message (stderr)
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:72:30: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for(int i = 0; i < p.size(); i ++)
| ~~^~~~~~~~~~
# | 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... |