Bookshelf 2 poj3628(01背包/DFS)

http://poj.org/problem?id=3628

题意:现有一个书架,N头牛,农场主想把这些牛放在书架上,当然,书架是有固定的高度的。现在问你在这些牛之中,其中一些牛的高度加在一块比书架高的最小差值是多少?

分析:

两种方法:(1)01背包,算得所有牛的高度,然后dp一遍,再找比书架高的最小值,两数相减即可。

          (2)DFS,DFS(牛的坐标, 牛高度的总和),若在DFS过程中,牛高度的总和>=书架高度,则可以求我们所需要的值

01背包:

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#include <iostream>
#include<algorithm>
#include<queue>
#define maxn 1100
#define oo 0x3f3f3f3f
using namespace std;
int dp[maxn*maxn], w[maxn];

int main()
{
    int n, m;

    while(scanf("%d %d", &n, &m)!=EOF)
    {
        int sum = 0;

        for(int i=1; i<=n; i++)
        {
               scanf("%d", &w[i]);
               sum += w[i];
        }

        memset(dp, 0, sizeof(dp));

        for(int i=1; i<=n; i++)
        {
            for(int j=sum; j>=w[i]; j--)
            {
                dp[j]=max(dp[j], dp[j-w[i]]+w[i]);
            }
        }
        int mins = oo;

       for(int i=1; i<=sum; i++)
       {
           if(dp[i]>=m)
           {
               mins=min(mins, dp[i]-m);
           }
       }

       printf("%d\n", mins);
    }
    return 0;
}

DFS:

#include<stdio.h>
#include<math.h>
#include<string.h>
#include<ctype.h>
#include<stdlib.h>
#include <iostream>
#include<algorithm>
#include<queue>
#define maxn 1100
#define oo 0x3f3f3f3f
using namespace std;
int a[maxn], v[maxn];
int n, m, mins;

void DFS(int x, int sum)
{
    if(sum >= m)
    {
        mins=min(mins, sum-m);
        return ;
    }

    if(x>n || mins==0) return ;

    for(int i=x; i<=n; i++)
    {
        if(!v[i])
        {
            v[i] = 1;
            DFS(i+1, sum+a[i]);
            v[i] = 0;
        }
    }

}

int main()
{
    while(scanf("%d %d", &n, &m)!=EOF)
    {
        for(int i=1; i<=n; i++)
            scanf("%d", &a[i]);

        memset(v, 0, sizeof(v));
            mins = oo;

         DFS(1, 0);

         printf("%d\n", mins);
    }
    return 0;
}

 

时间: 2024-10-01 18:26:03

Bookshelf 2 poj3628(01背包/DFS)的相关文章

[Swust OJ 465]--吴奶奶买鱼(0-1背包+dfs)

题目链接:http://acm.swust.edu.cn/problem/465/ Time limit(ms): 1000 Memory limit(kb): 65535 Description 吴奶奶有个可爱的外孙女——琪琪,她很喜欢小动物,尤其喜欢养鱼.为了让小孙女养到漂亮的小鱼,吴奶奶一大早就到花鸟鱼虫市场买鱼.这个市场可真大,里面有各种各样的宠物,就连宠物鱼都有好几十种.这些鱼实在是太美了,买的人越来越多,可是因为货源有限,卖鱼的老板不得不规定:同一种鱼,每个人最多只能买一条,并且有些

POJ3628 Bookshelf 2(01背包+dfs)

Bookshelf 2 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8745   Accepted: 3974 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

hdu3448 01背包+dfs

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3448 Description 0/1 bag problem should sound familiar to everybody. Every earth man knows it well. Here is a mutant: given the capacity of a bag, that is to say, the number of goods the bag can ca

HDU_2079_(01背包)(dfs)

选课时间(题目已修改,注意读题) Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4478    Accepted Submission(s): 3480 Problem Description 又到了选课的时间了,xhd看着选课表发呆,为了想让下一学期好过点,他想知道学n个学分共有多少组合.你来帮帮他吧.(xhd认为一样学分的课没区别)

codeforces 842C Ilya And The Tree (01背包+dfs)

(点击此处查看原题) 题目分析 题意:在一个树中,有n个结点,记为 1~n ,其中根结点编号为1,每个结点都有一个值val[i],问从根结点到各个结点的路径中所有结点的值的gcd(最大公约数)最大是多少,其中,我们可以将路径中某一个结点的值变为0,也可以选择不变. 思路:注意到对于每个结点,我们可以选择这个结点,或者不选这个结点(将权值记为0),因而有点01背包的感觉,而我们求gcd的时候需要取所有情况中的最大值 那么我们从根结点开始,每经过一个结点,就从其父节点的所有情况转移得到当前结点的状态

POJ 3628 Bookshelf 2【01背包】

题意:给出n头牛的身高,以及一个书架的高度,问怎样选取牛,使得它们的高的和超过书架的高度最小. 将背包容量转化为所有牛的身高之和,就可以用01背包来做=== 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #define maxn 2000005 6 using namespace std; 7 int dp[maxn],h[50]; 8

poj 3628 Bookshelf 2 基本01背包

题目大意:FJ有n头奶牛,和一个高为h的架子,给出每头奶牛高度,求使奶牛叠加起来超过架子的最低高度是多少. 题目思路:求出奶牛叠加能达到的所有高度,并用dp[]保存,最后进行遍历,找出与h差最小的dp[]即所求答案. #include<cstdio> #include<stdio.h> #include<cstdlib> #include<cmath> #include<iostream> #include<algorithm> #i

POJ 3628 Bookshelf 2 (01背包)

Bookshelf 2 Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7496   Accepted: 3451 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

UVA 624 CD (01背包+打印路径 或 dfs+记录路径)

Description You have a long drive by car ahead. You have a tape recorder, but unfortunately your best music is on CDs. You need to have it on tapes so the problem to solve is: you have a tape N minutes long. How to choose tracks from CD to get most o