HDU 1114:Piggy-Bank ← 完全背包问题

【题目概述】
给定一个数T,表示T个样例。
针对每个样例,第一行两个值,分别为小猪存钱罐空时的重量E,满时的重量F。
第二行一个值,表示小猪存钱罐中钱币的种类N。
接下来N行,每行两个值,分别某种钱币的面值P,某种钱币的单个重量W。
求小猪存钱罐中钱币的最小值。

数据范围:1<= E<= F<=10000,1<=N<= 500,1<= P<= 50000, 1<= W<=10000

From:http://acm.hdu.edu.cn/showproblem.php?pid=1114


【Problem Description】
Before ACM can do anything, a budget must be prepared and the necessary financial support obtained. The main income for this action comes from Irreversibly Bound Money (IBM). The idea behind is simple. Whenever some ACM member has any small money, he takes all the coins and throws them into a piggy-bank. You know that this process is irreversible, the coins cannot be removed without breaking the pig. After a sufficiently long time, there should be enough cash in the piggy-bank to pay everything that needs to be paid.
But there is a big problem with piggy-banks. It is not possible to determine how much money is inside. So we might break the pig into pieces only to find out that there is not enough money. Clearly, we want to avoid this unpleasant situation. The only possibility is to weigh the piggy-bank and try to guess how many coins are inside. Assume that we are able to determine the weight of the pig exactly and that we know the weights of all coins of a given currency. Then there is some minimum amount of money in the piggy-bank that we can guarantee. Your task is to find out this worst case and determine the minimum amount of cash inside the piggy-bank. We need your help. No more prematurely broken pigs!


【Input】
The input consists of T test cases. The number of them (T) is given on the first line of the input file. Each test case begins with a line containing two integers E and F. They indicate the weight of an empty pig and of the pig filled with coins. Both weights are given in grams. No pig will weigh more than 10 kg, that means 1 <= E <= F <= 10000. On the second line of each test case, there is an integer number N (1 <= N <= 500) that gives the number of various coins used in the given currency. Following this are exactly N lines, each specifying one coin type. These lines contain two integers each, P and W (1 <= P <= 50000, 1 <= W <=10000). P is the value of the coin in monetary units, W is it's weight in grams.

【Output】
Print exactly one line of output for each test case. The line must contain the sentence "The minimum amount of money in the piggy-bank is X." where X is the minimum amount of money that can be achieved using coins with the given total weight. If the weight cannot be reached exactly, print a line "This is impossible.".

【Sample Output】
The minimum amount of money in the piggy-bank is 60.
The minimum amount of money in the piggy-bank is 100.
This is impossible.

【Code】

#include <bits/stdc++.h>
using namespace std;

const int inf=0x3f3f3f3f;
const int maxv=10005;
const int maxn=505;

int c[maxv];
int val[maxn],vol[maxn];

int main() {
	int T;
	scanf("%d",&T);
	while(T--) {
		int empty,full,V;
		scanf("%d%d",&empty,&full);
		V=full-empty;

		int n;
		scanf("%d",&n);
		for(int i=1; i<=n; i++) {
			scanf("%d%d",&val[i],&vol[i]);
		}

		memset(c,inf,sizeof(c));
		c[0]=0;
		for(int i=1; i<=n; i++) {
			for(int j=vol[i]; j<=V; j++) {
				c[j]=min(c[j],c[j-vol[i]]+val[i]);
			}
		}

		if(c[V]==inf) {
			printf("This is impossible.\n");
		} else {
			printf("The minimum amount of money in the piggy-bank is %d.\n",c[V]);
		}
	}

	return 0;
}


/*
in:
3
10 110
2
1 1
30 50
10 110
2
1 1
50 30
1 6
2
10 3
20 4

out:
The minimum amount of money in the piggy-bank is 60.
The minimum amount of money in the piggy-bank is 100.
This is impossible.
*/


【Reference】
https://blog.csdn.net/weixin_44756457/article/details/98470225
https://blog.csdn.net/pmt123456/article/details/53350173/





 

本项目是一个基于SSM(Spring+SpringMVC+MyBatis)框架和Vue.js前端技术的大学生第二课堂系统,旨在为大学生提供一个便捷、高效的学习和实践平台。项目包含了完整的数据库设计、后端Java代码实现以及前端Vue.js页面展示,适合计算机相关专业的毕设学生和需要进行项目实战练习的Java学习者。 在功能方面,系统主要实现了以下几个模块:用户管理、课程管理、活动管理、成绩管理和通知公告。用户管理模块支持学生和教师的注册、登录及权限管理;课程管理模块允许教师上传课程资料、设置课程时间,并由学生进行选课;活动管理模块提供了活动发布、报名和签到功能,鼓励学生参与课外实践活动;成绩管理模块则用于记录和查询学生的课程成绩和活动参与情况;通知公告模块则实时发布学校或班级的最新通知和公告。 技术实现上,后端采用SSM框架进行开发,Spring负责业务逻辑层,SpringMVC处理Web请求,MyBatis进行数据库操作,确保了系统的稳定性和扩展性。前端则使用Vue.js框架,结合Axios进行数据请求,实现了前后端分离,提升了用户体验和开发效率。 该项目不仅提供了完整的源代码和相关文档,还包括了详细的数据库设计文档和项目部署指南,为学习和实践提供了便利。对于基础较好的学习者,可以根据自己的需求在此基础上进行功能扩展和优化,进一步提升自己的技术水平和项目实战能力。
本项目是一个基于SSM(Spring+SpringMVC+MyBatis)后端框架与Vue.js前端框架的个人健康信息管理系统。该项目旨在为个人提供一个便捷、高效的健康信息管理平台,通过该系统,用户可以轻松地记录、查询和分析自己的健康数据。 在技术选型上,后端采用了Spring框架进行依赖注入和事务管理,SpringMVC负责处理Web请求,MyBatis则用于数据库操作,实现了前后端的分离。前端则使用了Vue.js框架,结合Vue Router进行页面路由管理,以及Vuex进行状态管理,确保了用户界面的响应性和交互性。 系统的主要功能包括用户注册与登录、健康数据录入(如身高、体重、血压等)、健康数据查询与分析、健康建议推送等。用户可以通过系统直观地查看自己的健康趋势,并根据系统提供的建议调整生活习惯。 数据库设计方面,系统采用了关系型数据库MySQL,设计了用户表、健康数据表等,确保了数据的完整性和安全性。此外,项目还包含了详细的文档和论文资料,为使用者提供了完整的开发背景和技术实现细节。 对于计算机相关专业的毕设学生而言,该项目不仅提供了一个实际的应用场景,还涵盖了从需求分析到系统实现的完整开发流程,是一个理想的实践平台。对于Java学习者,该项目也是一个很好的实战练习,可以在理解现有代码的基础上,进一步探索和实现更多功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值