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;
typedef long long ll;
vector<vector<pair<ll,ll>>> g;
vector<bool> vis;
vector<ll> fir,sec,ans;
void dfs1(ll nodo, ll p){
vis[nodo] = true;
for(auto k : g[nodo]){
ll v = k.first;
ll w = k.second;
if(v == p)continue;
dfs1(v,nodo);
if(fir[v] + w > fir[nodo]){
sec[nodo] = fir[nodo];
fir[nodo] = fir[v] + w;
}else if(fir[v] + w > sec[nodo]){
sec[nodo] = fir[v] + w;
}
}
}
ll dfs2(ll nodo, ll p, ll top){
ans[nodo] = max(fir[nodo],top);
ll cont = ans[nodo];
for(auto k : g[nodo]){
ll v = k.first;
ll w = k.second;
if(v == p)continue;
if(fir[v] + w == fir[nodo]) cont = min(cont,dfs2(v,nodo,max(top,sec[nodo]) + w));
else cont = min(cont,dfs2(v,nodo,ans[nodo] + w));
}
return cont;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
g.resize(N);
for(int i=0; i<M; i++){
ll x = A[i];
ll y = B[i];
ll w = T[i];
g[x].push_back({y,w});
g[y].push_back({x,w});
}
vis.assign(N,false);
fir.assign(N,0);
sec.assign(N,0);
ans.resize(N);
ll m1,m2,m3;
m1 = m2 = m3 = 0;
for(int i=0; i<N; i++){
if(vis[i] == false){
dfs1(i,-1);
ll q = dfs2(i,-1,0);
if(q > m1){
m3 = m2;
m2 = m1;
m1 = q;
}else if(q > m2){
m3 = m2;
m2 = q;
}else if(q > m3){
m3 = q;
}
//cout << i << " " << q << endl;
}
}
return max(m1+m2+L,m2+m3+2*L);
}
/*
int main(){
int n,m,l;
cin >> n >> m >> l;
int a[m],b[m],t[m];
for(int i=0; i<m; i++){
cin >> a[i] >> b[i] >> t[i];
}
cout << travelTime(n,m,l,a,b,t) << endl;
}
*/
# | 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... |