poj 1384 piggy-bank

#include<iostream>
#include<fstream>
#include<stdio.h>
#include<algorithm>
#include<string.h>

using namespace std;
const int MAX = 0x6fffffff;
int t,e,f,n,net, m;
struct coin{
	int p;
	int w;
};
int c[10010];
int main(){
//	freopen("1.txt","r",stdin);
	scanf("%d",&t);
	while(t--){
		scanf("%d%d%d",&e,&f,&n);
		net = f - e;
		m = net + 1;
		memset(c,0,sizeof(c));
		for(int i = 0; i <= m; ++i){
			c[i] = MAX;
		}
		coin * pig = new coin[n+1];
		memset(pig,0,(n+1)*4);
		for(int i = 1; i <= n; ++i){
			scanf("%d%d",&pig[i].p,&pig[i].w);
		}
		for(int i = 1;i <= n; ++i){
			c[0] = 0;
			for(int j = pig[i].w; j <= net; ++j){
				if(c[j-pig[i].w] + pig[i].p < c[j]){
					c[j] = c[j-pig[i].w] + pig[i].p;
				}
			}
		}
		if(c[net] == MAX){
			printf("This is impossible.\n");
		}
		else{
			printf("The minimum amount of money in the piggy-bank is %d.\n",c[net]);
		}
	}
	return 0;
}





					
		

Key:

每次循环时c[0]记得初始化~

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值