sdau三 1018

来源:互联网 发布:纹理合成算法 编辑:程序博客网 时间:2024/06/04 19:16

问题:

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

input:

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

out:

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

simple input:

310 11021 130 5010 11021 150 301 6210 320 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;}


0 0
原创粉丝点击