# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
1257071 | _dtq_ | Museum (CEOI17_museum) | C++20 | 421 ms | 784052 KiB |
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define sz(x) (ll)(x.size())
#define all(v) v.begin(), v.end()
#define se second
#define fi first
using namespace std;
const int N = 1e4 + 2;
const int INF = 1e9;
int n, i, k, x, u, v, w, dp[N][N][2], sz[N];
vector<pair<int, int>>vec[N];
void calc( ll x, ll root )
{
sz[x] = 1;
dp[x][1][1] = dp[x][1][0] = 0;
for(auto k : vec[x])
{
if(k.fi != root){ calc(k.fi, x);
sz[x] += sz[k.fi];
for( int i = sz[x]; i >= 1; i -- )
{
for( int j = sz[k.fi]; j >= 1; j -- )
{
if(i <= j) break;
dp[x][i][0] = min(dp[x][i][0], dp[x][i - j][0] + dp[k.fi][j][1] + 2 * k.se);
dp[x][i][0] = min(dp[x][i][0], dp[x][i - j][1] + dp[k.fi][j][0] + k.se);
dp[x][i][1] = min(dp[x][i][1], dp[x][i - j][1] + dp[k.fi][j][1] + 2 * k.se);
}
}
}
}
}
int main()
{
#define TN "d"
if(fopen(TN ".in", "r"))
{
freopen(TN ".in", "r", stdin);
freopen(TN ".out", "w", stdout);
}
cin.tie(0)->sync_with_stdio(0);
cin >> n >> k >> x;
for( i = 1; i < n; i ++ )
{
cin >> u >> v >> w;
vec[u].pb({v, w});
vec[v].pb({u, w});
}
for( i = 1; i <= n; i ++ )
for(int j = 1; j <= n; j ++ ) dp[i][j][0] = dp[i][j][1] = INF;
calc(x, 0);
cout << dp[x][k][0];
return 0;
}
/*
*/
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |