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>
#define f first
#define s second
using namespace std;
int n;
const int MAXN = 1e5+10;
bool visited[MAXN],visited2[MAXN];
vector<pair<int,int>> adj[MAXN];
pair<int,int> dijkstra(bool* vis, int src){
pair<int,int> ret; ret.f = ret.s = -1;
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
pq.push({0,src}); vis[src] = true;
while(!pq.empty()){
int u = pq.top().s, d = pq.top().f;
if(ret.f < d){
ret.f = d;
ret.s = u;
}
for(auto p : adj[u]){
if(!vis[p.f]){
vis[p.f] = true;
pq.push({d+p.s,p.f});
}
}
}
return ret;
}
int longpath(int u){
int a = dijkstra(visited,u).s;
return dijkstra(visited2,a).f;
}
int res = 0, l;
bool dfs(int curu, int p, int tar, int d){
for(auto x : adj[curu]){
if(x.f != p){
if(dfs(x.f,curu,tar,d+x.s)){
res = min(res,max(d,l-d));
return true;
}
}
}
return false;
}
int calc(int u){
int a = dijkstra(visited,u).s;
pair<int,int> val = dijkstra(visited2,u);
int b = val.s;
res = val.s; l = val.s;
dfs(a,-1,b,0);
return res;
}
int travelTime(int N,int M,int L,int A[],int B[],int T[]){
n = N;
for(int i = 0;i<M;++i){
int u = A[i],v = B[i],w = T[i];
adj[u].push_back({v,w}); adj[v].push_back({u,w});
}
int ans = 0;
for(int i = 0;i<N;++i){
if(!visited[i]){
ans = max(ans,longpath(i));
}
}
memset(visited,false,sizeof(visited)); memset(visited2,false,sizeof(visited2));
vector<int> results;
for(int i = 0;i<N;++i){
if(!visited[i]){
results.push_back(calc(i));
}
}
sort(results.rbegin(),results.rend());
if(results.size() > 1){
ans = max(ans,results[0]+results[1]+L);
}
if(results.size() > 2){
ans = max(ans,results[1]+results[2]+2*L);
}
return ans;
}
# | 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... |