#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
struct edge{
int to,dist;
};
bool vis[100000];
int D[100000],DD[100000],DDD[100000];
vector<edge>E[100000];
void dfs0(int x,int from,vector<int>&v){
vis[x]=true;
v.push_back(x);
for(edge e:E[x]){
if(e.to==from)continue;
D[e.to]=D[x]+e.dist;
dfs0(e.to,x,v);
}
}
void dfs1(int x,int from){
for(edge e:E[x]){
if(e.to==from)continue;
DD[e.to]=DD[x]+e.dist;
dfs1(e.to,x);
}
}
void dfs2(int x,int from){
for(edge e:E[x]){
if(e.to==from)continue;
DDD[e.to]=DDD[x]+e.dist;
dfs2(e.to,x);
}
}
int travelTime(int N, int M, int L, int a[], int b[], int d[]) {
for(int i=0;i<M;i++){
E[a[i]].push_back({b[i],d[i]});
E[b[i]].push_back({a[i],d[i]});
}
vector<int>v;
for(int i=0;i<N;i++){
if(!vis[i]){
vector<int>V;
dfs0(i,i,V);
pair<int,int>p={-1,-1};
for(int j:V){
p=max(p,{D[j],j});
}
dfs1(p.second,p.second);
pair<int,int>pp={-1,-1};
for(int j:V){
pp=max(pp,{DD[j],j});
}
dfs2(pp.second,pp.second);
int mn=2000000000;
for(int j:V){
mn=min(mn,max(DD[j],DDD[j]));
}
v.push_back(mn);
cout<<i<<' '<<mn<<endl;
}
}
sort(v.begin(),v.end(),greater<>());
if(v.size()==1){
return v.front();
}
else if(v.size()==2){
return max(v[0],v[1]+L);
}
else{
return max({v[0],v[1]+L,v[2]+L+L});
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
65 ms |
12532 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
65 ms |
12532 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
65 ms |
12532 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
187 ms |
7028 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
65 ms |
12532 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
65 ms |
12532 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |