hdu 4003 求用k个机器人遍历一棵树的所有节点所需的最小花费

来源:互联网 发布:ieee 论文提交 知乎 编辑:程序博客网 时间:2024/06/03 22:01
#include<cstdio>#include<cstring>#define MIN(x,y) ((x)>(y)?(y):(x))struct node{int to,next,cost;}e[20000];int head[10000],dp[10000][12],cnt,n,s,k;void add_edge(int from,int to,int c){e[cnt].to=to;e[cnt].cost=c;e[cnt].next=head[from];head[from]=cnt++;}void dfs(int u,int v){for(int i=head[u];i!=-1;i=e[i].next){int to=e[i].to;int cost=e[i].cost;if(to==v)continue;dfs(to,u);for(int j=k;j>=0;j--){dp[u][j]+=dp[to][0]+2*cost;for(int p=1;p<=j;p++)dp[u][j]=MIN(dp[u][j],dp[u][j-p]+dp[to][p]+p*cost);}}}int main(){while(~scanf("%d%d%d",&n,&s,&k)){cnt=0;memset(head,-1,sizeof(head));memset(dp,0,sizeof(dp));for(int i=1;i<n;i++){int a,b,c;scanf("%d%d%d",&a,&b,&c);add_edge(a,b,c);add_edge(b,a,c);} dfs(s,-1);printf("%d\n",dp[s][k]);}}

0 0
原创粉丝点击