标签:
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 579 Accepted Submission(s): 179
#include<iostream> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<algorithm> #include<cstdlib> #include<vector> using namespace std; #define LL long long const long long INF=20000*100000000LL; vector<int> e[2010],w[2010]; int n,k; LL dp[2010][100],temp[100]; void dfs(int u,int fa) { for(int i=0;i<e[u].size();i++) { int v=e[u][i]; if(v==fa) continue; dfs(v,u); for(int j=0;j<=k;j++) temp[j]=dp[u][j]; for(int j=0;j<=k;j++) { for(int t=0;t<=j;t++) temp[j]=min(temp[j],dp[u][j-t]+dp[v][t]+t*(k-t)*w[u][i]*2); } for(int j=0;j<=k;j++) dp[u][j]=temp[j]; } } int main() { int tt,x,y,z; scanf("%d",&tt); while(tt--) { scanf("%d%d",&n,&k); for(int i=1;i<=n;i++) { e[i].clear(); w[i].clear(); } for(int i=1;i<n;i++) { scanf("%d%d%d",&x,&y,&z); e[x].push_back(y); e[y].push_back(x); w[x].push_back(z); w[y].push_back(z); } for(int i=1;i<=n;i++) { for(int j=0;j<=k;j++) { if(j<=1) dp[i][j]=0; else dp[i][j]=INF; } } dfs(1,-1); printf("%I64d\n",dp[1][k]); } return 0; }
标签:
原文地址:http://www.cnblogs.com/a972290869/p/4422877.html