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

 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;
}
时间: 2024-10-01 10:41:39

[usaco]4.1.3 Fence Rails 多维背包问题,dfsid的相关文章

算法:poj 1976 A Mini Locomotive (dp 二维01背包)

题目大意: 某个车站有N个火车车厢,编号为1-N,每个车厢上有xi个人. 这个车站还有 三个火车头,他们能拉最多m个车厢(m<=N/3),而且这m个车厢的编号要连续的.问这三个火车头最多 能拉多少个人. 思路: 因为m<=N/3, 所以按照贪心的思想,为了拉更多的人,每个火车 头一定是要拉m个连续的车厢. 然后,为了求某段连续的车厢共有多少人,可以前缀和预处理, 某 一段和=sum[ i ] - sum[ i-m]. f[i][j] 代表前i个车厢,用j个火车头拉,最多能拉多少人. 对于第i个

0-1背包-poj-1948-Triangular Pastures

poj-1948-Triangular Pastures 携程大赛2014.4.11 预赛第二场第二题 Description Like everyone, cows enjoy variety. Their current fancy is new shapes for pastures. The old rectangular shapes are out of favor; new geometries are the favorite.  I. M. Hei, the lead cow 

什么是网站优化的“蚁群算法”以及其特点

中介交易 http://www.aliyun.com/zixun/aggregation/6858.html">SEO诊断 淘宝客 云主机 技术大厅 想了解蚁群算法与SEO的关系,我们还是先来看看"蚁群算法"的由来:蚂蚁是地球上最常见.数量最多的昆虫种类之一,常常成群结队地出现在人类的日常生活环境中.这些昆虫的群体生物智能特征,引起了一些学者的注意.意大利学者M.Dorigo,V.Maniezzo等人在观察蚂蚁的觅食习性时发现,蚂蚁总能找到巢穴与食物源之间的最短路径.

关于背包问题的一些理解和应用_C 语言

1.背包问题介绍 背包问题不单单是一个简单的算法问题,它本质上代表了一大类问题,这类问题实际上是01线性规划问题,其约束条件和目标函数如下: 自从dd_engi在2007年推出<背包问题九讲>之后,背包问题的主要精髓基本已道尽.本文没有尝试对背包问题的本质进行扩展或深入挖掘,而只是从有限的理解(这里指对<背包问题九讲>的理解)出发,帮助读者更快地学习<背包问题九讲>中的提到的各种背包问题的主要算法思想,并通过实例解释了相应的算法,同时给出了几个背包问题的经典应用. 2.

[usaco]3.4.4 变形的动态规划问题Electric Fence

Electric Fence Don Piele In this problem, `lattice points' in the plane are points with integer coordinates. In order to contain his cows, Farmer John constructs a triangular electric fence by stringing a "hot" wire from the origin (0,0) to a la

通过Ruby on Rails和docker构建微服务架构之入门教程

说到时下的架构,免不了会涉及到微服务.而谈到微服务架构,又跟容器和Docker技术脱不了关系.虽然容器和Docker并不完全是一回事,但两者是密不可分的,而且二者之间也有共同之处:在大型复杂应用的构建和运营方面,二者都可以大大提高企业的效率.   微服务可不像一般的应用,可以通过apt-get工具进行安装,大家可能会问了:我们该如何才能像安装应用一样实现这种服务呢?在很大的程度上,这个问题的答案是否定的,我们无法轻松实现这种服务.更准确的说,至少目前我们还无法实现.在一个系统中,最难修改的就是架

POJ 3253 Fence Repair:贪心及优先队列

Fence Repair http://poj.org/problem?id=3253 Time Limit: 2000MS Memory Limit: 65536K Description Farmer John wants to repair a small length of the fence around the pasture. He measures the fence and finds that he needs N (1 ≤ N ≤ 20,000) planks of woo

10个基于 Ruby on Rails 构建的顶级站点

本文系国内 ITOM 行业领军企业 OneAPM 工程师翻译整理自 Raviraj Hegde 的文章 Top Sites Built with Ruby on Rails. 就其本身而言,Ruby in Rails 已经从一个简单的框架演化为强大的工具.最近几年,其名气大涨,这也合情合理:除拥有稳定的性能之外,在开发功能复杂的应用时使用 gem 能够节约大量时间. 目前,市场对Ruby on Rails 开发人员的需求庞大.各种各样的平台如雨后春笋般涌现,对优秀开发者的需求也从未如此之高.无论

运维自动化之使用Cobbler自动化部署Linux操作系统

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 .作者信息和本声明.否则将追究法律责任.http://dgd2010.blog.51cto.com/1539422/1672569 1.Cobbler是什么? Cobbler是一个Linux安装服务器,能够快速设置好网络安装环境.它实现了许多与Linux相关的任务的自动化和组合,因此你在部署新的(操作)系统或更改已经存在的操作系统时不需要在繁多的命令和应用程序之间来回切换.Cobbler能帮助(用户.管理者)置备和管理DNS.DHC