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