#include <bits/stdc++.h>
#include "dreaming.h"
using namespace std;
typedef long long ll;
int n, m, k;
vector<pair<int, int> > link[100002];
int DP[100002], DP2[100002];
bool visited[100002];
int ans;
int minVal;
vector<int> vec;
void dfs(int x){
visited[x] = 1;
for(auto y: link[x]){
if(visited[y.first]) continue;
dfs(y.first);
int tmp1 = DP[y.first] + y.second;
int tmp2 = DP2[y.first] + y.second;
if(DP2[x] < tmp1) DP2[x] = tmp1;
if(DP2[x] > DP[x]) swap(DP[x], DP2[x]);
if(DP2[x] < tmp2) DP2[x] = tmp2;
}
if(DP[x] < 0) DP[x] = 0;
ans = max(ans, DP[x] + DP2[x]);
visited[x] = 0;
}
void dfs2(int x, int fromUp){
visited[x] = 1;
minVal = min(minVal, max(fromUp, DP[x]));
for(auto y: link[x]){
if(visited[y.first]) continue;
dfs2(y.first, max(fromUp, (DP[x] == DP[y.first] + y.second) ? DP2[x] : DP[x]) + y.second);
}
}
int travelTime(int _n, int _m, int _k, int x[], int y[], int z[]){
n = _n, m = _m, k = _k;
for(int i=0; i<n; i++) DP[i] = DP2[i] = -1e9;
for(int i=0; i<m; i++){
link[x[i]].push_back(make_pair(y[i], z[i]));
link[y[i]].push_back(make_pair(x[i], z[i]));
}
for(int i=0; i<n; i++){
if(!visited[i]){
minVal = INT_MAX;
dfs(i);
dfs2(i, 0);
vec.push_back(minVal);
}
}
sort(vec.rbegin(), vec.rend());
if(vec.size() >= 2) ans = max(ans, vec[0] + k + vec[1]);
if(vec.size() >= 3) ans = max(ans, vec[1] + k + k + vec[2]);
return ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
13420 KB |
Output is correct |
2 |
Correct |
55 ms |
13036 KB |
Output is correct |
3 |
Correct |
36 ms |
11500 KB |
Output is correct |
4 |
Correct |
9 ms |
4204 KB |
Output is correct |
5 |
Correct |
9 ms |
3564 KB |
Output is correct |
6 |
Correct |
16 ms |
5100 KB |
Output is correct |
7 |
Correct |
2 ms |
2796 KB |
Output is correct |
8 |
Correct |
25 ms |
7148 KB |
Output is correct |
9 |
Correct |
34 ms |
9580 KB |
Output is correct |
10 |
Correct |
2 ms |
2796 KB |
Output is correct |
11 |
Correct |
54 ms |
10604 KB |
Output is correct |
12 |
Correct |
62 ms |
11884 KB |
Output is correct |
13 |
Correct |
2 ms |
2796 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
13420 KB |
Output is correct |
2 |
Correct |
55 ms |
13036 KB |
Output is correct |
3 |
Correct |
36 ms |
11500 KB |
Output is correct |
4 |
Correct |
9 ms |
4204 KB |
Output is correct |
5 |
Correct |
9 ms |
3564 KB |
Output is correct |
6 |
Correct |
16 ms |
5100 KB |
Output is correct |
7 |
Correct |
2 ms |
2796 KB |
Output is correct |
8 |
Correct |
25 ms |
7148 KB |
Output is correct |
9 |
Correct |
34 ms |
9580 KB |
Output is correct |
10 |
Correct |
2 ms |
2796 KB |
Output is correct |
11 |
Correct |
54 ms |
10604 KB |
Output is correct |
12 |
Correct |
62 ms |
11884 KB |
Output is correct |
13 |
Correct |
2 ms |
2796 KB |
Output is correct |
14 |
Incorrect |
2 ms |
2668 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
13420 KB |
Output is correct |
2 |
Correct |
55 ms |
13036 KB |
Output is correct |
3 |
Correct |
36 ms |
11500 KB |
Output is correct |
4 |
Correct |
9 ms |
4204 KB |
Output is correct |
5 |
Correct |
9 ms |
3564 KB |
Output is correct |
6 |
Correct |
16 ms |
5100 KB |
Output is correct |
7 |
Correct |
2 ms |
2796 KB |
Output is correct |
8 |
Correct |
25 ms |
7148 KB |
Output is correct |
9 |
Correct |
34 ms |
9580 KB |
Output is correct |
10 |
Correct |
2 ms |
2796 KB |
Output is correct |
11 |
Correct |
54 ms |
10604 KB |
Output is correct |
12 |
Correct |
62 ms |
11884 KB |
Output is correct |
13 |
Correct |
2 ms |
2796 KB |
Output is correct |
14 |
Incorrect |
2 ms |
2668 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
26 ms |
6640 KB |
Output is correct |
2 |
Correct |
28 ms |
6764 KB |
Output is correct |
3 |
Correct |
25 ms |
6640 KB |
Output is correct |
4 |
Correct |
32 ms |
6892 KB |
Output is correct |
5 |
Correct |
27 ms |
6636 KB |
Output is correct |
6 |
Correct |
29 ms |
7152 KB |
Output is correct |
7 |
Correct |
35 ms |
6768 KB |
Output is correct |
8 |
Correct |
25 ms |
6636 KB |
Output is correct |
9 |
Correct |
25 ms |
6508 KB |
Output is correct |
10 |
Correct |
29 ms |
6764 KB |
Output is correct |
11 |
Correct |
2 ms |
2668 KB |
Output is correct |
12 |
Correct |
6 ms |
4200 KB |
Output is correct |
13 |
Correct |
7 ms |
4200 KB |
Output is correct |
14 |
Correct |
6 ms |
4200 KB |
Output is correct |
15 |
Correct |
6 ms |
4200 KB |
Output is correct |
16 |
Correct |
6 ms |
4200 KB |
Output is correct |
17 |
Correct |
6 ms |
4200 KB |
Output is correct |
18 |
Correct |
7 ms |
4200 KB |
Output is correct |
19 |
Correct |
6 ms |
4200 KB |
Output is correct |
20 |
Correct |
2 ms |
2668 KB |
Output is correct |
21 |
Correct |
2 ms |
2668 KB |
Output is correct |
22 |
Correct |
2 ms |
2796 KB |
Output is correct |
23 |
Correct |
6 ms |
4200 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
13420 KB |
Output is correct |
2 |
Correct |
55 ms |
13036 KB |
Output is correct |
3 |
Correct |
36 ms |
11500 KB |
Output is correct |
4 |
Correct |
9 ms |
4204 KB |
Output is correct |
5 |
Correct |
9 ms |
3564 KB |
Output is correct |
6 |
Correct |
16 ms |
5100 KB |
Output is correct |
7 |
Correct |
2 ms |
2796 KB |
Output is correct |
8 |
Correct |
25 ms |
7148 KB |
Output is correct |
9 |
Correct |
34 ms |
9580 KB |
Output is correct |
10 |
Correct |
2 ms |
2796 KB |
Output is correct |
11 |
Correct |
54 ms |
10604 KB |
Output is correct |
12 |
Correct |
62 ms |
11884 KB |
Output is correct |
13 |
Correct |
2 ms |
2796 KB |
Output is correct |
14 |
Incorrect |
3 ms |
2796 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
55 ms |
13420 KB |
Output is correct |
2 |
Correct |
55 ms |
13036 KB |
Output is correct |
3 |
Correct |
36 ms |
11500 KB |
Output is correct |
4 |
Correct |
9 ms |
4204 KB |
Output is correct |
5 |
Correct |
9 ms |
3564 KB |
Output is correct |
6 |
Correct |
16 ms |
5100 KB |
Output is correct |
7 |
Correct |
2 ms |
2796 KB |
Output is correct |
8 |
Correct |
25 ms |
7148 KB |
Output is correct |
9 |
Correct |
34 ms |
9580 KB |
Output is correct |
10 |
Correct |
2 ms |
2796 KB |
Output is correct |
11 |
Correct |
54 ms |
10604 KB |
Output is correct |
12 |
Correct |
62 ms |
11884 KB |
Output is correct |
13 |
Correct |
2 ms |
2796 KB |
Output is correct |
14 |
Incorrect |
2 ms |
2668 KB |
Output isn't correct |
15 |
Halted |
0 ms |
0 KB |
- |