Section 4.1 Fence Rails(DFS_ID迭代加深搜索+剪枝)

Fence Rails
Burch, Kolstad, and Schrijvers

Farmer John is trying to erect a fence around part of his field. He has decided on the shape of the fence and has even already installed the posts, but he's having a problem with the rails. The local lumber store has dropped off boards of varying lengths; Farmer John must create as many of the rails he needs from the supplied boards.

Of course, Farmer John can cut the boards, so a 9 foot board can be cut into a 5 foot rail and a 4 foot rail (or three 3 foot rails, etc.). Farmer John has an `ideal saw', so ignore the `kerf' (distance lost during sawing); presume that perfect cuts can be made.

The lengths required for the rails might or might not include duplicates (e.g., a three foot rail and also another three foot rail might both be required). There is no need to manufacture more rails (or more of any kind of rail) than called for the list of required rails.

PROGRAM NAME: fence8

INPUT FORMAT

Line 1: N (1 <= N <= 50), the number of boards
Line 2..N+1: N lines, each containing a single integer that represents the length of one supplied board
Line N+2: R (1 <= R <= 1023), the number of rails
Line N+3..N+R+1: R lines, each containing a single integer (1 <= ri <= 128) that represents the length of a single required fence rail

SAMPLE INPUT (file fence8.in)

4
30
40
50
25
10
15
16
17
18
19
20
21
25
24
30

OUTPUT FORMAT

A single integer on a line that is the total number of fence rails that can be cut from the supplied boards. Of course, it might not be possible to cut all the possible rails from the given boards.

SAMPLE OUTPUT (file fence8.out)

7

题目:http://ace.delos.com/usacoprob2?a=98sI0RlsOtq&S=fence8

题意:给你n块木板,和 r 个需求,也就是用n块木板切割成小木板,使得更多的小木板满足需求。。。

分析:这题初看会发现是一道多维的背包问题,不过维度太高,背包是不可能了。这种情况下,往往只能暴力了,而暴力的最佳选择就是深搜+剪枝。。。

而这个DFS_ID嘛以前没听说过,不过貌似用过,就是限制每次搜索的深度而已,只用DFS_ID还是不行,这时候就要加上各种剪枝了,这里就不透露剪枝了,自己想出来最好

其实我是看到是DFS后乱搞过的,有个优化是错误的,不过是小概率的错误,所以AC了,只有一组用来0.011s,呵呵

代码:

/*
ID: 15114582
PROG: fence8
LANG: C++
*/
#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
int a[55],b[1111],c[1111];
int n,r,sum;
int dfs(int t,int sum,int p)
{
    if(t<0)return 1;
    if(sum<c[t])return 0;
    int i=0,j;
    if(b[t]==b[t+1])i=p+1;
    for(j;i<n;++i)
        if(a[i]>=b[t])
        {
            a[i]-=b[t];
            j=dfs(t-1,sum-b[t]-(a[i]>=b[0]?0:a[i]),p);
            a[i]+=b[t];
            if(j>0)return 1;
            if(j<0)return 0;
            if(a[i]==b[t])return -1;
        }
    return -1;
}
int main()
{
    freopen("fence8.in","r",stdin);
    freopen("fence8.out","w",stdout);
    int i,l,ans,m;
    while(~scanf("%d",&n))
    {
        for(sum=i=0;i<n;++i)
            scanf("%d",&a[i]),sum+=a[i];
        scanf("%d",&r);
        for(i=0;i<r;++i)
            scanf("%d",&b[i]);
        sort(b,b+r);
        for(c[0]=b[0],i=1;i<r;++i)
            c[i]=c[i-1]+b[i];
        ans=l=0,--r;
        while(r&&sum<c[r])--r;
        while(l<=r)
        {
            m=(l+r)>>1;
            if(dfs(m,sum,-1)>0)ans=m+1,l=m+1;
            else r=m-1;
        }
        printf("%d\n",ans);
    }
    return 0;
}



你可能感兴趣的:(Integer,input,Rails,output,distance,Duplicates)