#include "dreaming.h"
#include<bits/stdc++.h>
using namespace std;
const int maxn=100010;
int n, m, l;
vector<int> adj[maxn], peso[maxn];
int raio1, raio2;
int dist[maxn], pai[maxn];
int marc[maxn];
int maxi, maior;
void dfs(int u, int t){
marc[u]=1;
if(maxi<=dist[u]){
maior=u;
maxi=dist[u];
}
for(int i=0; i<adj[u].size(); i++){
int v=adj[u][i];
if(v!=pai[u]){
pai[v]=u;
dist[v]=dist[u]+peso[u][i];
dfs(v, t);
}
}
if(t) pai[u]=-1;
return;
}
pair<int, int> proximos_raios(int a){
int maxi=0;
dist[a]=0;
pai[a]=-1;
dfs(a, 1);
int pr=maior;
dist[maior]=0;
maxi=0;
dfs(maior, 0);
int at=maior;
pair<int, int> resp=make_pair(0, dist[maior]);
while(at!=pr){
if(max(resp.first, resp.second)> max(dist[at], dist[maior]-dist[at]) ){
resp=make_pair(dist[at], dist[maior]-dist[at]);
}
at=pai[at];
}
if(resp.first>resp.second) swap(resp.first, resp.second);
return resp;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
n=N; m=M; l=L;
for(int i=0; i<m; i++){
adj[A[i]].push_back(B[i]);
adj[B[i]].push_back(A[i]);
peso[A[i]].push_back(T[i]);
peso[B[i]].push_back(T[i]);
}
raio1=-1;
for(int i=0; i<n; i++){
if(marc[i]==0){
pair<int, int> p=proximos_raios(i);
// printf("%d: %d %d %d %d\n", i, raio1, raio2, p.first, p.second);
if(raio1==-1){
raio1=p.first;
raio2=p.second;
continue;
}
if(raio2>p.second){
raio1=raio2;
raio2=max(p.second+l, raio1);
}
else{
raio1=p.second;
raio2=max(raio2+l, p.first);
}
if(raio1>raio2) swap(raio1, raio2);
}
}
return raio1+raio2;
}
Compilation message
dreaming.cpp: In function 'void dfs(int, int)':
dreaming.cpp:23:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int i=0; i<adj[u].size(); i++){
~^~~~~~~~~~~~~~
dreaming.cpp: In function 'std::pair<int, int> proximos_raios(int)':
dreaming.cpp:36:6: warning: variable 'maxi' set but not used [-Wunused-but-set-variable]
int maxi=0;
^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
113 ms |
18600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
113 ms |
18600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
113 ms |
18600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
72 ms |
10448 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
113 ms |
18600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
113 ms |
18600 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |