#include <bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define pb push_back
#define pa pair<int, int>
#define pall pair<ll, int>
#define fi first
#define se second
#define TASK "test"
#define Size(x) (int) x.size()
#define all(x) x.begin(), x.end()
using namespace std;
template<typename T1, typename T2> bool mini (T1 &a, T2 b) {if(a > b) a = b; else return 0; return 1;}
template<typename T1, typename T2> bool maxi (T1 &a, T2 b) {if(a < b) a = b; else return 0; return 1;}
const int MOD = 1e9 + 7;
const int LOG = 20;
const int maxn = 1e4 + 7;
const ll oo = 1e9 + 69;
int n, u, v, w, sz[maxn], dp[maxn][maxn], f[maxn][maxn][2], k, node;
vector<pa> g[maxn];
void dfs(int u, int par) {
for(auto e:g[u]) {
int v = e.fi;
if(v == par) continue;
dfs(v, u);
}
for(int i = 0; i <= Size(g[u]); i++)
for(int j = 0; j <= n; j++)
for(int en = 0; en < 2; en++) f[i][j][en] = oo;
f[0][1][0] = 0;
f[0][1][1] = 0;
f[0][0][1] = 0;
sz[u] = 1;
for(int i = 1; i <= Size(g[u]); i++) {
int v = g[u][i - 1].fi, w = g[u][i - 1].se;
if(v == par) {
for(int j = 0; j <= sz[u]; j++)
for(int en = 0; en < 2; en++) f[i][j][en] = f[i - 1][j][en];
continue;
}
for(int j = 0; j <= sz[u]; j++) {
for(int pre = 0; pre <= sz[v]; pre++) {
int cost = dp[v][pre] + (pre == 0 ? 0:w);
mini(f[i][j + pre][0], f[i - 1][j][0] + 2 * cost);
mini(f[i][j + pre][1], f[i - 1][j][0] + cost);
if(pre == 0) mini(f[i][j + pre][1], f[i - 1][j][1] + cost);
}
}
sz[u] += sz[v];
}
for(int i = 0; i <= n; i++) dp[u][i] = f[Size(g[u])][i][1];
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
//freopen(TASK".inp", "r", stdin);
//freopen(TASK".out", "w", stdout);
cin>>n>>k>>node;
for(int i = 1; i < n; i++) {
cin>>u>>v>>w;
g[u].pb({v, w});
g[v].pb({u, w});
}
dfs(node, 0);
cout<<dp[node][k];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
453 ms |
392940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
453 ms |
392940 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
0 ms |
596 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |