#include "dreaming.h"
#include <vector>
#include <algorithm>
#include <iostream>
int const nmax = 100000;
int const inf = 1000000000;
using ll = long long;
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) < (b)) ? (b) : (a))
struct Edge{
int to;
int cost;
};
std::vector<Edge> g[1 + nmax];
int seen[1 + nmax], dp[1 + nmax];
void dfs(int node, int parent){
seen[node] = 1;
for(int h = 0; h < g[node].size(); h++){
Edge e = g[node][h];
if(parent == e.to)
g[node].erase(g[node].begin() + h);
}
for(int h = 0; h < g[node].size(); h++){
Edge e = g[node][h];
dfs(e.to, node);
dp[node] = MAX(dp[node], dp[e.to] + e.cost);
}
}
int dfs2(int node, int parent, int acc){
int result = MAX(acc, dp[node]);
std::vector<int> pref(g[node].size()), suff(g[node].size());
for(int h = 0; h < g[node].size(); h++){
Edge e = g[node][h];
pref[h] = suff[h] = dp[e.to] + e.cost;
}
for(int h = 0; h < g[node].size(); h++)
if(0 < h)
pref[h] = MAX(pref[h], pref[h - 1]);
for(int h = g[node].size() - 1; 0 <= h; h--)
if(h + 1 < g[node].size())
suff[h] = MAX(suff[h], suff[h + 1]);
for(int h = 0; h < g[node].size(); h++){
Edge e = g[node][h];
int newacc = acc;
if(0 < h)
newacc = MAX(newacc, pref[h - 1]);
if(h + 1 < g[node].size())
newacc = MAX(newacc, suff[h + 1]);
int result2 = dfs2(e.to, node, e.cost + newacc);
result = MIN(result, result2);
}
return result;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
for(int i = 0; i < M; i++){
g[A[i]].push_back({B[i], T[i]});
g[B[i]].push_back({A[i], T[i]});
}
std::vector<int> sol;
for(int i = 0; i < N; i++)
if(seen[i] == 0){
dfs(i, -1);
sol.push_back(dfs2(i, -1, 0));
}
sort(sol.begin(), sol.end());
if(sol.size() == 1)
return sol[0];
else if(sol.size() == 2)
return sol[0] + sol[1] + L;
else
return MAX(sol[sol.size() - 1] + sol[sol.size() - 2] + L, sol[sol.size() - 2] + sol[sol.size() - 3] + L * 2);
}
Compilation message
dreaming.cpp: In function 'void dfs(int, int)':
dreaming.cpp:22:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int h = 0; h < g[node].size(); h++){
~~^~~~~~~~~~~~~~~~
dreaming.cpp:27:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int h = 0; h < g[node].size(); h++){
~~^~~~~~~~~~~~~~~~
dreaming.cpp: In function 'int dfs2(int, int, int)':
dreaming.cpp:38:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int h = 0; h < g[node].size(); h++){
~~^~~~~~~~~~~~~~~~
dreaming.cpp:43:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int h = 0; h < g[node].size(); h++)
~~^~~~~~~~~~~~~~~~
dreaming.cpp:47:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(h + 1 < g[node].size())
~~~~~~^~~~~~~~~~~~~~~~
dreaming.cpp:50:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(int h = 0; h < g[node].size(); h++){
~~^~~~~~~~~~~~~~~~
dreaming.cpp:55:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if(h + 1 < g[node].size())
~~~~~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
94 ms |
15780 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
94 ms |
15780 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
94 ms |
15780 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
6516 KB |
Output is correct |
2 |
Correct |
32 ms |
6520 KB |
Output is correct |
3 |
Correct |
31 ms |
6520 KB |
Output is correct |
4 |
Correct |
32 ms |
6576 KB |
Output is correct |
5 |
Correct |
40 ms |
6452 KB |
Output is correct |
6 |
Correct |
37 ms |
7032 KB |
Output is correct |
7 |
Correct |
34 ms |
6776 KB |
Output is correct |
8 |
Correct |
30 ms |
6400 KB |
Output is correct |
9 |
Correct |
34 ms |
6516 KB |
Output is correct |
10 |
Correct |
34 ms |
6708 KB |
Output is correct |
11 |
Correct |
4 ms |
2680 KB |
Output is correct |
12 |
Correct |
19 ms |
3960 KB |
Output is correct |
13 |
Correct |
9 ms |
3956 KB |
Output is correct |
14 |
Correct |
9 ms |
3956 KB |
Output is correct |
15 |
Correct |
8 ms |
3956 KB |
Output is correct |
16 |
Correct |
9 ms |
3952 KB |
Output is correct |
17 |
Correct |
8 ms |
3700 KB |
Output is correct |
18 |
Correct |
10 ms |
4084 KB |
Output is correct |
19 |
Correct |
8 ms |
3956 KB |
Output is correct |
20 |
Correct |
4 ms |
2680 KB |
Output is correct |
21 |
Correct |
5 ms |
2680 KB |
Output is correct |
22 |
Correct |
4 ms |
2808 KB |
Output is correct |
23 |
Correct |
8 ms |
3956 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
94 ms |
15780 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
94 ms |
15780 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |