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.\n");
        else
            printf("The minimum amount of money in the piggy-bank is %d.\n", f[V]);
    }

    return 0;
}

 

posted @ 2015-02-19 23:03  地鼠地鼠  阅读(127)  评论(0编辑  收藏  举报