zoukankan      html  css  js  c++  java
  • 完全背包——HDU-1114

    题目含义

    给出存钱罐空时和满时的重量,以及一些钱币的价值和重量

    每种钱可以取无数次,问装满存钱罐的最小价值

    题目分析

    很明显,一个完全背包,并且要求刚好装满,又是求最小值,就可以将dp[i]除dp[0]以外赋为INF

    题目代码

    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #include<algorithm>
    using namespace std;
    typedef long long LL;
    const int maxn=1e4+7;;
    const int INF=0x3f3f3f3f;
    int t,e,f,w,n;
    struct node{
        int wei;
        int val;
    }coin[507];
    int dp[maxn];
    int main(){
        scanf("%d",&t);
        while(t--){
            scanf("%d%d%d",&e,&f,&n);
            w=f-e;
            for(int i=1;i<=n;i++)
                scanf("%d%d",&coin[i].val,&coin[i].wei);
            memset(dp,INF,sizeof(dp));
            dp[0]=0;
            for(int i=1;i<=n;i++)
            for(int j=coin[i].wei;j<=w;j++){
                dp[j]=min(dp[j],dp[j-coin[i].wei]+coin[i].val);
            }
            if(dp[w]<INF)printf("The minimum amount of money in the piggy-bank is %d.
    ",dp[w]);
            else printf("This is impossible.
    ");
        }
        return 0;
    }
  • 相关阅读:
    软件测试大赛决赛简讯
    期末提交作业清单
    4月12日-4月19日任务清单
    20160405
    软件系统设计文档模板
    吐槽
    致我亲爱的学生
    HBase 环境搭建
    Zookeeper 环境搭建
    hive 部署
  • 原文地址:https://www.cnblogs.com/helman/p/11232108.html
Copyright © 2011-2022 走看看