多重背包 (poj 1014)

 题目:Dividing

 题意:6种重量的的石头,每个给定数量,用总重的一半去装,问能否装满.

 

#include <iostream>
#include <algorithm>
#include <stdlib.h>
#include <time.h>
#include <cmath>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <queue>
#include <stack>
#include <set>

#define c_false ios_base::sync_with_stdio(false); cin.tie(0)
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3f
#define zero_(x,y) memset(x , y , sizeof(x))
#define zero(x) memset(x , 0 , sizeof(x))
#define MAX(x) memset(x , 0x3f ,sizeof(x))
#define swa(x,y) {LL s;s=x;x=y;y=s;}
using namespace std ;
#define N 20005

const double PI = acos(-1.0);
typedef long long LL ;
int dp[6*N];
int i = 0;
int W,a[10];
void ZeroOnePack(int siz, int prise){
    for(int i = W;i>=siz;i--)
        dp[i] = max(dp[i], dp[i-siz] + prise);
}

void CompletePack(int siz, int prise){
    for(int i = siz; i<= W; i++)
        dp[i] = max(dp[i], dp[i-siz]+prise);
}

void MultiplePack(int siz, int prise, int num){
    if(siz*num >= W){
        CompletePack(siz,prise);
        return ;
    }
    int k = 1;
    while(k<num){
        ZeroOnePack(k*siz, k*prise);
        num-=k;
        k*=2;
    }
    ZeroOnePack(num*siz, num*prise);
}

bool cal(){
    if(W%2 == 0) W/=2;
    else return false;
    for(int i =1 ; i <= 6; i++ ){
        MultiplePack(i,i,a[i]);
    }
    if(dp[W] == W)
        return true;
    else
        return false;
}

int main(void){
    //freopen("in.txt","r",stdin);
    while(cin>>a[1]>>a[2]>>a[3]>>a[4]>>a[5]>>a[6]){
        zero(dp);
        W = 0;
        for(int j = 1;j <= 6;j++){
            W+=j*a[j];
        }
        if(a[6]== 0 &&a[1] == 0 &&a[2] == 0&& a[3] == 0&& a[4] ==0&& a[5] ==0)
            break;
        printf("Collection #%d:\n",++i);
        if(cal())
            puts("Can be divided.\n");
        else
            puts("Can‘t be divided.\n");
    }
    return 0;
}
时间: 2024-10-04 17:16:04

多重背包 (poj 1014)的相关文章

多重背包——POJ 1742

对应POJ题目:点击打开链接 Coins Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 30387   Accepted: 10325 Description People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dollar.One day Tony opened his money-box and found

(多重背包)poj 1276

Cash Machine Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 28364   Accepted: 10125 Description A Bank plans to install a machine for cash withdrawal. The machine is able to deliver appropriate @ bills for a requested cash amount. The m

(转)多重背包

文章作者:Yx.Ac   文章来源:勇幸|Thinking (http://www.ahathinking.com)   转载请注明,谢谢合作. --- 前面已经回顾了01背包和完全背包,本节回顾多重背包的几种实现形式,主要有以下几方面内容: ==多重背包问题定义 & 基本实现 ==多重背包二进制拆分实现 ==防火防盗防健忘 ======================================== 多重背包问题定义 & 基本实现 问题:有个容量为V大小的背包,有很多不同重量weig

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

http://poj.org/problem?id=1014 题意:6个物品,每个物品都有其价值和数量,判断是否能价值平分. 思路: 多重背包.利用二进制来转化成0-1背包求解. 1 #include<iostream> 2 #include<string> 3 #include<cstring> 4 #include<cstdio> 5 #include<algorithm> 6 using namespace std; 7 8 const i

POJ 1014 Dividing (多重背包)

Dividing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 58921   Accepted: 15200 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 marbl

POJ 1014 Dividing【多重背包+二进制优化】

大意: 价值1, 2, 3, ……, 6的物品分别a1, a2, ……, a5, a6件 问能否把这些物品分成两份,使其具有相同的价值(所有物品必须全部用上) 分析: 给个物品有多件,即多重背包 只要看能不能将这些物品拼成   总价值 的 一半就可以了 转化为01背包是用二进制优化,否则会超时 代码: 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 using namespace std;

POJ 1014 Dividing 【DP 之 多重背包 / 二进制优化】

Language: Default Dividing Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 63647   Accepted: 16488 Description Marsha and Bill own a collection of marbles. They want to split the collection among themselves so that both receive an equal

POJ 1014 Dividing(多重背包)

Dividing 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

POJ 1014 Dividing 背包

这道题使用多重背包,不过其实我也不太明白为什么叫这个名字. 因为感觉不是什么多重,而是物体的分解问题. 就是比如一个物体有数量限制,比如是13,那么就需要把这个物体分解为1, 2, 4, 6 如果这个物体有数量为25,那么就分解为1, 2, 4, 8, 10 看出规律吗,就是分成2的倍数加上位数,比如6 = 13 - 1 - 2 - 4, 10 = 25 - 1 - 2 - 4 - 8,呵呵,为什么这么分解? 因为这样分解之后就可以组合成所有1到13的数,为25的时候可以组合成所有1到25的数啦

POJ 1837 Balance (多重背包计数)

Balance Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 11706   Accepted: 7305 Description Gigel has a strange "balance" and he wants to poise it. Actually, the device is different from any other ordinary balance. It orders two arms