您的位置:首页 > 其它

hdu1114 Piggy-Bank(完全背包)

2016-02-12 23:18 351 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1114

题意:存钱罐可以往里面放一些价值小的钱,但是时间久了就不知道里面有多少钱了,除非你打破它。现在给出空罐子的重量和最满能装到多重,然后给出每种硬币的价值和重量,我们要在不打破它的情况下确认罐子里最少有多少钱。

思路:很贴近生活。每种硬币数量不限,所以是完全背包。条件必须在装满的情况下,求最小价值,那么初始化变为装满的情况(背包九讲)和max变为min。如果遍历完物品后满容量的dp值没有被改变则说明这些硬币无法组成这个容量。由于数据10000,所以就不用二维的了。

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

using namespace std;

typedef long long LL;

const int N = 10005;
const int INF = 0x3f3f3f3f;

int dp
;

int main()
{
// freopen("in.txt", "r", stdin);
int t, V, n, emp, fil;
int cost
, weight
;
scanf("%d", &t);
while(t--)
{
scanf("%d%d", &emp, &fil);
scanf("%d", &n);//物品数量
for(int i = 1; i <= n; i++)
scanf("%d%d", &weight[i], &cost[i]);
V = fil-emp;
dp[0] = 0;//装满
for(int i = 1; i <= V; i++)
dp[i] = INF;
for(int i = 1; i <= n; i++)
for(int j = cost[i]; j <= V; j++)
{
dp[j] = min(dp[j], dp[j-cost[i]]+weight[i]);
}
/* for(int i = 0; i <= V; i++)
printf("%d ", dp[i]);
printf("\n");*/
if(dp[V] == INF) printf("This is impossible.\n");
else printf("The minimum amount of money in the piggy-bank is %d.\n", dp[V]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hdu