多重背包模板~

#include <stdio.h>
#include <string.h> int a[7];
int f[100005];
int v, k; void ZeroOnePack(int cost, int weight)
{
for (int i = v; i >= cost; i--)
if (f[i - cost] + weight > f[i])
f[i] = f[i - cost] + weight;
}
void CompletePack(int cost, int weight)
{
for (int i = cost; i <= v; i++)
if (f[i - cost] + weight > f[i])
f[i] = f[i - cost] + weight;
}
void MultiplePack(int cost, int weight, int amount)
{
if (cost * amount >= v) CompletePack(cost, weight);
else {
for (int k = 1; k < amount;) {
ZeroOnePack(k * cost, k * weight);
amount -= k;
k <<= 1;
}
ZeroOnePack(amount * cost, amount * weight);
}
} int main()
{
int sum;
int cas = 0;
while (1) {
sum = 0;
for (int i = 1; i < 7; i++) {
scanf("%d", &a[i]);
sum += a[i] * i;
}
if (sum == 0) break;
if (sum % 2 == 1) {
printf("Collection #%d:\nCan't be divided.\n\n", ++cas);
continue;
} else {
v = sum / 2;
memset(f, 0, sizeof(f));
for (int i = 1; i < 7; i++)
MultiplePack(i, i, a[i]);
if (f[v] == v)
printf("Collection #%d:\nCan be divided.\n\n", ++cas);
else
printf("Collection #%d:\nCan't be divided.\n\n", ++cas);
}
}
return 0;
}

Hdu 1059 Dividing & Zoj 1149 & poj 1014 Dividing(多重背包)的更多相关文章

  1. POJ 1014 Dividing(多重背包)

    Dividing   Description Marsha and Bill own a collection of marbles. They want to split the collectio ...

  2. POJ 1014 Dividing(多重背包+二进制优化)

    http://poj.org/problem?id=1014 题意:6个物品,每个物品都有其价值和数量,判断是否能价值平分. 思路: 多重背包.利用二进制来转化成0-1背包求解. #include&l ...

  3. DFS(DP)---POJ 1014(Dividing)

    原题目:http://poj.org/problem?id=1014 题目大意: 有分别价值为1,2,3,4,5,6的6种物品,输入6个数字,表示相应价值的物品的数量,问一下能不能将物品分成两份,是两 ...

  4. Coins(HDU 2844):一个会超时的多重背包

    Coins  HDU 2844 不能用最基础的多重背包模板:会超时的!!! 之后看了二进制优化了的多重背包. 就是把多重转变成01背包: 具体思路见:http://www.cnblogs.com/tt ...

  5. POJ 1014 Dividing

    Dividing Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 66032 Accepted: 17182 Descriptio ...

  6. POJ 1014 Dividing 多重背包

    Dividing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 63980   Accepted: 16591 Descri ...

  7. POJ 1014 Dividing (多重可行性背包)

    题意 有分别价值为1,2,3,4,5,6的6种物品,输入6个数字,表示相应价值的物品的数量,问一下能不能将物品分成两份,是两份的总价值相等,其中一个物品不能切开,只能分给其中的某一方,当输入六个0是( ...

  8. POJ 1014 Dividing(多重背包, 倍增优化)

    Q: 倍增优化后, 还是有重复的元素, 怎么办 A: 假定重复的元素比较少, 不用考虑 Description Marsha and Bill own a collection of marbles. ...

  9. POJ 1014 Dividing 背包

    二进制优化,事实上是物体的分解问题. 就是比方一个物体有数量限制,比方是13,那么就须要把这个物体分解为1. 2, 4, 6 假设这个物体有数量为25,那么就分解为1, 2, 4. 8. 10 看出规 ...

随机推荐

  1. pip使用国内镜像/源的方法

    一.修改配置文件 pip配置文件的路径如下,没有的话新建一个: linux/msys2等是:用户目录/.pip/pip.confwindows是:用户目录/pip/pip.ini 用户目录通过下行命令 ...

  2. CHANGE NOTEPAD DEFAULT CODE TO UTF-8

    Windows记事本新建文本文档编码默认为ANSI 修改步骤: 新建一个文本文档,不输入任何内容,然后"另存为",将编码由默认的ANSI修改为UTF-8,并将新文档命名为templ ...

  3. XgCalendar日历插件动态添加参数

    在使用xgcalendar日历插件的时候,参数数组并非只有类型.显示时间.时区等这些参数,还可以根据extParam自定义参数扩展搜索条件,例如根据用户Id搜索不同用户的日历信息,需要将用户的Id存在 ...

  4. block为什么要用copy,runtime的简单使用

    分享一篇文章:link

  5. ListActivity ListView 使用 介绍 用法

    ListActivity简单的说就是ListView和Activity的结合,跟ListView和Activity组合实现的没有什么很大的差别,主要是比较方便. 在实现时,要注意: 1.一般情况,Li ...

  6. 小学生之使用Mybatis反向生成dao,entity,xml

    本小学生刚进公司的时候,就一顿装逼,不管别人问我啥我都会说:"会"!毕竟在公司吗,什么都要装,不要别人看出你的底细.不过有一天,听说用Mybatis可以反向生成dao(第一次听说) ...

  7. 跨域信息传递postMessage

    var sendToParent = function(event, data, listener) { var message = { event: event, data: data, liste ...

  8. mongodb地理空间计算逻辑

    "1/地球半径"是怎么得出的 参考文档如下: http://janmatuschek.de/LatitudeLongitudeBoundingCoordinates http:// ...

  9. (@DBRef)spring-data-mongodb

    @DBRef用在哪些地方 已知的有 @DBRefprivate Shop product; @DBRefprivate List<Account> accounts;    如果不加@DB ...

  10. sqlyog使用注意事项

    在sqlyog中执行sql语句时,如果sql语句没有加limit 0,1000; sqlyog会自动查询从0开始的1000条,结果导致mysql慢查系统中显示的sql语句末尾加上了limit 0,10 ...