您的位置:首页 > 其它

sdau三 1018

2016-05-31 08:26 337 查看
问题:

有一扑满,有多种硬币,每种硬币价值重量不同,每种硬币数量无限。要把扑满装满,求扑满中的最少能装多少钱。

input:

输入n,后有n组事例,输入空扑满和装满钱的扑满的重量,输入m,后有m组硬币,各有价值v与重量w

out:

输出最小价值或输出不可能(若装不满)

simple input:

3
10 110
2
1 1
30 50
10 110
2
1 1
50 30
1 6
2
10 3
20 4

simple 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.
分析:

一定要装满的完全背包问题。求最小值,所以关键是要将dp数组初始化为正无穷,然后套完全背包模板就好

代码:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int p,w,dp[10001];
const int INF=100000000000;
int main(){
//freopen("s.txt","r",stdin);
int t;
cin>>t;
while(t--){
int s1,s2,s,n,i,j;
cin>>s1>>s2;
s=s2-s1;
cin>>n;
for(i=1;i<=s;i++)
dp[i]=INF;
for(i=1;i<=n;i++){
cin>>p>>w;
for(j=w;j<=s;j++)
dp[j]=min(dp[j],dp[j-w]+p);
}
if(dp[s]!=INF)
cout<<"The minimum amount of money in the piggy-bank is "<<dp[s]<<"."<<endl;
else
cout<<"This is impossible."<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: