이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
// In the name of the God
#include <bits/stdc++.h>
#define ll long long
// #define int long long
#define pb push_back
#define F first
#define S second
#define mp make_pair
#define pii pair <int, int>
#define smin(x, y) (x) = min((x), (y))
#define smax(x, y) (x) = max((x), (y))
#define all(x) (x).begin(), (x).end()
using namespace std;
const int inf = 1e9+7;
const int mod = 998244353;
const int maxn = 1e4+5;
int n, k, r, cnt[maxn], dp[maxn][maxn][2], tmp[maxn][2];
vector <pii> adj[maxn];
void dfs(int v = r, int p = r) {
cnt[v] = 1;
for (pii e : adj[v]) {
int u = e.F;
if (u == p) continue;
dfs(u, v);
for (int i = 1; i <= cnt[v]; i++) {
smin(tmp[i][0], dp[v][i][0]);
smin(tmp[i][1], dp[v][i][1]);
for (int j = 1; j <= cnt[u]; j++) {
smin(tmp[i+j][0], dp[v][i][0]+dp[u][j][0]+e.S*2);
smin(tmp[i+j][1],
min(dp[v][i][0]+dp[u][j][1]+e.S, dp[v][i][1]+dp[u][j][0]+e.S*2));
}
}
cnt[v] += cnt[u];
for (int i = 1; i <= cnt[v]; i++) {
dp[v][i][0] = tmp[i][0], dp[v][i][1] = tmp[i][1];
tmp[i][0] = tmp[i][1] = inf;
}
}
}
int32_t main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
memset(tmp, 63, sizeof tmp);
cin >> n >> k >> r;
r--;
for (int i = 1; i < n; i++) {
int v, u, w; cin >> v >> u >> w;
adj[--v].pb(mp(--u, w));
adj[u].pb(mp(v, w));
}
dfs();
cout << dp[r][k][1] << '\n';
// for (int i = 1; i <= n; i++) cout << dp[r][i][1] << '\n';
return 0;
}
# | 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... |