前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >动态规划(二)HDU1114

动态规划(二)HDU1114

作者头像
Enterprise_
发布2019-02-21 17:18:01
4330
发布2019-02-21 17:18:01
举报
文章被收录于专栏:小L的魔法馆小L的魔法馆

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.代码如下

代码语言:javascript
复制
#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;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018年03月10日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1.题目来源HDU1114
  • 2.题目分析
  • 3.代码如下
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档