首页 > 代码库 > POJ 3628 Bookshelf 2 0/1背包和DFS两种解法

POJ 3628 Bookshelf 2 0/1背包和DFS两种解法

题目链接: POJ 3628 Bookshelf 2

Bookshelf 2
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7462 Accepted: 3436

Description

Farmer John recently bought another bookshelf for the cow library, but the shelf is getting filled up quite quickly, and now the only available space is at the top.

FJ has N cows (1 ≤ N ≤ 20) each with some height of Hi (1 ≤ Hi ≤ 1,000,000 - these are very tall cows). The bookshelf has a height of B (1 ≤ B ≤ S, where S is the sum of the heights of all cows).

To reach the top of the bookshelf, one or more of the cows can stand on top of each other in a stack, so that their total height is the sum of each of their individual heights. This total height must be no less than the height of the bookshelf in order for the cows to reach the top.

Since a taller stack of cows than necessary can be dangerous, your job is to find the set of cows that produces a stack of the smallest height possible such that the stack can reach the bookshelf. Your program should print the minimal ‘excess‘ height between the optimal stack of cows and the bookshelf.

Input

* Line 1: Two space-separated integers: N and B
* Lines 2..N+1: Line i+1 contains a single integer: Hi

Output

* Line 1: A single integer representing the (non-negative) difference between the total height of the optimal set of cows and the height of the shelf.

Sample Input

5 16
3
1
3
5
6

Sample Output

1

Source

USACO 2007 December Bronze


题意

书架高度为b,n头奶牛叠罗汉,要你找几头奶牛叠加是高度大于b,要求高出的大小尽可能小,求解高出的最小的可能性。


思路1:

n的值不大,可考虑dfs搜索加剪枝,思路清晰,但是剪枝很重要不然就2的20次方了。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int a[30], cn[30], n, b, minex;
void dfs(int x, int sum)
{
    if(sum + cn[n] - cn[x] < b) return; //已经不可能到达书架顶端了
    if(sum >= b)        
    {
        if(minex > sum-b) minex = sum-b;
        return;
    }
    dfs(x+1, sum);
    dfs(x+1, sum+a[x+1]);
}
int main()
{
    while(~scanf("%d%d", &n, &b))
    {
        for(int i = 1; i <= n; i++)
            scanf("%d", &a[i]);
        cn[0] = 0;
        for(int i = 1; i <= n; i++)
            cn[i] += cn[i-1]+a[i];
        minex = 10000000;
        dfs(0, 0);
        printf("%d\n", minex);
    }
    return 0;
}
思路2:

0/1背包,把奶牛的总高度加起来减去b就是背包容量,尽可能装满这个背包,最后结果是背包容量减去最大值。

注意奶牛高度加起来很大,要开很大的数组,用memset会超内存,所以还是for循环吧。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

int dp[20000010];
int main()
{
    int n, b, h[22];
    while(~scanf("%d%d", &n, &b))
    {
        int sum = 0;
        for(int i = 1; i <= n; i++)
        {
            scanf("%d", &h[i]);
            sum += h[i];
        }
        int v = sum-b;
        //memset(dp, sizeof(dp));   memset会超内存啊
        for(int i = 0; i <= v; i++)
            dp[i] = 0;
        for(int i = 1; i <= n; i++)
            for(int j = v; j >= h[i]; j--)
                dp[j] = max(dp[j], dp[j-h[i]]+h[i]);
        printf("%d\n", v-dp[v]);
    }
    return 0;
}