poj 1014 多重背包
来源:互联网 发布:水晶dj软件下载 编辑:程序博客网 时间:2024/05/22 14:41
题目:
Dividing
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 71372 Accepted: 18606
Description
Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal share of the marbles. This would be easy if all the marbles had the same value, because then they could just split the collection in half. But unfortunately, some of the marbles are larger, or more beautiful than others. So, Marsha and Bill start by assigning a value, a natural number between one and six, to each marble. Now they want to divide the marbles so that each of them gets the same total value. Unfortunately, they realize that it might be impossible to divide the marbles in this way (even if the total value of all marbles is even). For example, if there are one marble of value 1, one of value 3 and two of value 4, then they cannot be split into sets of equal value. So, they ask you to write a program that checks whether there is a fair partition of the marbles.
Input
Each line in the input file describes one collection of marbles to be divided. The lines contain six non-negative integers n1 , . . . , n6 , where ni is the number of marbles of value i. So, the example from above would be described by the input-line "1 0 1 2 0 0". The maximum total number of marbles will be 20000.
The last line of the input file will be "0 0 0 0 0 0"; do not process this line.
The last line of the input file will be "0 0 0 0 0 0"; do not process this line.
Output
For each collection, output "Collection #k:", where k is the number of the test case, and then either "Can be divided." or "Can't be divided.".
Output a blank line after each test case.
Output a blank line after each test case.
Sample Input
1 0 1 2 0 0 1 0 0 0 1 1 0 0 0 0 0 0
Sample Output
Collection #1:Can't be divided.Collection #2:Can be divided.
给定一堆 大理石 每一个大理石都有一个价值,是1-6之间的一个数 问能否将它们分成两堆,使得两堆价值之和相等
分析:
拉出多重背包定义:
有N种物品和一个容量为V的背包。第i种物品最多有n[i]件可用,每件费用是c[i],价值是w[i]。求解将哪些物品装入背包可使这些物品的费用总和不超过背包容量,且价值总和最大。
可见大理石的费用和价值是同一个值 分析样例可以推断出,如果容量为sum/2的背包能够完全装满,则可分 否则不可分
如第一个样例 sum=12 而容量为6的背包是无法用已有大理石装满的
背包容量上限为20000*6/2=60000
代码:
#include<iostream>#include<stdio.h>#include<string.h>using namespace std;const int inf=-0x3f3f3f3f;const int maxn=60020;int val[10],cost[10],num[10],dp[maxn];int n,v;inline void compak(int val,int cost){ for(int i=cost;i<=v;++i){ dp[i]=max(dp[i],dp[i-cost]+val); }}inline void zeroone(int val,int cost){ for(int i=v;i>=cost;--i){ dp[i]=max(dp[i],dp[i-cost]+val); }}inline void mulpak(int val,int cost,int num){ if(cost*num>=v){///这种物品费用总和大于V 相当于在V的范围内可以任取多件 转化为完全背包问题 compak(val,cost); return; } int k=1; while(k<num){ zeroone(k*val,k*cost); num-=k; k<<=1; } zeroone(num*val,num*cost);}int main(){///916K16MS int cas=0; for(int i=1;i<=8;++i) cost[i]=i,val[i]=i; while(scanf("%d%d%d%d%d%d",&num[1],&num[2],&num[3],&num[4],&num[5],&num[6])==6){ cas++; if(!num[1] && !num[2] &&!num[3] &&!num[4] &&!num[5] &&!num[6]) break; if(cas!=1) puts(""); v=0; for(int i=1;i<=6;++i) v+=num[i]*i; if(v%2){ printf("Collection #%d:\nCan't be divided.\n",cas); continue; } v/=2; for(int i=1;i<=v;++i) dp[i]=inf;///恰为 初始化为负数 dp[0]=0;///容量恰为0时价值总和为零 for(int i=1;i<=6;++i) mulpak(cost[i],cost[i],num[i]); if(dp[v]==v) printf("Collection #%d:\nCan be divided.\n",cas); else printf("Collection #%d:\nCan't be divided.\n",cas); } return 0;}
阅读全文
0 0
- 【多重背包】POJ 1014
- POJ 1014 多重背包
- poj 1014 多重背包
- poj 1014 多重背包
- poj 1014 多重背包
- poj 1014 - 多重背包
- poj 1014多重背包
- POJ 1014 -多重背包
- poj 1014(多重背包)
- poj 1014 多重背包
- poj 1014 Dividing 多重背包
- Poj 1014 Dividing//多重背包
- Poj 1014 Dividing(多重背包)
- POJ 1014 Dividing 多重背包
- POJ 1014 Dividing 多重背包
- 多重背包问题 POJ 1014
- POJ 1014 Dividing (多重背包)
- poj 1014 多重背包问题
- JavaScript基础_01
- 数据索引实现原理
- Inventor API: 2018版本开始API是在线帮助文档
- 数据结构与算法-揭秘
- 去除Toast 文字内容带的黑色底色
- poj 1014 多重背包
- poj 1422 Air Raid (最小路径覆盖白痴题)
- matlab 读取音频并显示波形
- 服务器看图详解 XManager VNC imgcat
- ios开发常用的框架
- React Native在Android平台运行gif的解决方法
- 1-1 sacc(scss)入门
- 1-2 Sass安装(windows版)
- 1-3 Sass 语法、编译、调试