# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
298536 | Bill_00 | Dreaming (IOI13_dreaming) | C++14 | 0 ms | 0 KiB |
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 <bits/stdc++.h>
#define pb push_back
#define mp make_pair
using namespace std;
int n, m, L;
vector<pair<int, long long> > adj[100005];
bool vis[100005] = {0};
long long p = 0, res = 0;
int id = 0;
vector<long long > a;
void dfs(int u, int x, long long path)
{
if(path >= p)
{
p = path;
id = u;
}
vis[u] = 1;
for(int i = 0; i < adj[u].size(); i++){
int v = adj[u][i].first;
long long e = adj[u][i].second;
if(v != x) dfs(v, u, path + e);
}
}
int go[100005];
long long cost[100005];
void dfsd(int u, int x){
if(cost[u] >= p)
{
p = cost[u];
id = u;
}
go[u] = x;
for(int i = 0; i < adj[u].size(); i++)
{
int v = adj[u][i].first;
long long e = adj[u][i].second;
if(v != x)
{
cost[v] = cost[u] + e;
dfsd(v, u);
}
}
}
int travelTime(int n,int m,int L,int A[],int B[],int T[])
{
int i, j;
for(int i=0;i<m;i++)
{
adj[A[i]].pb(mp(B[i], long long(T[i])));
adj[B[i]].pb(mp(A[i], long long(T[i])));
}
for(i = 0; i < n; i++)
if(!vis[i]){
id = 0;
p = 0;
dfs(i, -1, 0);
dfsd(id, -1);
long long W = 1e12;
res = max(res, p);
int u = id;
while(u != -1)
{
W = min(W, max(cost[u], p - cost[u]));
u = go[u];
}
a.pb(W);
}
sort(a.begin(), a.end());
int sz = a.size();
if(sz >= 2) res = max(res, a[sz - 1] + a[sz - 2] + L);
if(sz > 2) res = max(res, a[sz - 2] + a[sz - 3] + 2 * L);
return res;
}