zoukankan      html  css  js  c++  java
  • HDU 1114 Piggy-Bank(判断是否恰好装满的背包)

      其实这道题在寒假自学训练赛的时候就做过,不过感觉现在的理解跟当时真的不一样,大一半年过去了,变了这么多,我们都在进步,一回头便走出了这么远...

    好了,题目很简单,一个背包,装不满做一个标记就行了

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <cmath>
    #include <map>
    #include <algorithm>
    using namespace std;
    long long dp[10010];
    int money[10010], weight[10010];
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            int w1,w2;
            scanf("%d%d",&w1,&w2);
            int all = w2 - w1;
            dp[0] = 0;
            for(int i = 1;i <= all;i++)
                dp[i] = 999999999;
            int kind;
            scanf("%d",&kind);
            for(int i = 0;i < kind;i++)
            {
                scanf("%d%d",&money[i],&weight[i]);
            }
            for(int i = 0;i < kind;i++)
            {
                for(int j = weight[i]; j <= all;j++)
                {
                    dp[j] = min(dp[j],dp[j-weight[i]] + money[i]);
                }
            }
            if(dp[all] == 999999999)
                printf("This is impossible.
    ");
            else printf("The minimum amount of money in the piggy-bank is %lld.
    ",dp[all]);
        }
        return 0;
    }
  • 相关阅读:
    SPOJ Distinct Substrings(后缀数组求不同子串个数,好题)
    POJ 1743 Musical Theme(后缀数组+二分答案)
    HDU 6191 Query on A Tree(可持久化Trie+DFS序)
    swust oj 1052
    swust oj 1051
    swust oj 1016
    swust oj 1014
    swust oj 1013
    swust oj 1012
    swust oj 1011
  • 原文地址:https://www.cnblogs.com/jifahu/p/5449254.html
Copyright © 2011-2022 走看看