[usaco]4.1.3 Fence Rails 多维背包问题,dfsid

简介: <p> Fence Rails<br> Burch, Kolstad, and Schrijvers <br> 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 t

 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

HINTS (use them carefully!)
Fence Rails: Hint 1
This is a high dimensionality multiple knapsack problem, so we just have to test the cases. Given that the search space has a high out-degree, we will use depth first search with iterative deepening in order to limit the depth of the tree. However, straight DFSID will be too slow, so some tree-pruning is necessary.

---------------------------------------------------------
多维背包问题
使用dfs遍历,
首先把rails排序,然后,从最大的rail开始遍历。
对于第k个rail,遍历所有的板子,然后遍历地k-1个rail。如果所有的rail都有解法,则输出最开始rail。
usaco称之为DFSID

/*
ID:yunleis2
PROG:fence8
LANG:C++
*/


#include<iostream>
#include<fstream>

using namespace std;
const int MAXB=50;
const int MAXR=1024;
int boards[MAXB];
int remains[MAXB];
int rails[MAXR];
int b;
int r;
int board_sum=0;
int result=0;
int waste_max=0;
int from[MAXR];
fstream fout("fence8.out",ios::out);
inline void swap(int * a,int * b){
	int t=*a;
	*a=*b;
	*b=t;
//	*a=*a+*b;
//	*b=*a-*b;
//	*a=*a-*b;
}
int partiton(int * a,int p,int r)
{
	int x=a[r];
	int i=p-1;
	for(int j=p;j<r;j++){
		if(a[j]<x){
			++i;
			swap(a+j,a+i);
		}
	}
	swap(a+i+1,a+r);
	return i+1;
}
void quicksort(int * a,int p,int r)
{
	if(p<r)
	{
		int q=partiton(a,p,r);
		quicksort(a,p,q-1);
		quicksort(a,q+1,r);
	}
}
void dfs(int k,int waste){
	if(k<0){
		fout<<result+1<<endl;
		exit(0);
	}
	int i=0;
	if(k!=result&&rails[k]==rails[k+1])
		i=from[k+1];
	for(;i<b;i++){
		if(remains[i]>=rails[k]){
			from[k]=i;
			remains[i]-=rails[k];
			if(remains[i]<rails[0])waste+=remains[i];
			if(waste>waste_max){
				remains[i]+=rails[k];
				waste-=remains[i];
				continue;
			}
			dfs(k-1,waste);
			remains[i]+=rails[k];
		}
	}
}
int main()
{

	fstream fin("fence8.in",ios::in );
	fin>>b;
	for(int i=0;i<b;i++)
	{
		fin>>boards[i];
		board_sum+=boards[i];
		remains[i]=boards[i];
	}
	fin>>r;
	for(int i=0;i<r;i++)
		fin>>rails[i];
	quicksort(boards,0,b-1);
	quicksort(rails,0,r-1);
	int rail_sum=0;
	for(int i=0;i<r;i++){
		rail_sum+=rails[i];
		if(rail_sum>board_sum)
		{
			rail_sum-=rails[i];
			r=i;
			break;
		}
	}

	for(int i=r-1;i>=0;--i){
		result=i;
		waste_max = board_sum - rail_sum;
		rail_sum -= rails[i];
		dfs(i,0);
	}
	fout<<0<<endl;
	return 0;
}


目录
相关文章
|
人工智能
P1208 [USACO1.3]混合牛奶 Mixing Milk(贪心思想加一点特别的循环)
P1208 [USACO1.3]混合牛奶 Mixing Milk(贪心思想加一点特别的循环)
81 0
|
机器学习/深度学习 Windows
The 2022 ICPC Asia Regionals Online Contest (I)-D题题解(DFS暴搜+剪枝+打表去重+二分)
时间复杂度:O(227 + nlog(n) + T * log(n)),227是DFS打表的时间,nlog(n)是vertor排序的时间,T*log(n)是询问次数和二分搜答案的时间。(如果算错了,评论或者私信指出谢谢)
177 0
The 2022 ICPC Asia Regionals Online Contest (I)-D题题解(DFS暴搜+剪枝+打表去重+二分)
|
存储 算法
数据结构第三周笔记——树(上)(慕课浙大版本--XiaoYu)(三)
数据结构第三周笔记——树(上)(慕课浙大版本--XiaoYu)(三)
114 0
|
存储 算法
数据结构第三周笔记——树(上)(慕课浙大版本--XiaoYu)(二)
数据结构第三周笔记——树(上)(慕课浙大版本--XiaoYu)(二)
162 0
|
机器学习/深度学习 存储 算法
数据结构第三周笔记——树(上)(慕课浙大版本--XiaoYu)(一)
数据结构第三周笔记——树(上)(慕课浙大版本--XiaoYu)(一)
136 0
|
存储 机器学习/深度学习 算法
数据结构第三周笔记——树(上1)(慕课浙大版本--XiaoYu)
树的内容非常多,将会分成多篇文章进行上传
237 0
|
存储 机器学习/深度学习 算法
|
存储 人工智能 算法
数据结构第二周笔记——线性结构(慕课浙大版本--XiaoYu)上
线性结构的开始,我们又要开始接触链表了
131 0