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;
}
题意:存钱罐可以往里面放一些价值小的钱,但是时间久了就不知道里面有多少钱了,除非你打破它。现在给出空罐子的重量和最满能装到多重,然后给出每种硬币的价值和重量,我们要在不打破它的情况下确认罐子里最少有多少钱。
思路:很贴近生活。每种硬币数量不限,所以是完全背包。条件必须在装满的情况下,求最小价值,那么初始化变为装满的情况(背包九讲)和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 1568
- HDU1290
- HDU1568(Fobonacci公式)
- HDU ACM Step 2.2.2 Joseph(约瑟夫环问题)
- HDU 1405
- HDU 1297
- hdu 1205
- hdu 2087
- hdu 1016
- HDU 4898 The Revenge of the Princess’ Knight ( 2014 Multi-University Training Contest 4 )
- HDU 5592 ZYB's Premutation 线段树(查找动态区间第K大)
- HDU 5240 Exam (好水的题)
- HDU5237 Base64 大模拟
- HDU 1000
- HDU 1001
- 2015-11-11 hdu新生赛 A题(AC)
- 2015-11-11 hdu新生赛 C题(结束后一发AC)
- 2015-11-11 hdu新生赛 E题(结束后一发AC)
- 2015-11-11 hdu新生赛 F题(结束后一发AC)
- hdu-5385