poj 2754 Similarity of necklaces 2

http://poj.org/problem?id=2754

先把low--up 转化为0--(up-low)

然后变成背包  背包的关键在于多重背包用二进制优化

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<map>
#include<vector>
#include<stack>
#include<set>
#include<map>
#include<queue>
#include<algorithm>
#include<cmath>
#define LL long long
//#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std;
const int INF=0x3f3f3f3f;
const int N=205;
const int M=100005;
int low[N],up[N],p[N],m[N];
int dp[M];
void pack01(int cost,int weight,int V)
{
    for(int v=V;v>=cost;--v)
    dp[v]=max(dp[v],dp[v-cost]+weight);
}
void packComplete(int cost,int weight,int V)
{
    for(int v=cost;v<=V;++v)
    dp[v]=max(dp[v],dp[v-cost]+weight);
}
void packMultiple(int cost,int weight,int num,int V)
{
    if((num+1)*cost>V)
    packComplete(cost,weight,V);
    else
    {
        int k=1;
        while(k<=num)
        {
            pack01(cost*k,weight*k,V);
            num-=k;
            k=k<<1;
        }
        pack01(cost*num,weight*num,V);
    }
}
int main()
{
    //freopen("data.in","r",stdin);
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        int k=0,res=0;
        for(int i=1;i<=n;++i)
        {
            scanf("%d %d %d %d",&p[i],&m[i],&low[i],&up[i]);
            k+=(0-low[i])*m[i];
            res+=(low[i]-0)*p[i];
        }
        memset(dp,0x80,sizeof(dp));
        dp[0]=0;
        for(int i=1;i<=n;++i)
        packMultiple(m[i],p[i],up[i]-low[i],k);
        printf("%d\n",dp[k]+res);
    }
    return 0;
}

  

 

转载于:https://www.cnblogs.com/liulangye/archive/2013/02/19/2917216.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值