Backpack dependent deformation --hdu4003

Thinking relatively strong, the code is very simple, dp [u] [j] represents the arrangement u j robots in the sub-tree, is not allowed to return to u

Note that the initial value of the transfer

/ * 
DP [u] [j] u is in the subtree returned by robot having j 
* / 
#include <bits / STDC ++ H.>
 The using  namespace STD;
 #define N 10005
 struct Edge { int to, NXT, W;} E [N << . 1 ];
 int head [N], TOT, n-, K, S;
 void the init () { 
    Memset (head, - . 1 , the sizeof head); TOT = 0 ; 
} 
void the Add ( int U, int V, int W) { 
    E [TOT] .to = V; E [TOT] .W = W; E [TOT] .nxt head = [U]; head [U] = TOT ++ ; 
} 
int DP [N] [20];
void dfs(int u,int pre){
    for(int i=head[u];i!=-1;i=e[i].nxt){
        int v=e[i].to;
        if(v==pre)continue;
        dfs(v,u);
        for(int j=k;j>=0;j--){
            dp[u][j]=2*e[i].w+dp[v][0]+dp[u][j];
            for(int l=1;l<=j;l++)
                dp[u][j]=min(dp[u][j],dp[u][j-l]+dp[v][l]+l*e[i].w);//给v子树l个机器人 
        }
    }
}

int main(){
    while(cin>>n>>s>>k){
        init();
        for(int i=1;i<n;i++){
            int u,v,w;scanf("%d%d%d",&u,&v,&w);
            add(u,v,w);add(v,u,w);
        }
        memset(dp,0,sizeof dp);
        dfs(s,s);
        int ans=0x3f3f3f3f;
        for(int i=0;i<=k;i++)
            ans=min(ans,dp[s][i]);
        cout<<ans<<'\n';
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/zsben991126/p/11387665.html