POJ1014:Dividing(多重背包)
http://poj.org/problem?id=1014
Description
Input
The last line of the input file will be "0 0 0 0 0 0"; do not process this line.
Output
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,2,3,4,5,6的6种物品,输入6个数字,表示相应价值的物品的数量,问一下能不能将物品分成两份,使两份的总价值相等,其中一个物品不能切开,只能分给其中的某一方,当输入六个0是(即没有物品了),这程序结束,总物品的总个数不超过20000
输出:每个测试用例占三行:
第一行: Collection #k: k为第几组测试用例
第二行:是否能分(具体形式见用例)
第三行:空白(必须注意,否则PE)
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
using namespace std;
int dp[],w[],v[];
int V,K=;
void wpack(int w)
{
for(int i=w; i<=V; i++)
{
if(dp[i-w]+w>dp[i])
dp[i]=dp[i-w]+w;
}
}
void pack1(int w)
{
for(int i=V; i>=w; i--)
{
if(dp[i-w]+w>dp[i])
dp[i]=dp[i-w]+w;
}
}
void Mul(int w,int num)
{
if(w*num>=V)
{
wpack(w);
return ;
}
int k=;
while(k<num)
{
pack1(k*w);
num-=k;
k=k*;
}
pack1(num*w);
}
int main()
{
while(scanf("%d%d%d%d%d%d",&w[],&w[],&w[],&w[],&w[],&w[])!=EOF)
{
K++;
V=w[]+w[]+w[]+w[]+w[]+w[];
if(V==) break;
V=w[]*+w[]*+w[]*+w[]*+w[]*+w[]*;
if(V%==)
{
printf("Collection #%d:\n",K);
printf("Can't be divided.\n\n");
}
else
{
V=V/;
memset(dp,,sizeof(dp));
for(int i=; i<=; i++)
{
Mul(i,w[i]);
}
if(dp[V]==V)
{
printf("Collection #%d:\n",K);
printf("Can be divided.\n\n");
}
else
{
printf("Collection #%d:\n",K);
printf("Can't be divided.\n\n");
}
}
}
return ;
}
POJ1014:Dividing(多重背包)的更多相关文章
- POJ1014(多重背包)
Dividing Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 65044 Accepted: 16884 Descri ...
- hdu 1059 Dividing(多重背包优化)
Dividing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Su ...
- poj1014 dp 多重背包
//Accepted 624 KB 16 ms //dp 背包 多重背包 #include <cstdio> #include <cstring> #include <i ...
- Hdu 1059 Dividing & Zoj 1149 & poj 1014 Dividing(多重背包)
多重背包模板- #include <stdio.h> #include <string.h> int a[7]; int f[100005]; int v, k; void Z ...
- poj1014 hdu1059 Dividing 多重背包
有价值为1~6的宝物各num[i]个,求能否分成价值相等的两部分. #include <iostream> #include <cstring> #include <st ...
- hdu 1059 Dividing 多重背包
点击打开链接链接 Dividing Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- Dividing 多重背包 倍增DP
Dividing 给出n个物品的价值和数量,问是否能够平分.
- POJ 1014 Dividing 多重背包
Dividing Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 63980 Accepted: 16591 Descri ...
- POJ 1014 Dividing(多重背包, 倍增优化)
Q: 倍增优化后, 还是有重复的元素, 怎么办 A: 假定重复的元素比较少, 不用考虑 Description Marsha and Bill own a collection of marbles. ...
- POJ 1014 / HDU 1059 Dividing 多重背包+二进制分解
Problem Description Marsha and Bill own a collection of marbles. They want to split the collection a ...
随机推荐
- free -m 下的含义
如下显示free是显示的当前内存的使用,-m的意思是M字节来显示内容.我们来一起看看. $ free -mtotal used free shared buffers cachedMem: 1002 ...
- Python 编码规范(Google)
Python 编码规范(Google) https://blog.csdn.net/q469587851/article/details/54096093 Python 风格规范(Google) 本项 ...
- springbatch---->springbatch的使用(四)
这里我们重点学习一下springbatch里面的各种监听器的使用,以及job参数的传递.追求得到之日即其终止之时,寻觅的过程亦即失去的过程. springbatch的监听器 一.JOB LISTENE ...
- 【Spring Boot && Spring Cloud系列】Spring Boot的启动器Starter
Spring Boot的内置Servlet Container: Name Servlet Version Java Version Tomcat8 3.1 Java 7+ Tomcat7 3.0 J ...
- Bat脚本实现监控进程功能
脚本不间断监控notepad.exe进程是否执行,若停止,则自动重启该进程,程序如下: @echo off set _task = notepad.exe set _svr = c:\windows\ ...
- window下node更新
打开cmd查看你之前node版本安装的路径,where node: 直接去官网下载与你电脑系统(32位还是64位)对应的最新的mis版本,安装在上述路径中覆盖即可. 注意:windows上并不支持n模 ...
- windows中cmd--->进入到别的磁盘
方法:直接敲: f: 不要加cd,在同一个磁盘的盘符下用cd.
- 教你写gulp plugin
前端开发近两年工程化大幅飙升.随着Nodejs大放异彩,静态文件处理不再需要其他语言辅助.主要的两大工具即为基于文件的grunt,基于流的gulp.简单来说,如果需要的只是文件处理,gulp绝对首选. ...
- Mac下使用Fiddler(转载园友小坦克)
Fiddler是用C#开发的. 所以Fiddler不能在Mac系统中运行. 没办法直接用Fiddler来截获MAC系统中的HTTP/HTTPS, Mac 用户怎么办呢? Fiddler可以允 ...
- ftok函数
ftok函数 系统建立IPC通讯(消息队列.信号量和共享内存)时必须指定一个ID值.通常情况下,该id值通过ftok函数得到. ftok原型 头文件: #include <sys/types.h ...