#include <bits/stdc++.h>
#include "dreaming.h"
#define ll long long
#define F first
#define S second
#define pii pair<int,int>
const int mod = 1e9 + 7;
const int mxN = 2e5 + 5;
using namespace std;
vector<vector<pii>>v;
vector<int>nums;
int mx[mxN];
bool vis[mxN];
int path;
int dpD[mxN],dpU[mxN];
void dfs(int u){
vis[u] = 1;
for(auto x : v[u]){
if(vis[x.F]) continue;
dfs(x.F);
dpD[u] = max(dpD[x.F] + x.S,dpD[u]);
}
}
void hfs(int u,int par){
multiset<int>s;
for(auto x : v[u]){
if(x.F == par) continue;
s.insert(dpD[x.F] + x.S);
}
for(auto x : v[u]){
if(x.F == par) continue;
dpU[x.F] = dpU[u] + x.S;
s.erase(s.find(dpD[x.F] + x.S));
if(s.size()) dpU[x.F] = max(dpU[x.F], *(s.rbegin()) + x.S);
s.insert(dpD[x.F] + x.S);
}
for(auto x : v[u]) if(x.F != par) hfs(x.F,u);
path = min(path,max(dpD[u],dpU[u]));
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
v.resize(N + 2);
for(int i = 0;i < M;i++){
v[A[i]].push_back({B[i],T[i]});
v[B[i]].push_back({A[i],T[i]});
}
int mx = 0;
for(int i = 0;i < N;i++){
if(!vis[i]){
path = 2e9;
dfs(i);
hfs(i,-1);
if(path > mx){
if(i)
nums.push_back(mx + L);
mx = path;
} else nums.push_back(path + L);
}
}
sort(nums.begin(),nums.end());
// for(auto x : nums) cout<<x<<' ';
// cout<<'\n';
int ans = mx;
for(int i = 0;i < nums.size();i++){
int num = 0;
if(i == nums.size() - 1){
if(i != 0){
num = nums[i] + nums[i - 1];
}
} else num = nums[i] + nums[nums.size() - 1];
num = max(num, nums[i] + mx);
ans = max(num,ans);
}
return ans;
}
// #define MAX_N 100000
// static int A[MAX_N];
// static int B[MAX_N];
// static int T[MAX_N];
// int main() {
// int N, M, L, i;
// int res;
// cin >>N>>M>>L;
// for (i = 0; i < M; i++)
// cin>>A[i]>>B[i]>>T[i];
// int answer = travelTime(N, M, L, A, B, T);
// printf("%d\n", answer);
// return 0;
// }
Compilation message
dreaming.cpp: In function 'int travelTime(int, int, int, int*, int*, int*)':
dreaming.cpp:63:21: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
63 | for(int i = 0;i < nums.size();i++){
| ~~^~~~~~~~~~~~~
dreaming.cpp:65:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
65 | if(i == nums.size() - 1){
| ~~^~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
43 ms |
19792 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
43 ms |
19792 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
7248 KB |
Output is correct |
2 |
Correct |
16 ms |
7272 KB |
Output is correct |
3 |
Correct |
16 ms |
7260 KB |
Output is correct |
4 |
Correct |
21 ms |
7260 KB |
Output is correct |
5 |
Correct |
17 ms |
7260 KB |
Output is correct |
6 |
Correct |
17 ms |
7904 KB |
Output is correct |
7 |
Correct |
16 ms |
7392 KB |
Output is correct |
8 |
Correct |
15 ms |
7136 KB |
Output is correct |
9 |
Correct |
15 ms |
7172 KB |
Output is correct |
10 |
Correct |
16 ms |
7640 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
5 ms |
5396 KB |
Output is correct |
13 |
Correct |
5 ms |
5588 KB |
Output is correct |
14 |
Correct |
4 ms |
5592 KB |
Output is correct |
15 |
Correct |
5 ms |
5588 KB |
Output is correct |
16 |
Correct |
4 ms |
5592 KB |
Output is correct |
17 |
Correct |
4 ms |
5592 KB |
Output is correct |
18 |
Correct |
5 ms |
5592 KB |
Output is correct |
19 |
Correct |
6 ms |
5592 KB |
Output is correct |
20 |
Incorrect |
0 ms |
348 KB |
Output isn't correct |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
1 ms |
2396 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
43 ms |
19792 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |