HDU - 1561 The more,The Better

Posted jjjjjjy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU - 1561 The more,The Better相关的知识,希望对你有一定的参考价值。

题目 HDU-1561 
The more, The Better

http://acm.hdu.edu.cn/showproblem.php?pid=1561
技术图片
 1 #include <iostream>
 2 #include <string.h>
 3 using namespace std;
 4 
 5 int n,m,a,ans=0,dp[205][205],vis[205],cnt[205],pre[205],f[205][205];
 6 
 7 //dp[i][j]表示的是是以i为根攻克 第j个城堡  j 必须是 i 的子树 
 8 // cnt[] 表示 宝物 个数 
 9  
10 struct node
11     int v;
12     int next;
13 c[205];
14 
15 void add(int u,int v)
16      c[ans].v=v;
17      c[ans].next=pre[u];
18      pre[u]=ans++;
19 
20 
21 void dfs(int root)
22     vis[root]=1;
23     for(int i=pre[root];i!=-1;i=c[i].next)
24         int u=c[i].v;
25         if(!vis[u])
26             dfs(u);
27             for(int k=m;k>=0;k--)
28                for(int j=0;j<=k;j++)
29               f[root][k]=max(f[root][k],f[root][k-j]+dp[u][j]);
30         
31     
32     for(int i=1;i<=m+1;i++)
33       dp[root][i]=f[root][i-1]+cnt[root];
34 
35 
36 int main()
37     while(cin>>n>>m&&n&&m)
38          ans=cnt[0]=0;
39          memset(f,0,sizeof(f));
40          memset(dp,0,sizeof(dp));
41          memset(pre,-1,sizeof(pre));
42          memset(vis,0,sizeof(vis));
43          for(int i=1;i<=n;i++)
44              cin>>a>>cnt[i];
45              add(a,i);
46          
47          dfs(0);
48          cout<<dp[0][m+1]<<endl;
49     
50     return 0;
51 

 







 

以上是关于HDU - 1561 The more,The Better的主要内容,如果未能解决你的问题,请参考以下文章