#include<bits/stdc++.h>
#include"dreaming.h"
#define maxn 100005
using namespace std;
int dist[maxn][3],marc[maxn][3];
vector<int> grafo[maxn],peso[maxn],cc[maxn],raios;
void dfs(int v,int k,int id){
marc[v][id]=k;
for(int i=0;i<grafo[v].size();i++){
int viz=grafo[v][i];
int p=peso[v][i];
if(marc[viz][id]==0){
dist[viz][id]=dist[v][id]+p;
dfs(viz,k,id);
}
}
}
int travelTime(int N,int M,int L,int A[],int B[],int T[]){
int k=1;
for(int i=0;i<M;i++){
grafo[A[i]].push_back(B[i]);
peso[A[i]].push_back(T[i]);
grafo[B[i]].push_back(A[i]);
peso[B[i]].push_back(T[i]);
}
for(int i=0;i<N;i++){
if(marc[i][0]==0){
dist[i][0]=0;
dfs(i,k,0);
k++;
}
cc[marc[i][0]].push_back(i);
}
for(int i=1;i<k;i++){
//////////////////////////////////ACHAR a
int a=cc[i][0];
for(int j=0;j<cc[i].size();j++){
int prox=cc[i][j];
if(dist[prox][0]>dist[a][0]) a=prox;
}
dist[a][1]=0;
dfs(a,i,1);
/////////////////////////////////ACHAR b
int b=a;
for(int j=0;j<cc[i].size();j++){
int prox=cc[i][j];
if(dist[prox][1]>dist[b][1]) b=prox;
}
dist[b][2]=0;
dfs(b,i,2);
////////////////////////////////ACHAR O MAIOR RAIO
int d=dist[b][1],r=d;
for(int j=0;j<cc[i].size();j++){
int cur=cc[i][j];
int r1=dist[cur][1],r2=dist[cur][2];
if(r1+r2==d) r=min(r,max(r1,r2));
}
///////////////////////////////ACHAMOS
raios.push_back(r);
}
sort(raios.begin(),raios.end());
int tam=raios.size();
int resp=0;
if(tam>=3){
int m1=raios[tam-1],m2=raios[tam-2],m3=raios[tam-3];
resp=max(m1+m2+L,m2+m3+2*L);
}
else if(tam==2){
int m1=raios[tam-1],m2=raios[tam-2];
resp=m1+m2+L;
}
else if(tam==1){
int m1=raios[tam-1];
resp=m1;
}
for(int i=0;i<N;i++){
grafo[i].clear();
peso[i].clear();
cc[i].clear();
dist[i][0]=0;dist[i][1]=0;dist[i][2]=0;
marc[i][0]=0;marc[i][1]=0;marc[i][2]=0;
}
raios.clear();
return resp;
}
/*int main(){
int n,m,l;
scanf("%d %d %d",&n,&m,&l);
int A[maxn],B[maxn],P[maxn];
for(int i=0;i<m;i++){
int a,b,p;
scanf("%d %d %d",&a,&b,&p);
A[i]=a;
B[i]=b;
P[i]=p;
}
cout<<travelTime(n,m,l,A,B,P);
}*/
Compilation message
dreaming.cpp: In function 'void dfs(int, int, int)':
dreaming.cpp:9:15: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0;i<grafo[v].size();i++){
~^~~~~~~~~~~~~~~~
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:37:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0;j<cc[i].size();j++){
~^~~~~~~~~~~~~
dreaming.cpp:45:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0;j<cc[i].size();j++){
~^~~~~~~~~~~~~
dreaming.cpp:53:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int j=0;j<cc[i].size();j++){
~^~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
115 ms |
23920 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
115 ms |
23920 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
115 ms |
23920 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
101 ms |
16252 KB |
Output is correct |
2 |
Correct |
86 ms |
16248 KB |
Output is correct |
3 |
Correct |
70 ms |
16248 KB |
Output is correct |
4 |
Correct |
102 ms |
16248 KB |
Output is correct |
5 |
Correct |
69 ms |
16120 KB |
Output is correct |
6 |
Correct |
94 ms |
17012 KB |
Output is correct |
7 |
Correct |
92 ms |
16632 KB |
Output is correct |
8 |
Correct |
70 ms |
16120 KB |
Output is correct |
9 |
Correct |
64 ms |
15992 KB |
Output is correct |
10 |
Correct |
78 ms |
16504 KB |
Output is correct |
11 |
Correct |
8 ms |
7424 KB |
Output is correct |
12 |
Correct |
22 ms |
13568 KB |
Output is correct |
13 |
Correct |
21 ms |
13564 KB |
Output is correct |
14 |
Correct |
22 ms |
13568 KB |
Output is correct |
15 |
Correct |
24 ms |
13568 KB |
Output is correct |
16 |
Correct |
23 ms |
13540 KB |
Output is correct |
17 |
Correct |
23 ms |
13440 KB |
Output is correct |
18 |
Correct |
26 ms |
13564 KB |
Output is correct |
19 |
Correct |
23 ms |
13568 KB |
Output is correct |
20 |
Correct |
8 ms |
7452 KB |
Output is correct |
21 |
Correct |
7 ms |
7424 KB |
Output is correct |
22 |
Correct |
8 ms |
7552 KB |
Output is correct |
23 |
Correct |
22 ms |
13568 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
115 ms |
23920 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
115 ms |
23920 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |