标签:des style blog http io color ar os sp
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5516 Accepted Submission(s): 3280
思路: 不同于普通的0/1背包,种类之间有约束,需要在有限的范围内进行0/1背包dP
代码:
1 #include<cstring> 2 #include<cstdio> 3 #define NN 205 4 struct node{ 5 int a,b; 6 }sac[NN]; 7 bool map[NN][NN]; 8 int dp[NN][NN]; //dp[][]; 表示攻击j城市剩余i次攻击 9 int n,m; 10 int max(int a,int b) 11 { 12 if(a>b) return a ; 13 return b; 14 } 15 16 void dfs(int st ,int time){ 17 int i; 18 dp[st][1]=sac[st].b; 19 for( i=1;i<=n;i++) 20 { 21 if(map[st][i]){ 22 if(1<time) dfs(i,time-1); 23 //0/1背包 24 for(int j=time ;j >=1 ;j--) { 25 int v=j+1; 26 for(int k=1; k<=j ;k++){ 27 dp[st][v]=max(dp[st][v-k]+dp[i][k],dp[st][v]); 28 } 29 } 30 } 31 } 32 } 33 int main() 34 { 35 while(~scanf("%d%d",&n,&m)&&m+n){ 36 memset(map,0,sizeof map); 37 memset(dp,0,sizeof dp); 38 for(int i=1;i<=n;i++){ 39 scanf("%d%d",&sac[i].a,&sac[i].b); 40 map[sac[i].a][i]=1; 41 } 42 dfs(0,m+1); 43 printf("%d\n",dp[0][m+1]); 44 } 45 return 0; 46 }
hdu 1561 The more, The Better (树上背包)
标签:des style blog http io color ar os sp
原文地址:http://www.cnblogs.com/gongxijun/p/4101676.html