#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
vector<vector<pair<int,int>>> adjlist; //ind,val
int n;
vector<bool> visited;
vector<int> parent;
vector<int> depth;
int fdist = 0;
int f_ind = 0;
int INF = 1e9;
void getFurthest(int ind, int par){
//cout<<ind<<' '<<par<<'\n';
visited[ind]=true;
parent[ind]=par;
for (auto p : adjlist[ind]){
//cout<<"proc "<<p.first<<' '<<p.second<<'\n';
if (p.first==par){continue;}
depth[p.first]=depth[ind]+p.second;
if (depth[p.first]>fdist){
fdist=depth[p.first];
f_ind=p.first;
}
getFurthest(p.first,ind);
}
}
pair<int,int> getDiamDepth(int ind){
//cout<<"check "<<ind<<'\n';
fdist=0;f_ind=ind;
parent[ind]=-1;
getFurthest(ind,-1);
int ftmp = f_ind;
fdist=0;f_ind=ftmp;
parent[ftmp]=-1;depth[ftmp]=0;
getFurthest(ftmp,-1);
//find min depth
int cur = f_ind;
int md = INF;
while (cur!=-1){
md = min(md, max(depth[cur],fdist-depth[cur]));
cur=parent[cur];
}
//cout<<ind<<": "<<fdist<<' '<<md<<'\n';
return {fdist,md};
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
n=N;
adjlist.resize(n);
for (int i = 0; i<M; i++){
adjlist[A[i]].push_back({B[i],T[i]});
adjlist[B[i]].push_back({A[i],T[i]});
}
visited.resize(n);
parent.resize(n,-1);
depth.resize(n);
int mindist = INF;
int maxd = 0;
int smaxd = 0;
int tmaxd = 0;
for (int i = 0; i<n; i++){
if (visited[i]){continue;}
auto pii = getDiamDepth(i);
mindist = min(mindist, pii.first);
if (pii.second>=maxd){
tmaxd=smaxd;
smaxd=maxd;
maxd=pii.second;
} else if (pii.second>=smaxd){
tmaxd=smaxd;
smaxd=pii.second;
} else if (pii.second>=tmaxd){
tmaxd=pii.second;
}
}
return max(mindist,max(maxd+smaxd+L,tmaxd+smaxd+2*L));
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
61 ms |
12664 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
61 ms |
12664 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
61 ms |
12664 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
41 ms |
6272 KB |
Output is correct |
2 |
Correct |
26 ms |
6016 KB |
Output is correct |
3 |
Correct |
35 ms |
6064 KB |
Output is correct |
4 |
Correct |
27 ms |
6016 KB |
Output is correct |
5 |
Correct |
36 ms |
6008 KB |
Output is correct |
6 |
Correct |
36 ms |
6400 KB |
Output is correct |
7 |
Correct |
27 ms |
6400 KB |
Output is correct |
8 |
Correct |
43 ms |
5880 KB |
Output is correct |
9 |
Correct |
32 ms |
5884 KB |
Output is correct |
10 |
Correct |
27 ms |
6264 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
6 ms |
3456 KB |
Output is correct |
13 |
Correct |
7 ms |
3584 KB |
Output is correct |
14 |
Correct |
6 ms |
3456 KB |
Output is correct |
15 |
Correct |
6 ms |
3456 KB |
Output is correct |
16 |
Correct |
5 ms |
3456 KB |
Output is correct |
17 |
Correct |
5 ms |
3456 KB |
Output is correct |
18 |
Correct |
6 ms |
3584 KB |
Output is correct |
19 |
Correct |
5 ms |
3456 KB |
Output is correct |
20 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
61 ms |
12664 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
61 ms |
12664 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |