이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define f first
#define s second
#define pb push_back
#define ep emplace
#define eb emplace_back
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
#define rall(x) x.rbegin(), x.rend()
#define uniquev(v) sort(all(v)), (v).resize(unique(all(v)) - (v).begin())
#define mem(f,x) memset(f , x , sizeof(f))
#define sz(x) (int)(x).size()
#define __lcm(a, b) (1ll * ((a) / __gcd((a), (b))) * (b))
#define mxx *max_element
#define mnn *min_element
#define cntbit(x) (ll)__builtin_popcountll(x)
#define len(x) (int)(x.length())
const int N = 1e4 + 10;
int dp[2][N][N], sz[N];
vector <pair <int, int>> ed[N];
int n, k;
void minimize(int &a, int b) {
if (a > b) {
a = b;
}
}
void dfs(int u, int v) {
sz[u] = 1;
dp[0][u][1] = dp[1][u][1] = dp[0][u][0] = dp[1][u][0] = 0;
for (auto x : ed[u]) {
if (x.f == v)
continue;
dfs(x.f, u);
for (int a = min(k, sz[u] + sz[x.f]); a >= 1; a--) {
for (int b = 0; b <= a; b++) {
minimize(dp[0][u][a], dp[0][x.f][b] + dp[0][u][a - b] + x.s * 2);
minimize(dp[1][u][a], dp[1][x.f][b] + dp[0][u][a - b] + x.s);
minimize(dp[1][u][a], dp[0][x.f][b] + dp[1][u][a - b] + x.s * 2);
}
}
sz[u] += sz[x.f];
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
mem(dp, 0x3f);
int x;
cin >> n >> k >> x;
for (int i = 1; i < n; i++) {
int a, b, c;
cin >> a >> b >> c;
ed[a].pb({b, c});
ed[b].pb({a, c});
}
dfs(x, x);
cout << min(dp[0][x][k], dp[1][x][k]) << '\n';
return 0;
}
/*
11 8 3
1 3 3
3 2 5
6 4 5
1 11 3
9 1 2
9 10 2
3 7 10
6 7 1
7 8 1
7 5 1
*/
# | 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... |