poj1873 The Fortified Forest(位运算枚举+凸包)

The Fortified Forest
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 5511 Accepted: 1567

Description

Once upon a time, in a faraway land, there lived a king. This king owned a small collection of rare and valuable trees, which had been gathered by his ancestors on their travels. To protect his trees from thieves, the king ordered that a high fence be built around them. His wizard was put in charge of the operation. 
Alas, the wizard quickly noticed that the only suitable material available to build the fence was the wood from the trees themselves. In other words, it was necessary to cut down some trees in order to build a fence around the remaining trees. Of course, to prevent his head from being chopped off, the wizard wanted to minimize the value of the trees that had to be cut. The wizard went to his tower and stayed there until he had found the best possible solution to the problem. The fence was then built and everyone lived happily ever after. 

You are to write a program that solves the problem the wizard faced. 

Input

The input contains several test cases, each of which describes a hypothetical forest. Each test case begins with a line containing a single integer n, 2 <= n <= 15, the number of trees in the forest. The trees are identified by consecutive integers 1 to n. Each of the subsequent n lines contains 4 integers xi, yi, vi, li that describe a single tree. (xi, yi) is the position of the tree in the plane, vi is its value, and li is the length of fence that can be built using the wood of the tree. vi and li are between 0 and 10,000. 
The input ends with an empty test case (n = 0). 

Output

For each test case, compute a subset of the trees such that, using the wood from that subset, the remaining trees can be enclosed in a single fence. Find the subset with minimum value. If more than one such minimum-value subset exists, choose one with the smallest number of trees. For simplicity, regard the trees as having zero diameter. 
Display, as shown below, the test case numbers (1, 2, ...), the identity of each tree to be cut, and the length of the excess fencing (accurate to two fractional digits). 

Display a blank line between test cases. 

Sample Input

6
 0  0  8  3
 1  4  3  2
 2  1  7  1
 4  1  2  3
 3  5  4  6
 2  3  9  8
3
 3  0 10  2
 5  5 20 25
 7 -3 30 32
0

Sample Output

Forest 1
Cut these trees: 2 4 5 
Extra wood: 3.16

Forest 2
Cut these trees: 2 
Extra wood: 15.00

这个题目好气啊,据说是某WF上的大水题。卧槽。想法很快想出来了,代码却断断续续敲了一个小时,最后居然还debug了3个小时。我的天,想想。5个小时应该就这样没了。水平还是虐渣啊。。。

题目就是给你n个树,每个树都有坐标,然后价值,然后可以做成木头的长度。问取最小的价值的树,把剩下的树围起来。价值相同就取最少棵树的树。输入最多15课树,我是用位运算枚举娶那几颗树作为木头来围剩下的树,把剩下的树求个凸包周长,跟木头长度比较。万万没想到,凸包那里出现bug。还有剩下一棵树两棵树的情况特殊处理。好纠结啊。差点就放弃了。。。还好没有放弃自己。作任何事都不能放弃,让我想到上次考的财务管理,完全不会做,本来不打算去,后来想到不能轻言放弃,然后冒着大雨去了,卷子发下来,傻了,完全看不懂,好困,躺下睡了会,朦胧中还是感觉不能放弃自己啊,提笔随便乱写,乱画,终究还是把试卷用黑笔填满了。尽人事,听天命了。没想到最后今天查成绩居然过了。真是领悟了不能轻言放弃的真理啊。一定要坚持下去。

#include<limits>
#include<queue>
#include<vector>
#include<list>
#include<map>
#include<set>
#include<deque>
#include<stack>
#include<bitset>
#include<algorithm>
#include<functional>
#include<numeric>
#include<utility>
#include<sstream>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<ctime>
#define LL __int64
#define eps 1e-8
#define pi acos(-1)
using namespace std;
struct point{
	double x,y,v,l;
	point (double x=0,double y=0,double v=0,double l=0):x(x),y(y),v(v),l(l){
	} 
};
point g[100],p[100],pp[100];
int dcmp(double x){
	if (fabs(x)<=eps) return 0;
	if (x>0) return 1;
	else return -1;
}
double across(point a,point b,point c){
	return (a.x-c.x)*(b.y-c.y)-(a.y-c.y)*(b.x-c.x);
}
double dis(point a,point b){
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool cmp(point a,point b){
	double k=across(a,b,g[0]);
	if (dcmp(k)<0) return false;
	if (dcmp(k)==0) return dis(a,p[0])<dis(b,p[0]);
	return true; 
}
int begin,n,m;
void gotu(){
	int k=0;
	for (int i=1;i<m;i++)
		if (g[k].y>g[i].y || (g[k].y==g[i].y && g[k].x>g[i].x))
			k=i;
	point gt=g[k];
	g[k]=g[0];
	g[0]=gt;
	sort(g+1,g+m,cmp);
	pp[0]=g[0];
	pp[1]=g[1];
	begin=1;
	int l=2;
	while (l<m){
		double d=across(pp[begin],g[l],pp[begin-1]);
		/*if (begin==1 || dcmp(d)>0)
			pp[++begin]=g[l++];
		else begin--;*/
		if (begin>1 && dcmp(d)<=0) begin--;
		else pp[++begin]=g[l++];
	}
}
int a[50];
int main(){
	int T=0;
	while (~scanf("%d",&n) && n){
		for (int i=0;i<n;i++)
			scanf("%lf%lf%lf%lf",&p[i].x,&p[i].y,&p[i].v,&p[i].l);
		int val=0x7fffffff;
		int vt=0;
		double ansl=0;
		int ans=0;
		for (int i=1;i<(1<<(n));i++){
			int tv=0;
			int t=0;
			double tl=0;
			m=0;
			memset(g,0,sizeof(g));
			memset(pp,0,sizeof(pp)); 
			for (int j=0;j<n;j++)
				if (i & (1<<j)){
					t++;
					tv+=p[j].v;
					tl+=p[j].l;
				}else g[m++]=p[j];
			if (tv>val || (tv==val && t>=vt)) continue;
			double s=0;
			if (m==1) s=0;else
			if (m==2) s=dis(g[0],g[1])*2;else{
			gotu();
			s=dis(pp[0],pp[begin]);
			for (int k=0;k<begin;k++)
				s+=dis(pp[k],pp[k+1]);
			}
			if (dcmp(tl-s)>=0){
				vt=t;
				val=tv;
				ansl=tl-s;
				int h=0;
				ans=i;
			}
		}
		if (T)cout<<endl;
		printf("Forest %d\nCut these trees:",++T);
		for (int i=0;i<n;i++)
			if (ans & (1<<i))
				printf(" %d",i+1);
		printf("\nExtra wood: %.2f\n",ansl);
	}
	return 0;
}



评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值