poj_1276

源地址:http://poj.org/problem?id=1276

题目大意就是有cash这么多的现金,然后有n种面值的钱,问用这n种面值的钱,最多能够凑成不超过cash这么多的现金。

一看就是一道多重背包的裸题,用下多重背包的公式就好~

#include<stdio.h>
#include<iostream>
#include<string>
#include<string.h>
#include<algorithm>
#include<iomanip>
#include<vector>
#include<time.h>
#include<queue>
#include<stack>
#include<iterator>
#include<math.h>
#include<stdlib.h>
#include<limits.h>
#include<set>
#include<map>
//#define ONLINE_JUDGE
#define eps 1e-8
#define INF 0x7fffffff
#define FOR(i,a) for((i)=0;i<(a);(i)++)
#define MEM(a) (memset((a),0,sizeof(a)))
#define sfs(a) scanf("%s",a)
#define sf(a) scanf("%d",&a)
#define sfI(a) scanf("%I64d",&a)
#define pf(a) printf("%d\n",a)
#define pfI(a) printf("%I64d\n",a)
#define pfs(a) printf("%s\n",a)
#define sfd(a,b) scanf("%d%d",&a,&b)
#define sft(a,b,c)scanf("%d%d%d",&a,&b,&c)
#define for1(i,a,b) for(int i=(a);i<b;i++)
#define for2(i,a,b) for(int i=(a);i<=b;i++)
#define for3(i,a,b)for(int i=(b);i>=a;i--)
#define MEM1(a) memset(a,0,sizeof(a))
#define MEM2(a) memset(a,-1,sizeof(a))
const double PI=acos(-1.0);
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
using namespace std;
#define ll __int64
int n,m;
#define Mod 1000000007
#define N 510
#define M 1000100
int cash;
int c[15];
int w[15];
int dp[100010];
void ZeroOnePack(int cost,int weight){
	for(int v=cash;v>=cost;v--)
		dp[v] = Max(dp[v],dp[v-cost]+weight);
}
void CompletePack(int cost,int weight){
	for(int v=cost;v<=cash;v++)
		dp[v] = Max(dp[v],dp[v-cost]+weight);
}
void MultiplePack(int cost,int weight,int amount){
	if(cost*amount >= cash){
		CompletePack(cost,weight);
		return;
	}
	int k = 1;
	while(k<amount){
		ZeroOnePack(k*cost,k*weight);
		amount -= k;
		k = k*2;
	}
	ZeroOnePack(amount*cost,amount*weight);
}
int main(){
#ifndef ONLINE_JUDGE
    freopen("in.txt","r",stdin);
//  freopen("out.txt","w",stdout);
#endif
    while(sfd(cash,n)!=EOF){
    	for(int i=1;i<=n;i++){
    		sfd(c[i],w[i]);
    	}
    	memset(dp,0,sizeof dp);
    	for(int i=1;i<=n;i++)
    		MultiplePack(w[i],w[i],c[i]);
    	printf("%d\n",dp[cash]);
    }
return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值