• 动态规划(二)HDU1114


    1.题目来源HDU1114

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

    Sample 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.

    2.题目分析

    题目首先给定一个空存钱罐的重量和这个存钱罐最多能装进去的重量,现在需要在不打破这个存钱罐的情况下猜测里面最少的钱每种钱的数量不做限制,条件是必须装满,同时给出每种钱币的价值和重量。
    参考背包九讲,这属于完全背包问题,初始状态为装满的情况,所以放钱币的策略就变成了放几个问题,有的钱币组合不合理就设置得比较大。

    3.代码如下

    #include<iostream>
    #include<algorithm>
    using namespace std;
    const int num = 10005;
    const int abc = 9999999999;
    int main(void)
    {
        //(1 <= P <= 50000, 1 <= W <= 10000)
        //1 <= E <= F <= 10000, N (1 <= N <= 500)(coins)
        int dp[num], C[num], W[num], T, N, E, F, V, i, j;
        cin >> T;
        while (T-- > 0) {
            cin >> E >> F;   // the weight of an empty pig and of the pig filled with coins
            cin >> N;       //the number of various coins used in the given currency. 
            V = F - E;      //the rest of the space
                            //C is the value of the coin in monetary units, W is it's weight in grams. 
            for (i = 1; i <= N; i++)
                cin >> W[i] >> C[i];    
            dp[0] = 0;
            for (i = 1; i <= V; i++) dp[i] = abc;
            for (i = 1; i <= N; i++) {
                for (j = C[i]; j <= V; j++) {
                    dp[j] = min(dp[j], dp[j - C[i]] + W[i]);
                }
            }
            if (dp[V] == abc) cout << "This is impossible."<<endl;
            else cout << "The minimum amount of money in the piggy-bank is "<<dp[V]<<"."<<endl;
        }
        return 0;
    }
  • 相关阅读:
    Linux 软件安装到哪里合适,目录详解
    python如何判断1个列表中所有的数据都是相等的?
    web接口开发基础知识-什么是web接口?
    MIME TYPE是什么?
    jenkins展示html测试报告(不使用html publisher)
    【转】Java虚拟机的JVM垃圾回收机制
    Map 排序
    sql in 和 exist的区别
    distinct和group by 去掉重复数据分析
    sql执行机制
  • 原文地址:https://www.cnblogs.com/FlyerBird/p/9052551.html
Copyright © 2020-2023  润新知