hdu 2546 饭卡

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

简单的01背包

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<map>
#include<algorithm>

using namespace std;

const int maxn = 1005;
int n, m, W;
int dp[maxn], w[maxn], v[maxn];

int main()
{
    while(~scanf("%d", &n), n)
    {
        memset(dp, 0, sizeof(dp));
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &w[i]);
        }
        cin >> W;
        if(W >= 5)
        {
            W -= 5;
            sort(w + 1, w + n + 1);
            for(int i = 1; i <= n; i++)
            {
                v[i] = w[i];
            }
            for(int i = 1; i < n; i++)
            {
                for(int j = W; j >= w[i]; j--)
                {
                    dp[j] = max(dp[j], dp[j - w[i]] + v[i]);
                }
            }
            cout << 5 +  W - dp[W] - w[n] << endl;
        }
        else 
        {
            cout << W << endl; 
        }
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/k_ona/article/details/79490955