#include "dreaming.h"
#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
int n;
const int MAXN = 1e5+10;
bool visited[MAXN],visited2[MAXN];
vector<pair<int,int>> adj[MAXN];
pair<int,int> dijkstra(bool *vis, int src){
pair<int,int> ret; ret.f = ret.s = -1;
priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> pq;
pq.push({0,src}); vis[src] = true;
while(!pq.empty()){
int u = pq.top().s, d = pq.top().f; pq.pop();
if(ret.f < d){
ret.f = d;
ret.s = u;
}
for(auto p : adj[u]){
if(!vis[p.f]){
vis[p.f] = true;
pq.push({d+p.s,p.f});
}
}
}
return ret;
}
int longpath(int u){
int a = dijkstra(visited2,u).s;
return dijkstra(visited,a).f;
}
int res = 0, l;
bool dfs(int curu, int p, int tar, int d){
if(curu == tar) return true;
for(auto x : adj[curu]){
if(x.f != p){
if(dfs(x.f,curu,tar,d+x.s)){
res = min(res,max(d,l-d));
return true;
}
}
}
return false;
}
int calc(int u){
int a = dijkstra(visited,u).s;
pair<int,int> val = dijkstra(visited2,u);
int b = val.s;
res = val.s; l = val.s;
dfs(a,-1,b,0);
return res;
}
int travelTime(int N,int M,int L,int A[],int B[],int T[]){
n = N;
for(int i = 0;i<M;++i){
int u = A[i],v = B[i],w = T[i];
adj[u].push_back({v,w}); adj[v].push_back({u,w});
}
int ans = 0;
for(int i = 0;i<N;++i){
if(!visited[i]){
ans = max(ans,longpath(i));
}
}
memset(visited,false,sizeof(visited)); memset(visited2,false,sizeof(visited2));
vector<int> results;
for(int i = 0;i<N;++i){
if(!visited[i]){
results.push_back(calc(i));
}
}
sort(results.rbegin(),results.rend());
if(results.size() > 1){
ans =max(ans,results[0]+results[1]+L);
}
if(results.size() > 2){
ans = max(ans,results[1]+results[2]+2*L);
}
return ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
6740 KB |
Output is correct |
2 |
Correct |
35 ms |
6888 KB |
Output is correct |
3 |
Correct |
25 ms |
6000 KB |
Output is correct |
4 |
Correct |
6 ms |
4444 KB |
Output is correct |
5 |
Correct |
5 ms |
4312 KB |
Output is correct |
6 |
Correct |
9 ms |
4440 KB |
Output is correct |
7 |
Incorrect |
1 ms |
3932 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3932 KB |
Output is correct |
2 |
Correct |
1 ms |
3932 KB |
Output is correct |
3 |
Incorrect |
1 ms |
3932 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
6740 KB |
Output is correct |
2 |
Correct |
35 ms |
6888 KB |
Output is correct |
3 |
Correct |
25 ms |
6000 KB |
Output is correct |
4 |
Correct |
6 ms |
4444 KB |
Output is correct |
5 |
Correct |
5 ms |
4312 KB |
Output is correct |
6 |
Correct |
9 ms |
4440 KB |
Output is correct |
7 |
Incorrect |
1 ms |
3932 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
25 ms |
6368 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
3932 KB |
Output is correct |
2 |
Correct |
1 ms |
3932 KB |
Output is correct |
3 |
Incorrect |
1 ms |
3932 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
33 ms |
6740 KB |
Output is correct |
2 |
Correct |
35 ms |
6888 KB |
Output is correct |
3 |
Correct |
25 ms |
6000 KB |
Output is correct |
4 |
Correct |
6 ms |
4444 KB |
Output is correct |
5 |
Correct |
5 ms |
4312 KB |
Output is correct |
6 |
Correct |
9 ms |
4440 KB |
Output is correct |
7 |
Incorrect |
1 ms |
3932 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |