您的位置:首页 > 其它

基础dp - 完全背包 F - Piggy-Bank HDU - 1114

2017-08-10 10:46 393 查看
题目链接:https://vjudge.net/contest/163018#problem/F

题意:

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

#include<cstdio>
#include<cstring>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<algorithm>
#include<iostream>
#include<set>
using namespace std;
const int mmax = 600100;
const int inf = 0x3f3f3f3f;
int dp[mmax],data[mmax],da[mmax];
int n;
int p[mmax],w[mmax];
int main()
{
int t;
cin>>t;
while(t--)
{
int s,e;
scanf("%d%d",&s,&e);
cin>>n;
int m = e-s;
for(int i=0;i<n;i++)
scanf("%d%d",&p[i],&w[i]);
memset(dp,inf,sizeof(dp));
dp[0] = 0;  //***重要
for(int i=0;i<n;i++)
{
for(int j=w[i];j<=m;j++)
{
dp[j] = min(dp[j],dp[j-w[i]]+p[i]);
}
}
if(dp[m]==inf)puts("This is impossible.");
else printf("The minimum amount of money in the piggy-bank is %d.\n",dp[m]);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  dp acm 动态规划