#include <bits/stdc++.h>
#define fi first
#define se second
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pill pair<int,ll>
#define mem(a, b) memset(a, b, sizeof(a))
#define MASK(i) (1LL << (i))
#define BIT(x, i) (((x) >> (i)) & 1)
#define name "test"
using namespace std;
const int N = 1e4 + 5;
const int mod = 1e9 + 7;
const ll oo = 1e18;
template <typename T1, typename T2> bool maxi(T1 &a, T2 b){if (a < b){a = b; return 1;} return 0;}
template <typename T1, typename T2> bool mini(T1 &a, T2 b){if (a > b){a = b; return 1;} return 0;}
int n;
int rooms;
int root;
int sz[N];
int g[N], g2[N];
vector <int> f[N];
vector <int> longest[N];
vector <pii> adj[N];
void load(){
cin.tie(0)->sync_with_stdio(0);
if (fopen(name".inp", "r")){
freopen(name".inp", "r", stdin);
freopen(name".out", "w", stdout);
}
}
void input(){
cin >> n >> rooms >> root;
for (int i = 1; i < n; i++){
int u, v, w;
cin >> u >> v >> w;
adj[u].push_back({v, w});
adj[v].push_back({u, w});
}
}
void dfs_sz(int u, int pre){
sz[u] = 1;
for (auto v : adj[u]) if (v.fi != pre){
dfs_sz(v.fi, u);
sz[u] += sz[v.fi];
}
f[u].resize(sz[u] + 1, mod);
longest[u].resize(sz[u] + 1, 0);
}
void combine(int u, int v, int w){
for (int i = 0; i <= min(rooms, sz[u] + sz[v]); i++)
g[i] = mod;
for (int i = 1; i <= min(rooms, sz[u]); i++)
for (int j = 0; j <= min(rooms - i, sz[v]); j++){
if (mini(g[i + j], f[u][i] + f[v][j] + w * 2))
g2[i + j] = max(longest[u][i], longest[v][j] + w);
}
for (int i = 0; i <= min(rooms, sz[u] + sz[v]); i++)
if (mini(f[u][i], g[i]))
longest[u][i] = g2[i];
}
void dfs(int u, int pre){
sz[u] = 1;
f[u][1] = 0;
for (auto it : adj[u]){
int v = it.fi;
int w = it.se;
if (v != pre){
dfs(v, u);
combine(u, v, w);
sz[u] += sz[v];
}
}
}
void solve(){
dfs_sz(root, root);
dfs(root, root);
cout << f[root][rooms] - longest[root][rooms];
}
int main(){
load();
input();
solve();
}
Compilation message
museum.cpp: In function 'void load()':
museum.cpp:34:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | freopen(name".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
museum.cpp:35:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
35 | freopen(name".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Incorrect |
1 ms |
1116 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
3676 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
8 ms |
3676 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1116 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Incorrect |
1 ms |
1116 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |