#include "dreaming.h"
#include<bits/stdc++.h>
using namespace std;
using lol=long long int;
void dfs(int u,const vector<vector<pair<int,int> > >& g,vector<int>& vis,vector<int>& D,vector<int>& sD,vector<int>& marked)
{
vis[u]=1;
for(auto [v,w]:g[u])
{
if(vis[v]==1) continue;
dfs(v,g,vis,D,sD,marked);
if(D[v]+w>=D[u])
{
sD[u]=D[u];
D[u]=D[v]+w;
marked[u]=v;
}else if(D[v]+w>=sD[u])
{
sD[u]=D[v]+w;
}
}
}
void reroot(int u,const vector<vector<pair<int,int> > >& g,vector<int>& vis,vector<int>& D,vector<int>& sD,vector<int>& marked,vector<int>& fD)
{
vis[u]=2;
for(auto [v,w]:g[u])
{
if(vis[v]==2) continue;
if(v!=marked[u])
{
if(fD[u]+w>=D[v])
{
sD[v]=D[v];
fD[v]=fD[u]+w;
marked[v]=u;
}else if(fD[u]+w>=sD[v])
{
fD[v]=D[v];
sD[v]=fD[u]+w;
}
}else
{
if(sD[u]+w>=D[v])
{
sD[v]=D[v];
fD[v]=sD[u]+w;
marked[v]=u;
}else if(sD[u]+w>=sD[v])
{
fD[v]=D[v];
sD[v]=sD[u]+w;
}
}
reroot(v,g,vis,D,sD,marked,fD);
}
}
int getrep(int u,const vector<vector<pair<int,int> > >& g,vector<int>& vis,const vector<int>& fD)
{
vis[u]=3;
int mn=fD[u];
for(auto [v,w]:g[u])
{
if(vis[v]==3) continue;
mn=min(mn,getrep(v,g,vis,fD));
}
return mn;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
vector<vector<pair<int,int> > > g(N);
for(int i=0;i<M;i++)
{
g[A[i]].push_back({B[i],T[i]});
g[B[i]].push_back({A[i],T[i]});
}
vector<int> D(N,0),sD(N,0),fD(N,0),marked(N,-1);
vector<int> vis(N,0);
vector<int> reps;
for(int i=0;i<N;i++)
{
if(vis[i]==0)
{
dfs(i,g,vis,D,sD,marked);
fD[i]=D[i];
reroot(i,g,vis,D,sD,marked,fD);
reps.push_back(getrep(i,g,vis,fD));
}
}
sort(reps.rbegin(),reps.rend());
/*for(auto& e:D) cout<<e<<" ";
cout<<endl;
for(auto& e:sD) cout<<e<<" ";
cout<<endl;
for(auto& e:fD) cout<<e<<" ";
cout<<endl;
for(auto& e:reps) cout<<e<<" ";
cout<<endl;*/
int ans=max(reps[0],*max_element(fD.begin(),fD.end()));
if(reps.size()>1) ans=max(ans,L+reps[0]+reps[1]);
if(reps.size()>2) ans=max(ans,2*L+reps[1]+reps[2]);
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
13948 KB |
Output is correct |
2 |
Correct |
69 ms |
14484 KB |
Output is correct |
3 |
Correct |
44 ms |
11988 KB |
Output is correct |
4 |
Correct |
8 ms |
2340 KB |
Output is correct |
5 |
Correct |
8 ms |
1484 KB |
Output is correct |
6 |
Correct |
14 ms |
3532 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
32 ms |
6340 KB |
Output is correct |
9 |
Correct |
44 ms |
9508 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
74 ms |
11052 KB |
Output is correct |
12 |
Correct |
86 ms |
12996 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Incorrect |
1 ms |
296 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
13948 KB |
Output is correct |
2 |
Correct |
69 ms |
14484 KB |
Output is correct |
3 |
Correct |
44 ms |
11988 KB |
Output is correct |
4 |
Correct |
8 ms |
2340 KB |
Output is correct |
5 |
Correct |
8 ms |
1484 KB |
Output is correct |
6 |
Correct |
14 ms |
3532 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
32 ms |
6340 KB |
Output is correct |
9 |
Correct |
44 ms |
9508 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
74 ms |
11052 KB |
Output is correct |
12 |
Correct |
86 ms |
12996 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Incorrect |
1 ms |
296 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
36 ms |
6988 KB |
Output is correct |
2 |
Correct |
31 ms |
7068 KB |
Output is correct |
3 |
Correct |
31 ms |
6992 KB |
Output is correct |
4 |
Correct |
39 ms |
7088 KB |
Output is correct |
5 |
Correct |
31 ms |
7116 KB |
Output is correct |
6 |
Correct |
48 ms |
7636 KB |
Output is correct |
7 |
Correct |
45 ms |
7348 KB |
Output is correct |
8 |
Correct |
28 ms |
6924 KB |
Output is correct |
9 |
Correct |
29 ms |
6852 KB |
Output is correct |
10 |
Correct |
32 ms |
7260 KB |
Output is correct |
11 |
Correct |
0 ms |
204 KB |
Output is correct |
12 |
Correct |
7 ms |
5276 KB |
Output is correct |
13 |
Correct |
8 ms |
5192 KB |
Output is correct |
14 |
Correct |
8 ms |
5172 KB |
Output is correct |
15 |
Correct |
7 ms |
5252 KB |
Output is correct |
16 |
Correct |
7 ms |
5192 KB |
Output is correct |
17 |
Correct |
7 ms |
5192 KB |
Output is correct |
18 |
Correct |
8 ms |
5296 KB |
Output is correct |
19 |
Correct |
7 ms |
5192 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
460 KB |
Output is correct |
23 |
Correct |
7 ms |
5192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Incorrect |
1 ms |
296 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
69 ms |
13948 KB |
Output is correct |
2 |
Correct |
69 ms |
14484 KB |
Output is correct |
3 |
Correct |
44 ms |
11988 KB |
Output is correct |
4 |
Correct |
8 ms |
2340 KB |
Output is correct |
5 |
Correct |
8 ms |
1484 KB |
Output is correct |
6 |
Correct |
14 ms |
3532 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
32 ms |
6340 KB |
Output is correct |
9 |
Correct |
44 ms |
9508 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
74 ms |
11052 KB |
Output is correct |
12 |
Correct |
86 ms |
12996 KB |
Output is correct |
13 |
Correct |
1 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Incorrect |
1 ms |
296 KB |
Output isn't correct |
17 |
Halted |
0 ms |
0 KB |
- |