#include "dreaming.h"
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100000;
int d[MAXN + 5];
int c1[MAXN + 5], c2[MAXN + 5];
int m1[MAXN + 5], m2[MAXN + 5];
int viz[MAXN + 5];
vector<pair<int, int> >G[MAXN + 5];
void dfs1(int node) {
viz[node] = 1;
for (auto it:G[node])
if (!viz[it.first]) {
dfs1(it.first);
c1[node] = max(c1[node], c1[it.first] + it.second);
}
}
int dfs2(int node, int cUp) {
viz[node] = 2;
d[node] = max(c1[node], cUp);
int ans = d[node];
m1[0] = 0;
for (int i = 0; i < G[node].size(); ++i) {
int u = G[node][i].first;
if (viz[u] == 2)
m1[i + 1] = m1[i];
else
m1[i + 1] = max(m1[i], c1[u] + G[node][i].second);
}
m2[G[node].size()] = 0;
for (int i = G[node].size() - 1; i >= 0; --i) {
int u = G[node][i].first;
if (viz[u] == 2)
m2[i] = m2[i + 1];
else
m2[i] = max(m2[i + 1], c1[u] + G[node][i].second);
}
for (int i = 0; i < G[node].size(); ++i) {
int u = G[node][i].first;
if (viz[u] != 2)
c2[u] = m1[i] + m2[i + 1];
}
for (int i = 0; i < G[node].size(); ++i) {
int u = G[node][i].first;
if (viz[u] != 2)
ans = min(ans, dfs2(u, max(cUp, c2[u]) + G[node][i].second));
}
return ans;
}
int travelTime(int N, int M, int L, int A[], int B[], int T[]) {
for (int i = 0; i < M; ++i) {
A[i]++;
B[i]++;
G[A[i]].push_back({B[i], T[i]});
G[B[i]].push_back({A[i], T[i]});
}
int ans = 0, ll = 0;
for (int i = 1; i <= N; ++i) {
if (viz[i] == 0) {
dfs1(i);
int aux = dfs2(i, 0);
ans = max(ans, ll + L + aux);
ll = min(max(ll, L + aux), max(aux, ll + L));
}
}
for (int i = 1; i <= N; ++i)
ans = max(ans, d[i]);
return ans;
}
/*int x[MAXN], y[MAXN], z[MAXN];
int main() {
freopen("date.in", "r", stdin);
freopen("date.out", "w", stdout);
int n, m, l;
scanf("%d%d%d", &n, &m, &l);
for (int i = 0; i < m; ++i)
scanf("%d%d%d", &x[i], &y[i], &z[i]);
printf("%d\n", travelTime(n, m, l, x, y, z));
}
*/
Compilation message
dreaming.cpp: In function 'int dfs2(int, int)':
dreaming.cpp:29:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < G[node].size(); ++i) {
~~^~~~~~~~~~~~~~~~
dreaming.cpp:44:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < G[node].size(); ++i) {
~~^~~~~~~~~~~~~~~~
dreaming.cpp:49:21: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for (int i = 0; i < G[node].size(); ++i) {
~~^~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
10488 KB |
Output is correct |
2 |
Correct |
60 ms |
10232 KB |
Output is correct |
3 |
Correct |
40 ms |
8568 KB |
Output is correct |
4 |
Correct |
12 ms |
3768 KB |
Output is correct |
5 |
Correct |
9 ms |
3456 KB |
Output is correct |
6 |
Correct |
16 ms |
4348 KB |
Output is correct |
7 |
Incorrect |
4 ms |
2816 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
10488 KB |
Output is correct |
2 |
Correct |
60 ms |
10232 KB |
Output is correct |
3 |
Correct |
40 ms |
8568 KB |
Output is correct |
4 |
Correct |
12 ms |
3768 KB |
Output is correct |
5 |
Correct |
9 ms |
3456 KB |
Output is correct |
6 |
Correct |
16 ms |
4348 KB |
Output is correct |
7 |
Incorrect |
4 ms |
2816 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
10488 KB |
Output is correct |
2 |
Correct |
60 ms |
10232 KB |
Output is correct |
3 |
Correct |
40 ms |
8568 KB |
Output is correct |
4 |
Correct |
12 ms |
3768 KB |
Output is correct |
5 |
Correct |
9 ms |
3456 KB |
Output is correct |
6 |
Correct |
16 ms |
4348 KB |
Output is correct |
7 |
Incorrect |
4 ms |
2816 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
32 ms |
6440 KB |
Output is correct |
2 |
Incorrect |
30 ms |
6504 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
10488 KB |
Output is correct |
2 |
Correct |
60 ms |
10232 KB |
Output is correct |
3 |
Correct |
40 ms |
8568 KB |
Output is correct |
4 |
Correct |
12 ms |
3768 KB |
Output is correct |
5 |
Correct |
9 ms |
3456 KB |
Output is correct |
6 |
Correct |
16 ms |
4348 KB |
Output is correct |
7 |
Incorrect |
4 ms |
2816 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
72 ms |
10488 KB |
Output is correct |
2 |
Correct |
60 ms |
10232 KB |
Output is correct |
3 |
Correct |
40 ms |
8568 KB |
Output is correct |
4 |
Correct |
12 ms |
3768 KB |
Output is correct |
5 |
Correct |
9 ms |
3456 KB |
Output is correct |
6 |
Correct |
16 ms |
4348 KB |
Output is correct |
7 |
Incorrect |
4 ms |
2816 KB |
Output isn't correct |
8 |
Halted |
0 ms |
0 KB |
- |