码迷,mamicode.com
首页 > 其他好文 > 详细

hdu 1114 Piggy-Bank 完全背包

时间:2015-02-20 00:04:46      阅读:222      评论:0      收藏:0      [点我收藏+]

标签:

题目链接: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;
}

 

hdu 1114 Piggy-Bank 完全背包

标签:

原文地址:http://www.cnblogs.com/dishu/p/4296346.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!