标签:return mem include print min int ++ for ret
树形dp
#include<bits/stdc++.h> #define N 305 using namespace std; struct LEB{ int to,nxt,w; }e[N<<1]; int f[N][N][2]; int tmp[N][2]; int head[N]; int n,m,k,x,z,y,cnt; void Build(int x,int y,int z){ e[++cnt]=(LEB){y,head[x],z}; head[x]=cnt; } void dp(int x,int y){ f[x][0][0]=f[x][1][1]=0; for (int i=head[x];i;i=e[i].nxt){ int z=e[i].to; if (z==y) continue; dp(z,x); memcpy(tmp,f[x],sizeof(tmp)); memset(f[x],63,sizeof(f[x])); for (int j=0;j<=k;j++) for(int t=0;t<=j;++t){ f[x][j][0]=min(f[x][j][0],min(f[z][t][0]+tmp[j-t][0]+(m==2)*e[i].w,f[z][t][1]+tmp[j-t][0])); f[x][j][1]=min(f[x][j][1],min(f[z][t][1]+tmp[j-t][1]+e[i].w,f[z][t][0]+tmp[j-t][1])); } } } int main(){ scanf("%d%d%d",&n,&m,&k); if (n-k<m-1){ printf("-1\n"); return 0; } for (int i=1;i<n;i++){ scanf("%d%d%d",&x,&y,&z); Build(x,y,z); Build(y,x,z); } memset(f,63,sizeof(f)); dp(1,1); printf("%d\n",f[1][k][1]); return 0; }
标签:return mem include print min int ++ for ret
原文地址:https://www.cnblogs.com/wjnclln/p/9602433.html