zoukankan      html  css  js  c++  java
  • HDU1114 PiggyBank 完全背包

    简单完全背包。

    代码如下:

    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    #define MAXN 500
    #define INF 0x3ffffff
    using namespace std;
    
    int E, F, N, W, dp[10005];
    
    struct Node
    {
        int p, w;    
    }e[MAXN+5];
    
    void c_bag(int x)
    {
        for (int i = e[x].w; i <= W; ++i) {
            if (dp[i-e[x].w] != INF) {
                dp[i] = min(dp[i], dp[i-e[x].w]+e[x].p);
            //    printf("i=%d, dp=%d\n", i, dp[i]);
            }
        }
    }
    
    void DP()
    {
        for (int i = 1; i <= N; ++i) {
            c_bag(i);
        }  
    }
    
    int main()
    {
        int T;
        scanf("%d", &T);
        while (T--) {
            scanf("%d %d", &E, &F);
            W = F - E;
            dp[0] = 0;
            for (int i = 1; i <= W; ++i) {
                dp[i] = INF;
            }
            scanf("%d", &N);
            for (int i = 1; i <= N; ++i) {
                scanf("%d %d", &e[i].p, &e[i].w);
            }
            DP();
            if (dp[W] == INF) {
                puts("This is impossible.");
            }
            else {
                printf("The minimum amount of money in the piggy-bank is %d.\n", dp[W]);
            }
        }
        return 0;
    }
  • 相关阅读:
    python opencv PyQt5
    各大web服务器https的证书文件
    mysql 常用字符串操作
    python 修改字符串中的某一位字符
    python mysql
    小程序
    m4a 转MP3
    安装python 3.7
    树莓派版本信息
    bash 重启后台程序脚本
  • 原文地址:https://www.cnblogs.com/Lyush/p/2481232.html
Copyright © 2011-2022 走看看