#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
#define getar(ar,n) for(ll i=0;i<n;++i) cin>>ar[i]
#define show(n) cout<<n<<'\n'
#define all(v) v.begin(), v.end()
#define br cout<<"\n"
#define pb push_back
#define nl '\n'
#define yes cout<<"YES\n"
#define no cout<<"NO\n"
#define ret return
#define ll long long
#define ld long double
#define sza(x) ((int)x.size())
#define fi first
#define se second
const int mxN = 1e4 + 50;
const ll MOD = 1e9 + 7;
const ll INF = 1e9;
const ld EPS = 1e-9;
vector<pair<ll,ll>> adj[mxN];
ll dp[mxN][mxN],sz[mxN];
void dfs(ll u,ll par){
dp[u][0]=0;
sz[u]=1;
bool first_child=1;
for(auto it:adj[u]){
if(it.fi^par){
dfs(it.fi,u);
if(first_child){
for(ll j=1;j<=sz[it.fi];++j){
dp[u][j]=dp[it.fi][j-1]+it.se;
}
first_child=0;
}else{
for(ll j=0;j<sz[u];++j){
for(ll k=1;k<sz[it.fi];++k){
dp[u][j+k]=min(dp[u][j+k],dp[u][j]+dp[it.fi][k-1]+it.se);
}
}
}
sz[u]+=sz[it.fi];
}
}
}
void lets_go() {
ll n,k,x,u,v,w;cin>>n>>k>>x;
for(ll i=0;i<n-1;++i){
cin>>u>>v>>w;
adj[u].pb({v,w});
adj[v].pb({u,w});
}
memset(dp,0x3f,sizeof(dp));
dfs(x,-1);
show(dp[x][k-1]);
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
lets_go();
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... |