#include<bits/stdc++.h>
using namespace std;
const int maxn=10000+10;
struct yal{
int u,v,w;
int getad(int fu){
return (u^v^fu);
}
}alle[maxn];
vector<int>adj[maxn];
int sz[maxn],n,x,k,inf=1e9;
vector<int>dp[maxn],pd[maxn];
void vorod(){
cin>>n>>k>>x;
for(int i=0;i<n-1;i++){
cin>>alle[i].u>>alle[i].v>>alle[i].w;
adj[alle[i].u].push_back(i);
adj[alle[i].v].push_back(i);
}
}
void add(int u,int w){
for(int i=1;i<(int)dp[u].size();i++){
dp[u][i]+=w;
pd[u][i]+=2*w;
}
}
void merge(int u,int v){
int szu=dp[u].size(),szv=dp[v].size();
vector<int>fake(szu+szv-1,inf),fakea(szu+szv-1,inf);
for(int i=0;i<szu;i++){
for(int j=0;j<szv;j++){
fake[i+j]=min(fake[i+j],dp[u][i]+pd[v][j]);
fake[i+j]=min(fake[i+j],pd[u][i]+dp[v][j]);
fakea[i+j]=min(fakea[i+j],pd[u][i]+pd[v][j]);
}
}
dp[u].swap(fake);
pd[u].swap(fakea);
}
void dfs(int u,int par=-1){
dp[u].resize(2);
pd[u].resize(2);
for(auto x:adj[u]){
int v=alle[x].getad(u);
if(v==par){
continue;
}
dfs(v,u);
add(v,alle[x].w);
merge(u,v);
}
// cout<<"hey: "<<u<<" "<<par<<endl;
// for(auto x:dp[u]){
// cout<<x<<" ";
// }
// cout<<"\n";
// for(auto x:pd[u]){
// cout<<x<<" ";
// }
// cout<<"\n";
}
void solve(){
dfs(x);
}
void khor(){
cout<<dp[x][k]<<"\n";
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
// freopen("inp.txt","r",stdin);
vorod();
solve();
khor();
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
95 ms |
3920 KB |
Output is correct |
2 |
Correct |
96 ms |
4336 KB |
Output is correct |
3 |
Correct |
224 ms |
150620 KB |
Output is correct |
4 |
Correct |
139 ms |
48688 KB |
Output is correct |
5 |
Correct |
105 ms |
12952 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
95 ms |
3920 KB |
Output is correct |
2 |
Correct |
96 ms |
4336 KB |
Output is correct |
3 |
Correct |
224 ms |
150620 KB |
Output is correct |
4 |
Correct |
139 ms |
48688 KB |
Output is correct |
5 |
Correct |
105 ms |
12952 KB |
Output is correct |
6 |
Correct |
101 ms |
3436 KB |
Output is correct |
7 |
Correct |
177 ms |
88760 KB |
Output is correct |
8 |
Correct |
349 ms |
2760 KB |
Output is correct |
9 |
Correct |
241 ms |
2852 KB |
Output is correct |
10 |
Correct |
116 ms |
3372 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1112 KB |
Output is correct |
2 |
Correct |
1 ms |
1116 KB |
Output is correct |
3 |
Correct |
1 ms |
1116 KB |
Output is correct |
4 |
Correct |
1 ms |
1116 KB |
Output is correct |
5 |
Correct |
1 ms |
1116 KB |
Output is correct |
6 |
Correct |
95 ms |
3920 KB |
Output is correct |
7 |
Correct |
96 ms |
4336 KB |
Output is correct |
8 |
Correct |
224 ms |
150620 KB |
Output is correct |
9 |
Correct |
139 ms |
48688 KB |
Output is correct |
10 |
Correct |
105 ms |
12952 KB |
Output is correct |
11 |
Correct |
101 ms |
3436 KB |
Output is correct |
12 |
Correct |
177 ms |
88760 KB |
Output is correct |
13 |
Correct |
349 ms |
2760 KB |
Output is correct |
14 |
Correct |
241 ms |
2852 KB |
Output is correct |
15 |
Correct |
116 ms |
3372 KB |
Output is correct |
16 |
Correct |
97 ms |
4572 KB |
Output is correct |
17 |
Correct |
96 ms |
4436 KB |
Output is correct |
18 |
Correct |
146 ms |
56916 KB |
Output is correct |
19 |
Correct |
259 ms |
2872 KB |
Output is correct |
20 |
Correct |
111 ms |
3828 KB |
Output is correct |
21 |
Correct |
168 ms |
84048 KB |
Output is correct |
22 |
Correct |
105 ms |
4236 KB |
Output is correct |
23 |
Correct |
265 ms |
2888 KB |
Output is correct |
24 |
Correct |
102 ms |
3408 KB |
Output is correct |
25 |
Correct |
230 ms |
143580 KB |
Output is correct |