zoukankan      html  css  js  c++  java
  • hdu 1114 Piggy-Bank 完全背包

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1114

    近乎裸的完全背包求最小

    dp[0] = 0;

    后面的用INF填充 【用fill的话记得前后都要 “+1”啊血泪...】

    如果dp[V]还是INF说明无解

    #include <cstdio>
    #include <cstdlib>
    #include <ctime>
    #include <iostream>
    #include <cmath>
    #include <cstring>
    #include <algorithm>
    #include <stack>
    #include <set>
    #include <queue>
    #include <vector>
    
    using namespace std;
    
    typedef long long ll;
    
    const int maxn = 510;
    const int maxm = 10100;
    const int INF = 2000000000;
    
    int w[maxn], c[maxn];
    int f[maxm];
    int n, V;
    
    void CompletePack(int cost, int weight)
    {
        for(int i = cost; i <= V; i++)
        {
            f[i] = min(f[i], f[(i-cost)] + weight);
        }
    }
    
    int main()
    {
        //freopen("in.txt", "r", stdin);
    
        int T;
        scanf("%d", &T);
        while(T--)
        {
            int E, F;
            scanf("%d%d", &E, &F);
            V = F - E;
    
            f[0] = 0;
            fill(f + 1, f + V + 1, INF);
    
            int n;
            scanf("%d", &n);
            for(int i = 0; i < n; i++)
                scanf("%d%d", &w[i], &c[i]);
    
            for(int i = 0; i < n; i++)
            {
                CompletePack(c[i], w[i]);
            }
    
            if(f[V] == INF)
                printf("This is impossible.
    ");
            else
                printf("The minimum amount of money in the piggy-bank is %d.
    ", f[V]);
        }
    
        return 0;
    }
  • 相关阅读:
    Redis学习笔记
    Sevrlet 工作原理解析-转
    Spring MVC基础学习
    JVMGC机制
    MyBatis基础学习笔记--摘录
    MyBatis基础学习笔记--自总结
    Spring基础复习
    java.lang.Class类
    浅谈linux静态库、动态库。
    关于字符编码的一点学习。
  • 原文地址:https://www.cnblogs.com/dishu/p/4296346.html
Copyright © 2011-2022 走看看