hdu 1114Piggy-Bank(完全背包)
来源:互联网 发布:暗黑之光骑兵进阶数据 编辑:程序博客网 时间:2024/06/17 01:17
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!
310 11021 130 5010 11021 150 301 6210 320 4
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.
题意:给你一个存钱罐,给你这个存钱罐空的时候和装满的时候的重量,给你几种硬币的价值和重量,问你正好把这个存钱罐装满时,钱的最小值。
思路:因为一种钱币是可以无限次装的,所以是完全背包.
代码:
#include<iostream>#include<cstdio>using namespace std;int main(){ int dp[10005]; int p[510],w[510]; int t,e,f,n,W; int i,j; scanf("%d",&t); while(t--) { scanf("%d%d",&e,&f); W=f-e; scanf("%d",&n); for(i=0;i<n;i++) { scanf("%d%d",&p[i],&w[i]); } for(i=1;i<=W;i++) dp[i]=1000001;//题目要求最小的价值,所以赋初值尽量大一些
dp[0]=0; for(i=0;i<n;i++)//把每种物品都试完 { for(j=w[i];j<=W;j++)//把每个容量都试一下,看看要不要装这个东西 { if(dp[j]>(dp[j-w[i]]+p[i])) dp[j]=(dp[j-w[i]]+p[i]); } } if(dp[W]==1000001) printf("This is impossible.\n"); else printf("The minimum amount of money in the piggy-bank is %d.\n",dp[W]); }}
- hdu 1114 Piggy-Bank(完全背包)
- hdu 1114 Piggy-Bank(完全背包)
- hdu 1114 Piggy-Bank(完全背包)
- HDU 1114 Piggy-Bank (完全背包)
- HDU 1114 Piggy-Bank (完全背包)
- HDU 1114 Piggy-Bank (完全背包)
- hdu 1114 Piggy-Bank(完全背包)
- hdu 1114 Piggy-Bank (完全背包)
- HDU 1114 Piggy-Bank (完全背包)
- HDU 1114 Piggy-Bank(完全背包)
- HDU - 1114 - Piggy-Bank (完全背包)
- hdu 1114 Piggy-Bank(完全背包)
- hdu 1114 Piggy-Bank(完全背包)
- HDU 1114 Piggy-Bank(完全背包)
- HDU 1114 Piggy-Bank (完全背包)
- HDU--1114 Piggy-Bank(完全背包)
- HDU 1114 Piggy-Bank (完全背包)
- HDU 1114 Piggy-Bank (完全背包)
- 《Shell相关文章索引(1)》
- redis+lua+nginx打造CDN云加速
- IntelliJ IDEA WEB项目的部署配置
- RequestMapping注解源码
- 数组的引用和遍历
- hdu 1114Piggy-Bank(完全背包)
- Android 头像替换,解决华为手机取不到图片
- 【第十四周项目3 - 是否二叉排序树】
- Python Pandas读取中文格式CSV文件遇到的问题(UTF-8与UTF-8 BOM)
- 编译器的工作过程
- source tree Push出错
- 贝叶斯判别法做了,中途报了以下错误:
- eclipse中svn插件突然出现错误,不能连接svn了:Could not create the view: org.tigris.subversion.subclipse.ui.repositor
- SpringMVC笔记系列(3)——@RequestMapping请求value和method属性