标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 19391 Accepted Submission(s): 5446
1 #include <cstdio> 2 #include <cstring> 3 #include <queue> 4 #include <iostream> 5 #include <cmath> 6 #include <algorithm> 7 using namespace std ; 8 int dp[65000] ; 9 int r[10] ; 10 int sum, num ; 11 int c = 1 ; 12 void _zeroone(int c, int w)//0-1背包处理单件物品 13 { 14 for(int i = sum; i >= c; --i) 15 dp[i] = max(dp[i], dp[i - c] + w) ; 16 } 17 void _compl(int c, int w)//完全背包处理单件物品 18 { 19 for(int i = c; i <= sum; ++i) 20 dp[i] = max(dp[i], dp[i - c] + w) ; 21 } 22 void _mult(int c, int w, int amount) 23 { 24 if(c * amount >= sum) { _compl(c, w) ; return ; }//满足条件时,该件物品可以直接当完全背包时的单件物品处理 25 int k = 1 ; 26 while(k < amount)//二进制分解 27 { 28 _zeroone(c * k,w * k) ; 29 amount -= k ; 30 k = k * 2 ; 31 } 32 _zeroone(amount * c, amount * w) ; 33 } 34 int main() 35 { 36 while(1) 37 { 38 memset(dp, 0, sizeof dp) ; 39 sum = 0 ; 40 int flag = 0 ; 41 for(int i = 1; i <= 6; ++i){ 42 scanf("%d",&r[i]) ; 43 if(r[i] != 0) flag = 1 ; 44 sum += i * r[i] ; 45 } 46 if(!flag) break ; 47 printf("Collection #%d:\n",c++) ; 48 if(sum & 1) { printf("Can‘t be divided.\n\n") ; continue ;} 49 sum /= 2 ; 50 for(int i = 1; i <= 6; ++i)//将多重背包每件分开转换为完全背包或0-1背包中的单件物品处理 51 _mult(i,i,r[i]) ; 52 if(dp[sum] == sum ) printf("Can be divided.\n") ; 53 else printf("Can‘t be divided.\n") ; 54 printf("\n") ; 55 } 56 return 0 ; 57 }
标签:
原文地址:http://www.cnblogs.com/orchidzjl/p/4507704.html