#include<bits/stdc++.h>
#include "dreaming.h"
#define MAXN 100007
using namespace std;
const int inf=1e9;
int n,m,len;
vector< pair<int,int> > v[MAXN];
bool vis[MAXN];
int down[MAXN],up[MAXN],mindist;
vector<int> w;
void dfs(int x,int p){
vis[x]=true;
for(int i=0;i<v[x].size();i++){
if(v[x][i].first==p)continue;
dfs(v[x][i].first,x);
down[x]=max(down[x],down[v[x][i].first]+v[x][i].second);
}
}
void dfs2(int x,int p){
vis[x]=true;
multiset<int> s;
for(int i=0;i<v[x].size();i++){
if(v[x][i].first==p)continue;
s.insert({down[v[x][i].first]+v[x][i].second});
}
for(int i=0;i<v[x].size();i++){
if(v[x][i].first==p)continue;
s.erase(s.find(down[v[x][i].first]+v[x][i].second));
up[v[x][i].first]=up[x];
if(!s.empty())up[v[x][i].first]=max(up[v[x][i].first], *s.rbegin());
up[v[x][i].first]+=v[x][i].second;
dfs2(v[x][i].first,x);
s.insert(down[v[x][i].first]+v[x][i].second);
}
mindist=min(mindist,max(down[x],up[x]));
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
//int travelTime(int N, int M, int L, vector<int> A, vector<int> B, vector<int> T) {
n=N; m=M; len=L;
for(int i=1;i<=m;i++){
A[i-1]++; B[i-1]++;
v[A[i-1]].push_back({B[i-1],T[i-1]});
v[B[i-1]].push_back({A[i-1],T[i-1]});
}
for(int i=1;i<=n;i++){
if(!vis[i])dfs(i,0);
}
for(int i=1;i<=n;i++)vis[i]=false;
for(int i=1;i<=n;i++){
if(!vis[i]){
mindist=inf; dfs2(i,0);
w.push_back(mindist);
}
}
sort(w.begin(),w.end());
reverse(w.begin(),w.end());
if(w.size()==2){
return w[0]+w[1]+len;
}else{
return max(w[0]+w[1]+len,w[1]+w[2]+2*len);
}
}
/*int main(){
cout<<travelTime(12,8,2,{0,8,2,5,5,1,1,10},{8,2,7,11,1,3,9,6},{4,2,4,3,7,1,5,3})<<"\n";
return 0;
}*/
Compilation message
dreaming.cpp: In function 'void dfs(int, int)':
dreaming.cpp:18:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
18 | for(int i=0;i<v[x].size();i++){
| ~^~~~~~~~~~~~
dreaming.cpp: In function 'void dfs2(int, int)':
dreaming.cpp:30:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
30 | for(int i=0;i<v[x].size();i++){
| ~^~~~~~~~~~~~
dreaming.cpp:35:15: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
35 | for(int i=0;i<v[x].size();i++){
| ~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
41 ms |
16732 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4568 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
41 ms |
16732 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
7640 KB |
Output is correct |
2 |
Correct |
19 ms |
7508 KB |
Output is correct |
3 |
Correct |
16 ms |
7552 KB |
Output is correct |
4 |
Correct |
16 ms |
7508 KB |
Output is correct |
5 |
Correct |
17 ms |
7508 KB |
Output is correct |
6 |
Correct |
18 ms |
7740 KB |
Output is correct |
7 |
Correct |
16 ms |
7640 KB |
Output is correct |
8 |
Correct |
25 ms |
7492 KB |
Output is correct |
9 |
Correct |
17 ms |
7364 KB |
Output is correct |
10 |
Correct |
24 ms |
7632 KB |
Output is correct |
11 |
Correct |
1 ms |
4600 KB |
Output is correct |
12 |
Correct |
5 ms |
5172 KB |
Output is correct |
13 |
Correct |
4 ms |
5328 KB |
Output is correct |
14 |
Correct |
5 ms |
5328 KB |
Output is correct |
15 |
Correct |
5 ms |
5328 KB |
Output is correct |
16 |
Correct |
5 ms |
5328 KB |
Output is correct |
17 |
Correct |
5 ms |
5328 KB |
Output is correct |
18 |
Correct |
5 ms |
5328 KB |
Output is correct |
19 |
Correct |
4 ms |
5328 KB |
Output is correct |
20 |
Incorrect |
1 ms |
4436 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
3 ms |
4568 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
41 ms |
16732 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |