#include"dreaming.h"
#include<iostream>
#include<vector>
#include<algorithm>
#include<map>
using namespace std;
typedef long long ll;
vector<pair<ll,ll>>nodes[100000];
bool vis[100000];
vector<ll>node_list;
void dfs(ll node){
if(vis[node])
return;
vis[node]=true;
node_list.push_back(node);
for(auto ne:nodes[node])
dfs(ne.first);
}
map<pair<ll,ll>,ll>dp;
ll get_max_dist(ll node,ll prev){
if(dp.count({node,prev}))
return dp[{node,prev}];
ll res=0;
for(auto ne:nodes[node]){
if(ne.first==prev)
continue;
res=max(res,get_max_dist(ne.first,node)+ne.second);
}
dp[{node,prev}]=res;
return res;
}
ll get_diameter(ll root){
node_list.clear();
dfs(root);
/*cout<<"- ";
for(int i:node_list)
cout<<i<<" ";
cout<<"\n";*/
ll res=1e18;
for(ll node:node_list){
//cout<<node<<": "<<get_max_dist(node,node)<<"\n";
res=min(res,get_max_dist(node,node));
}
return res;
}
int travelTime(int N,int M,int L,int A[],int B[],int T[]){
for(ll i=0;i<M;i++){
nodes[A[i]].emplace_back(B[i],T[i]);
nodes[B[i]].emplace_back(A[i],T[i]);
}
vector<ll>dists;
for(ll i=0;i<N;i++)
if(!vis[i])
dists.push_back(get_diameter(i));
/*for(int i:dists)
cout<<i<<" ";
cout<<"\n";*/
sort(dists.begin(),dists.end());
reverse(dists.begin(),dists.end());
if(dists.size()==1)
return dists[0];
else
return dists[0]+dists[1]+L;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
189 ms |
32448 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
3928 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
189 ms |
32448 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
63 ms |
16076 KB |
Output is correct |
2 |
Incorrect |
57 ms |
16328 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
3928 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
189 ms |
32448 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |