#include "closing.h"
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
int n, x, y, ch[202020], ans;
ll k, need[202020], dis[202020];
vector< pair<int, ll> > adj[202020];
int L;
vector< pair<int, ll> > path;
void find_path(int v) {
for (int i = 0; i < adj[v].size(); i++) {
if (path.back().first == y) return;
int u = adj[v][i].first; ll w = adj[v][i].second;
if (ch[u] == 1) continue;
ch[u] = 1;
path.push_back({u, w});
find_path(u);
if (path.back().first != y) path.pop_back();
}
}
pair<int, ll> in[3030][3030];
void inner() {
for (int r = 0; r <= L; r++) {
in[r][L].first = r + 2;
in[r][L].second = 0;
ll need[3030];
for (int i = 0; i <= r; i++) need[i] = dis[i] - dis[0];
for (int i = r + 1; i <= L; i++) need[i] = 0;
for (int i = 0; i <= L; i++) in[r][L].second += need[i];
for (int l = L - 1; l >= 0; l--) {
in[r][l].first = in[r][l + 1].first + 1;
in[r][l].second = in[r][l + 1].second + max(0LL, dis[L] - dis[l] - need[l]);
}
}
}
ll dp[404040], nxt[404040];
void update(ll a, ll b) {
for (int i = 0; i <= 2*n; i++) {
ll val = dp[i];
if (i >= 1) val = min(val, dp[i - 1] + a);
if (i >= 2) val = min(val, dp[i - 2] + b);
nxt[i] = val;
}
for (int i = 0; i <= 2*n; i++) dp[i] = nxt[i];
}
void DFS(int v, ll a, ll b) {
for (int i = 0; i < adj[v].size(); i++) {
int u = adj[v][i].first; ll w = adj[v][i].second;
if (ch[u] == 1) continue;
ch[u] = 1;
update(a + w, b + w);
DFS(u, a + w, b + w);
}
}
void outer(int r, int l) {
for (int i = 0; i < n; i++) ch[i] = 0;
for (int i = 0; i <= L; i++) ch[path[i].first] = 1;
for (int i = 0; i <= 2*n; i++) dp[i] = 2e18;
dp[0] = 0;
/*
for (int i = 0; i <= r; i++) {
ll a = min(dis[i] - dis[0], dis[L] - dis[i]);
ll b = max(dis[i] - dis[0], dis[L] - dis[i]);
DFS(path[i].first, a, b);
}
for (int i = l; i <= L; i++) {
ll a = min(dis[i] - dis[0], dis[L] - dis[i]);
ll b = max(dis[i] - dis[0], dis[L] - dis[i]);
DFS(path[i].first, a, b);
}
*/
if (r < l) {
for (int i = 0; i <= r; i++) {
DFS(path[i].first, dis[i] - dis[0], 2e18);
}
for (int i = l; i <= L; i++) {
DFS(path[i].first, dis[L] - dis[i], 2e18);
}
}
else {
for (int i = 0; i <= l - 1; i++) {
DFS(path[i].first, dis[i] - dis[0], 2e18);
}
for (int i = l; i <= r; i++) {
ll a = min(dis[i] - dis[0], dis[L] - dis[i]);
ll b = max(dis[i] - dis[0], dis[L] - dis[i]);
DFS(path[i].first, a, b);
}
for (int i = r + 1; i <= L; i++) {
DFS(path[i].first, dis[L] - dis[i], 2e18);
}
}
}
int max_score(int N, int X, int Y, ll K, vector<int> U, vector<int> V, vector<int> W) {
n = N, x = X, y = Y, k = K;
ans = 0;
for (int i = 0; i < n; i++) adj[i].clear();
path.clear();
for (int i = 0; i < (int)U.size(); i++) {
adj[U[i]].push_back({V[i], W[i]});
adj[V[i]].push_back({U[i], W[i]});
}
for (int i = 0; i < n; i++) ch[i] = 0;
path.push_back({x, 0});
ch[x] = 1;
find_path(x);
assert(path.back().first == y);
L = path.size() - 1;
for (int i = 1; i <= L; i++) dis[i] = dis[i - 1] + path[i].second;
inner();
for (int r = 0; r <= L; r++) {
for (int l = L; l >= 0; l--) {
if (k - in[r][l].second < 0) continue;
int val = in[r][l].first;
outer(r, l);
int idx = upper_bound(dp, dp + 2*n + 1, k - in[r][l].second) - dp - 1;
ans = max(ans, val + idx);
}
}
return ans;
}
Compilation message
closing.cpp: In function 'void find_path(int)':
closing.cpp:14:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
14 | for (int i = 0; i < adj[v].size(); i++) {
| ~~^~~~~~~~~~~~~~~
closing.cpp: In function 'void DFS(int, ll, ll)':
closing.cpp:52:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
52 | for (int i = 0; i < adj[v].size(); i++) {
| ~~^~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
99 ms |
64200 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14680 KB |
Output is correct |
2 |
Correct |
2 ms |
14680 KB |
Output is correct |
3 |
Correct |
2 ms |
14680 KB |
Output is correct |
4 |
Correct |
2 ms |
14680 KB |
Output is correct |
5 |
Correct |
2 ms |
14680 KB |
Output is correct |
6 |
Correct |
3 ms |
14684 KB |
Output is correct |
7 |
Correct |
2 ms |
14680 KB |
Output is correct |
8 |
Correct |
4 ms |
14684 KB |
Output is correct |
9 |
Correct |
2 ms |
14680 KB |
Output is correct |
10 |
Correct |
2 ms |
16728 KB |
Output is correct |
11 |
Correct |
3 ms |
16732 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14680 KB |
Output is correct |
2 |
Correct |
2 ms |
14680 KB |
Output is correct |
3 |
Correct |
2 ms |
14680 KB |
Output is correct |
4 |
Correct |
2 ms |
14680 KB |
Output is correct |
5 |
Correct |
2 ms |
14680 KB |
Output is correct |
6 |
Correct |
3 ms |
14684 KB |
Output is correct |
7 |
Correct |
2 ms |
14680 KB |
Output is correct |
8 |
Correct |
4 ms |
14684 KB |
Output is correct |
9 |
Correct |
2 ms |
14680 KB |
Output is correct |
10 |
Correct |
2 ms |
16728 KB |
Output is correct |
11 |
Correct |
3 ms |
16732 KB |
Output is correct |
12 |
Correct |
2 ms |
14936 KB |
Output is correct |
13 |
Correct |
2 ms |
14680 KB |
Output is correct |
14 |
Correct |
32 ms |
16728 KB |
Output is correct |
15 |
Correct |
2 ms |
14680 KB |
Output is correct |
16 |
Correct |
4 ms |
18780 KB |
Output is correct |
17 |
Correct |
5 ms |
18936 KB |
Output is correct |
18 |
Correct |
3 ms |
14680 KB |
Output is correct |
19 |
Correct |
8 ms |
14680 KB |
Output is correct |
20 |
Correct |
5 ms |
14936 KB |
Output is correct |
21 |
Execution timed out |
1031 ms |
29016 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14680 KB |
Output is correct |
2 |
Correct |
2 ms |
14680 KB |
Output is correct |
3 |
Correct |
2 ms |
14680 KB |
Output is correct |
4 |
Correct |
2 ms |
14680 KB |
Output is correct |
5 |
Correct |
2 ms |
14680 KB |
Output is correct |
6 |
Correct |
3 ms |
14684 KB |
Output is correct |
7 |
Correct |
2 ms |
14680 KB |
Output is correct |
8 |
Correct |
4 ms |
14684 KB |
Output is correct |
9 |
Correct |
2 ms |
14680 KB |
Output is correct |
10 |
Correct |
2 ms |
16728 KB |
Output is correct |
11 |
Correct |
3 ms |
16732 KB |
Output is correct |
12 |
Correct |
2 ms |
14936 KB |
Output is correct |
13 |
Correct |
2 ms |
14680 KB |
Output is correct |
14 |
Correct |
32 ms |
16728 KB |
Output is correct |
15 |
Correct |
2 ms |
14680 KB |
Output is correct |
16 |
Correct |
4 ms |
18780 KB |
Output is correct |
17 |
Correct |
5 ms |
18936 KB |
Output is correct |
18 |
Correct |
3 ms |
14680 KB |
Output is correct |
19 |
Correct |
8 ms |
14680 KB |
Output is correct |
20 |
Correct |
5 ms |
14936 KB |
Output is correct |
21 |
Execution timed out |
1031 ms |
29016 KB |
Time limit exceeded |
22 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14680 KB |
Output is correct |
2 |
Correct |
2 ms |
14680 KB |
Output is correct |
3 |
Correct |
2 ms |
14680 KB |
Output is correct |
4 |
Correct |
2 ms |
14680 KB |
Output is correct |
5 |
Correct |
2 ms |
14680 KB |
Output is correct |
6 |
Correct |
2 ms |
14680 KB |
Output is correct |
7 |
Correct |
2 ms |
14680 KB |
Output is correct |
8 |
Correct |
2 ms |
14684 KB |
Output is correct |
9 |
Correct |
2 ms |
14684 KB |
Output is correct |
10 |
Correct |
2 ms |
14680 KB |
Output is correct |
11 |
Correct |
2 ms |
14680 KB |
Output is correct |
12 |
Correct |
2 ms |
14680 KB |
Output is correct |
13 |
Correct |
2 ms |
14680 KB |
Output is correct |
14 |
Correct |
2 ms |
14680 KB |
Output is correct |
15 |
Correct |
2 ms |
14680 KB |
Output is correct |
16 |
Correct |
2 ms |
14680 KB |
Output is correct |
17 |
Correct |
3 ms |
14680 KB |
Output is correct |
18 |
Correct |
2 ms |
14680 KB |
Output is correct |
19 |
Correct |
2 ms |
14680 KB |
Output is correct |
20 |
Correct |
3 ms |
14684 KB |
Output is correct |
21 |
Correct |
2 ms |
14680 KB |
Output is correct |
22 |
Correct |
3 ms |
14680 KB |
Output is correct |
23 |
Correct |
2 ms |
14680 KB |
Output is correct |
24 |
Correct |
2 ms |
14680 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14680 KB |
Output is correct |
2 |
Correct |
2 ms |
14680 KB |
Output is correct |
3 |
Correct |
2 ms |
14680 KB |
Output is correct |
4 |
Correct |
2 ms |
14680 KB |
Output is correct |
5 |
Correct |
2 ms |
14680 KB |
Output is correct |
6 |
Correct |
2 ms |
14680 KB |
Output is correct |
7 |
Correct |
3 ms |
14684 KB |
Output is correct |
8 |
Correct |
2 ms |
14680 KB |
Output is correct |
9 |
Correct |
4 ms |
14684 KB |
Output is correct |
10 |
Correct |
2 ms |
14680 KB |
Output is correct |
11 |
Correct |
2 ms |
16728 KB |
Output is correct |
12 |
Correct |
3 ms |
16732 KB |
Output is correct |
13 |
Correct |
2 ms |
14936 KB |
Output is correct |
14 |
Correct |
2 ms |
14680 KB |
Output is correct |
15 |
Correct |
32 ms |
16728 KB |
Output is correct |
16 |
Correct |
2 ms |
14680 KB |
Output is correct |
17 |
Correct |
4 ms |
18780 KB |
Output is correct |
18 |
Correct |
5 ms |
18936 KB |
Output is correct |
19 |
Correct |
2 ms |
14680 KB |
Output is correct |
20 |
Correct |
2 ms |
14684 KB |
Output is correct |
21 |
Correct |
2 ms |
14684 KB |
Output is correct |
22 |
Correct |
2 ms |
14680 KB |
Output is correct |
23 |
Correct |
2 ms |
14680 KB |
Output is correct |
24 |
Correct |
2 ms |
14680 KB |
Output is correct |
25 |
Correct |
2 ms |
14680 KB |
Output is correct |
26 |
Correct |
2 ms |
14680 KB |
Output is correct |
27 |
Correct |
2 ms |
14680 KB |
Output is correct |
28 |
Correct |
2 ms |
14680 KB |
Output is correct |
29 |
Correct |
3 ms |
14680 KB |
Output is correct |
30 |
Correct |
2 ms |
14680 KB |
Output is correct |
31 |
Correct |
2 ms |
14680 KB |
Output is correct |
32 |
Correct |
3 ms |
14684 KB |
Output is correct |
33 |
Correct |
2 ms |
14680 KB |
Output is correct |
34 |
Correct |
3 ms |
14680 KB |
Output is correct |
35 |
Correct |
2 ms |
14680 KB |
Output is correct |
36 |
Correct |
2 ms |
14680 KB |
Output is correct |
37 |
Correct |
2 ms |
14680 KB |
Output is correct |
38 |
Correct |
3 ms |
14880 KB |
Output is correct |
39 |
Correct |
2 ms |
14684 KB |
Output is correct |
40 |
Correct |
6 ms |
14684 KB |
Output is correct |
41 |
Correct |
3 ms |
14680 KB |
Output is correct |
42 |
Correct |
2 ms |
14684 KB |
Output is correct |
43 |
Correct |
6 ms |
16728 KB |
Output is correct |
44 |
Correct |
8 ms |
14680 KB |
Output is correct |
45 |
Correct |
4 ms |
14680 KB |
Output is correct |
46 |
Correct |
7 ms |
14680 KB |
Output is correct |
47 |
Correct |
5 ms |
14680 KB |
Output is correct |
48 |
Correct |
3 ms |
14680 KB |
Output is correct |
49 |
Correct |
3 ms |
14680 KB |
Output is correct |
50 |
Correct |
2 ms |
14684 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14680 KB |
Output is correct |
2 |
Correct |
2 ms |
14680 KB |
Output is correct |
3 |
Correct |
2 ms |
14680 KB |
Output is correct |
4 |
Correct |
2 ms |
14680 KB |
Output is correct |
5 |
Correct |
2 ms |
14680 KB |
Output is correct |
6 |
Correct |
2 ms |
14680 KB |
Output is correct |
7 |
Correct |
3 ms |
14684 KB |
Output is correct |
8 |
Correct |
2 ms |
14680 KB |
Output is correct |
9 |
Correct |
4 ms |
14684 KB |
Output is correct |
10 |
Correct |
2 ms |
14680 KB |
Output is correct |
11 |
Correct |
2 ms |
16728 KB |
Output is correct |
12 |
Correct |
3 ms |
16732 KB |
Output is correct |
13 |
Correct |
2 ms |
14936 KB |
Output is correct |
14 |
Correct |
2 ms |
14680 KB |
Output is correct |
15 |
Correct |
32 ms |
16728 KB |
Output is correct |
16 |
Correct |
2 ms |
14680 KB |
Output is correct |
17 |
Correct |
4 ms |
18780 KB |
Output is correct |
18 |
Correct |
5 ms |
18936 KB |
Output is correct |
19 |
Correct |
3 ms |
14680 KB |
Output is correct |
20 |
Correct |
8 ms |
14680 KB |
Output is correct |
21 |
Correct |
5 ms |
14936 KB |
Output is correct |
22 |
Execution timed out |
1031 ms |
29016 KB |
Time limit exceeded |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14680 KB |
Output is correct |
2 |
Correct |
2 ms |
14680 KB |
Output is correct |
3 |
Correct |
2 ms |
14680 KB |
Output is correct |
4 |
Correct |
2 ms |
14680 KB |
Output is correct |
5 |
Correct |
2 ms |
14680 KB |
Output is correct |
6 |
Correct |
2 ms |
14680 KB |
Output is correct |
7 |
Correct |
3 ms |
14684 KB |
Output is correct |
8 |
Correct |
2 ms |
14680 KB |
Output is correct |
9 |
Correct |
4 ms |
14684 KB |
Output is correct |
10 |
Correct |
2 ms |
14680 KB |
Output is correct |
11 |
Correct |
2 ms |
16728 KB |
Output is correct |
12 |
Correct |
3 ms |
16732 KB |
Output is correct |
13 |
Correct |
2 ms |
14936 KB |
Output is correct |
14 |
Correct |
2 ms |
14680 KB |
Output is correct |
15 |
Correct |
32 ms |
16728 KB |
Output is correct |
16 |
Correct |
2 ms |
14680 KB |
Output is correct |
17 |
Correct |
4 ms |
18780 KB |
Output is correct |
18 |
Correct |
5 ms |
18936 KB |
Output is correct |
19 |
Correct |
3 ms |
14680 KB |
Output is correct |
20 |
Correct |
8 ms |
14680 KB |
Output is correct |
21 |
Correct |
5 ms |
14936 KB |
Output is correct |
22 |
Execution timed out |
1031 ms |
29016 KB |
Time limit exceeded |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
14680 KB |
Output is correct |
2 |
Correct |
2 ms |
14680 KB |
Output is correct |
3 |
Correct |
2 ms |
14680 KB |
Output is correct |
4 |
Correct |
2 ms |
14680 KB |
Output is correct |
5 |
Correct |
2 ms |
14680 KB |
Output is correct |
6 |
Correct |
2 ms |
14680 KB |
Output is correct |
7 |
Correct |
3 ms |
14684 KB |
Output is correct |
8 |
Correct |
2 ms |
14680 KB |
Output is correct |
9 |
Correct |
4 ms |
14684 KB |
Output is correct |
10 |
Correct |
2 ms |
14680 KB |
Output is correct |
11 |
Correct |
2 ms |
16728 KB |
Output is correct |
12 |
Correct |
3 ms |
16732 KB |
Output is correct |
13 |
Correct |
2 ms |
14936 KB |
Output is correct |
14 |
Correct |
2 ms |
14680 KB |
Output is correct |
15 |
Correct |
32 ms |
16728 KB |
Output is correct |
16 |
Correct |
2 ms |
14680 KB |
Output is correct |
17 |
Correct |
4 ms |
18780 KB |
Output is correct |
18 |
Correct |
5 ms |
18936 KB |
Output is correct |
19 |
Correct |
3 ms |
14680 KB |
Output is correct |
20 |
Correct |
8 ms |
14680 KB |
Output is correct |
21 |
Correct |
5 ms |
14936 KB |
Output is correct |
22 |
Execution timed out |
1031 ms |
29016 KB |
Time limit exceeded |
23 |
Halted |
0 ms |
0 KB |
- |