// correct/solution-ayaze-knapsack-cube.cpp
// O(N^3) with high constant factor
// probably can AC N <= 500 if optimized e.g using plain array
#include "closing.h"
#include <bits/stdc++.h>
using namespace std;
const int kStateX = 1, kStateYActive = 2, kStateYInActive = 4;
const long long kInf = 4e18;
vector<vector<pair<int, int>>> adj_list;
vector<long long> dist_from_x, dist_from_y;
vector<bool> in_path;
vector<vector<vector<long long>>> dp_mat;
int N, Y;
void DfsDistance(int now, int prv, long long current_dist, vector<long long> &dist_vec, vector<int> &parent)
{
dist_vec[now] = current_dist;
parent[now] = prv;
for (pair<int, int> edge : adj_list[now])
{
int nxt = edge.first;
if (nxt == prv)
continue;
long long new_dist = edge.second + current_dist;
DfsDistance(nxt, now, new_dist, dist_vec, parent);
}
}
void DfsDP(int now, int prv)
{
for (int score = 0; score <= 2 * N; score++)
{
for (int mask = 0; mask < (1 << 3); mask++)
{
dp_mat[now][score][mask] = kInf;
}
}
for (int mask = 0; mask < (1 << 3); mask++)
{
if ((mask & kStateYActive) > 0 && (mask & kStateYInActive) > 0)
continue;
int score = 0;
long long val = 0;
if (mask & kStateX)
score++, val = max(val, dist_from_x[now]);
if (mask & kStateYActive)
score++, val = max(val, dist_from_y[now]);
dp_mat[now][score][mask] = val;
}
for (pair<int, int> edge : adj_list[now])
{
int nxt = edge.first;
if (nxt == prv)
continue;
DfsDP(nxt, now);
vector<vector<long long>> copy_current_dp = dp_mat[now];
for (int score = 0; score <= 2 * N; score++)
{
for (int mask = 0; mask < (1 << 3); mask++)
{
dp_mat[now][score][mask] = kInf;
}
}
for (int score = 0; score <= 2 * N; score++)
{
for (int mask = 0; mask < (1 << 3); mask++)
{
if ((mask & kStateYActive) > 0 && (mask & kStateYInActive) > 0)
continue;
int active_x = mask & kStateX;
int upper_mask = mask - active_x;
for (int child_score = 0; child_score + score <= 2 * N; child_score++)
{
for (int state_x = 0; state_x <= active_x; state_x++)
{ // we can keep X, or turn it off
// Y state do not change
dp_mat[now][score + child_score][mask] = min(
copy_current_dp[score][mask] + dp_mat[nxt][child_score][state_x | upper_mask],
dp_mat[now][score + child_score][mask]);
// currently reachable by Y, make child unreachable by Y
if (mask & kStateYActive)
{
dp_mat[now][score + child_score][mask] = min(
copy_current_dp[score][mask] + dp_mat[nxt][child_score][state_x],
dp_mat[now][score + child_score][mask]);
}
// this and above not reachable by Y,
// from here downward make it possible to be reachable by Y
if ((mask & kStateYInActive) > 0 && in_path[nxt])
{
dp_mat[now][score + child_score][mask] = min(
copy_current_dp[score][mask] + dp_mat[nxt][child_score][state_x | kStateYActive],
dp_mat[now][score + child_score][mask]);
}
}
}
}
}
}
// if it is Y, then it must be reachable by Y
if (now == Y)
{
for (int score = 0; score <= 2 * N; score++)
{
for (int mask = 0; mask < (1 << 3); mask++)
{
if ((mask & kStateYActive) == 0)
{
dp_mat[now][score][mask] = kInf;
}
}
}
}
}
int max_score(int _N, int X, int _Y, long long K,
std::vector<int> U, std::vector<int> V, std::vector<int> W)
{
N = _N;
Y = _Y;
adj_list.assign(N, vector<pair<int, int>>());
dist_from_x.resize(N);
dist_from_y.resize(N);
in_path.assign(N, false);
dp_mat.assign(N, vector<vector<long long>>(2 * N + 1, vector<long long>(1 << 3, 0)));
for (int i = 0; i < static_cast<int>(U.size()); i++)
{
adj_list[U[i]].push_back({V[i], W[i]});
adj_list[V[i]].push_back({U[i], W[i]});
}
vector<int> parent_rooted_x(N);
DfsDistance(Y, -1, 0, dist_from_y, parent_rooted_x);
DfsDistance(X, -1, 0, dist_from_x, parent_rooted_x);
for (int i = Y; i != X; i = parent_rooted_x[i])
{
in_path[i] = true;
}
DfsDP(X, -1);
int ret = 0;
for (int score = 0; score <= 2 * N; score++)
{
for (int mask = 0; mask < (1 << 3); mask++)
{
if (dp_mat[X][score][mask] <= K)
{
ret = max(ret, score);
}
}
}
return ret;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
1170 ms |
1833528 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
5 ms |
828 KB |
Output is correct |
7 |
Correct |
5 ms |
760 KB |
Output is correct |
8 |
Correct |
5 ms |
724 KB |
Output is correct |
9 |
Correct |
7 ms |
724 KB |
Output is correct |
10 |
Correct |
6 ms |
796 KB |
Output is correct |
11 |
Correct |
6 ms |
840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
5 ms |
828 KB |
Output is correct |
7 |
Correct |
5 ms |
760 KB |
Output is correct |
8 |
Correct |
5 ms |
724 KB |
Output is correct |
9 |
Correct |
7 ms |
724 KB |
Output is correct |
10 |
Correct |
6 ms |
796 KB |
Output is correct |
11 |
Correct |
6 ms |
840 KB |
Output is correct |
12 |
Correct |
51 ms |
2388 KB |
Output is correct |
13 |
Correct |
48 ms |
2344 KB |
Output is correct |
14 |
Correct |
33 ms |
2024 KB |
Output is correct |
15 |
Correct |
35 ms |
2004 KB |
Output is correct |
16 |
Correct |
44 ms |
2376 KB |
Output is correct |
17 |
Correct |
43 ms |
2388 KB |
Output is correct |
18 |
Correct |
14 ms |
392 KB |
Output is correct |
19 |
Execution timed out |
1079 ms |
51412 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
5 ms |
828 KB |
Output is correct |
7 |
Correct |
5 ms |
760 KB |
Output is correct |
8 |
Correct |
5 ms |
724 KB |
Output is correct |
9 |
Correct |
7 ms |
724 KB |
Output is correct |
10 |
Correct |
6 ms |
796 KB |
Output is correct |
11 |
Correct |
6 ms |
840 KB |
Output is correct |
12 |
Correct |
51 ms |
2388 KB |
Output is correct |
13 |
Correct |
48 ms |
2344 KB |
Output is correct |
14 |
Correct |
33 ms |
2024 KB |
Output is correct |
15 |
Correct |
35 ms |
2004 KB |
Output is correct |
16 |
Correct |
44 ms |
2376 KB |
Output is correct |
17 |
Correct |
43 ms |
2388 KB |
Output is correct |
18 |
Correct |
14 ms |
392 KB |
Output is correct |
19 |
Execution timed out |
1079 ms |
51412 KB |
Time limit exceeded |
20 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
1 ms |
340 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
1 ms |
300 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
296 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
5 ms |
828 KB |
Output is correct |
8 |
Correct |
5 ms |
760 KB |
Output is correct |
9 |
Correct |
5 ms |
724 KB |
Output is correct |
10 |
Correct |
7 ms |
724 KB |
Output is correct |
11 |
Correct |
6 ms |
796 KB |
Output is correct |
12 |
Correct |
6 ms |
840 KB |
Output is correct |
13 |
Correct |
51 ms |
2388 KB |
Output is correct |
14 |
Correct |
48 ms |
2344 KB |
Output is correct |
15 |
Correct |
33 ms |
2024 KB |
Output is correct |
16 |
Correct |
35 ms |
2004 KB |
Output is correct |
17 |
Correct |
44 ms |
2376 KB |
Output is correct |
18 |
Correct |
43 ms |
2388 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
1 ms |
340 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
1 ms |
344 KB |
Output is correct |
31 |
Correct |
1 ms |
300 KB |
Output is correct |
32 |
Correct |
1 ms |
340 KB |
Output is correct |
33 |
Correct |
1 ms |
296 KB |
Output is correct |
34 |
Correct |
1 ms |
340 KB |
Output is correct |
35 |
Correct |
1 ms |
340 KB |
Output is correct |
36 |
Correct |
1 ms |
292 KB |
Output is correct |
37 |
Correct |
2 ms |
340 KB |
Output is correct |
38 |
Correct |
10 ms |
596 KB |
Output is correct |
39 |
Correct |
38 ms |
2380 KB |
Output is correct |
40 |
Correct |
34 ms |
2132 KB |
Output is correct |
41 |
Correct |
39 ms |
2260 KB |
Output is correct |
42 |
Correct |
37 ms |
2208 KB |
Output is correct |
43 |
Correct |
37 ms |
2096 KB |
Output is correct |
44 |
Correct |
41 ms |
2132 KB |
Output is correct |
45 |
Correct |
39 ms |
2376 KB |
Output is correct |
46 |
Correct |
37 ms |
2260 KB |
Output is correct |
47 |
Correct |
48 ms |
2340 KB |
Output is correct |
48 |
Correct |
26 ms |
1748 KB |
Output is correct |
49 |
Correct |
10 ms |
852 KB |
Output is correct |
50 |
Correct |
2 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
5 ms |
828 KB |
Output is correct |
8 |
Correct |
5 ms |
760 KB |
Output is correct |
9 |
Correct |
5 ms |
724 KB |
Output is correct |
10 |
Correct |
7 ms |
724 KB |
Output is correct |
11 |
Correct |
6 ms |
796 KB |
Output is correct |
12 |
Correct |
6 ms |
840 KB |
Output is correct |
13 |
Correct |
51 ms |
2388 KB |
Output is correct |
14 |
Correct |
48 ms |
2344 KB |
Output is correct |
15 |
Correct |
33 ms |
2024 KB |
Output is correct |
16 |
Correct |
35 ms |
2004 KB |
Output is correct |
17 |
Correct |
44 ms |
2376 KB |
Output is correct |
18 |
Correct |
43 ms |
2388 KB |
Output is correct |
19 |
Correct |
14 ms |
392 KB |
Output is correct |
20 |
Execution timed out |
1079 ms |
51412 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
5 ms |
828 KB |
Output is correct |
8 |
Correct |
5 ms |
760 KB |
Output is correct |
9 |
Correct |
5 ms |
724 KB |
Output is correct |
10 |
Correct |
7 ms |
724 KB |
Output is correct |
11 |
Correct |
6 ms |
796 KB |
Output is correct |
12 |
Correct |
6 ms |
840 KB |
Output is correct |
13 |
Correct |
51 ms |
2388 KB |
Output is correct |
14 |
Correct |
48 ms |
2344 KB |
Output is correct |
15 |
Correct |
33 ms |
2024 KB |
Output is correct |
16 |
Correct |
35 ms |
2004 KB |
Output is correct |
17 |
Correct |
44 ms |
2376 KB |
Output is correct |
18 |
Correct |
43 ms |
2388 KB |
Output is correct |
19 |
Correct |
14 ms |
392 KB |
Output is correct |
20 |
Execution timed out |
1079 ms |
51412 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
5 ms |
828 KB |
Output is correct |
8 |
Correct |
5 ms |
760 KB |
Output is correct |
9 |
Correct |
5 ms |
724 KB |
Output is correct |
10 |
Correct |
7 ms |
724 KB |
Output is correct |
11 |
Correct |
6 ms |
796 KB |
Output is correct |
12 |
Correct |
6 ms |
840 KB |
Output is correct |
13 |
Correct |
51 ms |
2388 KB |
Output is correct |
14 |
Correct |
48 ms |
2344 KB |
Output is correct |
15 |
Correct |
33 ms |
2024 KB |
Output is correct |
16 |
Correct |
35 ms |
2004 KB |
Output is correct |
17 |
Correct |
44 ms |
2376 KB |
Output is correct |
18 |
Correct |
43 ms |
2388 KB |
Output is correct |
19 |
Correct |
14 ms |
392 KB |
Output is correct |
20 |
Execution timed out |
1079 ms |
51412 KB |
Time limit exceeded |
21 |
Halted |
0 ms |
0 KB |
- |