poj1873(枚举+凸包)

The Fortified Forest
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 7291 Accepted: 2031

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

Source

题意:平面上有n棵树,给出他们的坐标x,y,价值 v,可以做成篱笆的长度,要求你求出砍掉最小价值的树并且使得砍掉的树做成的篱笆可以把剩下的树围起来 。
当价值相同时,要求砍掉的树最少。
思路:因为这道题的n的范围很小,所以可以用二进制枚举所有的情况,找出最优解。
代码:
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#define zero(a) (fabs((double)(a))<(1e-8))
using namespace std;
const int eps=1e-8;
struct tr{
	int x,y,v,l;
}s1[20],s[20];
int st[20];
int mul(tr p,tr u,tr v){//求叉积 
	return (p.x-u.x)*(v.y-u.y)-(v.x-u.x)*(p.y-u.y);
}
double dis(tr a,tr b){//求长度 
	return sqrt((double)(a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool cmp(tr a,tr b){//极角排序 
	if(mul(a,s[0],b)>0)//以最小点s[0]为基准排序 
	return true;
	else if(zero(mul(a,s[0],b))&&dis(s[0],a)<dis(s[0],b))
	return true;
	return false;
}
int mv,ct,nt;
double l2;
int do1(int n,int l1,int mv1,int c){
	int i,j;
	int x,y;
	double l;//要用的木材长度 
	if(n==1){
		l=0;
	}
	/*else if(n==2){
		l=dis(s[0],s[1])*2;
	}*/ 
	else{ 
		l=0;
		for(i=1;i<n;i++){
		if(s[0].y>s[i].y||(s[0].y==s[i].y&&s[0].x>s[i].x)){//将s[0]化为最小点,以便极角排序 
			swap(s[0],s[i]);
			}
		}
		sort(s+1,s+n,cmp);//排序 
		int top,cnt;
		st[0]=0;
		st[1]=1;
		tr a,b;
		top=2;
		cnt=2;
		while(cnt<n-1){//当 
			st[top]=cnt;//先放入当前点 
			top++;
			cnt++;
			a=s[st[top-1]];
			b=s[st[top-2]];
			while(mul(s[cnt],a,b)<eps){//以当前点的下一个点为标杆点来除去不合凸包条件的点(Graham扫描法)(如果标杆点在当前栈顶的两个点组成的直线的右边,则说明栈顶的点不是凸包上的点) 
				top--;//去掉不合条件的点 
				a=b;//重新判断当前栈顶的点 
				b=s[st[top-2]];
			}
		}
		st[top++]=n-1;//压入最后一次的标杆点,它一定是凸包上的点,因为它在最左边 
   		for( i=0;i<top-1;i++){
    		l+=dis(s[st[i]],s[st[i+1]]);
		}
        l+=dis(s[0],s[n-1]);//把这句代码写成了l+=dis(s[st[0]],s[st[n-1]]),卡了三个小时,QAQ,引以为戒啊 
	}
	//printf("%d %d\n",l1,l);
	if((l1-l)>=0){// 砍掉的树可以把剩下的树围起来 
		if(mv1>mv){//剩下树的价值要最大 
			l2=l1-l;
			nt=n;
			mv=mv1;
			ct=c;
		}
		else if(mv1==mv&&nt<=n){//价值相同时砍掉的树要最少 
			l2=l1-l;
			nt=n;
			mv=mv1;
			ct=c;
		}
	}
	return 0;
}
int main(){
	int n,i,j,k=0;
	int a,b,cnt,c;
	int mv1;
	while(scanf("%d",&n)!=EOF&&n){
		k++;
		nt=0;
		for(i=0;i<n;i++){
			scanf("%d%d%d%d",&s1[i].x,&s1[i].y,&s1[i].v,&s1[i].l);
		}
			mv=0;
			ct=0;
			for(j=1;j<(1<<n)-1;j++){//二进制枚举 
				//printf("%d\n",j);
				a=j,b=0;
				cnt=0;
				mv1=0;
				int l1=0;
				for(i=0;i<n;i++){
                	if(((1<<i)&j)){//按位与 
                		s[cnt++]=s1[i];//放入不砍的树 
                		mv1+=s1[i].v;//剩余的价值 
					}
                	else{
               	        l1+=s1[i].l;//可以做篱笆的长度 
              	  	}
           		 }
				if(mv>mv1)//剩下的价值小于最优的价值 
				continue;
				do1(cnt,l1,mv1,j);
			}
			printf("Forest %d\n",k);
			printf("Cut these trees:");
			for(i=0;i<n;i++)
            if(!((1<<i)&ct))  printf(" %d",i+1);
			printf("\n");
			printf("Extra wood: %.2f\n\n",l2);
	}
	return 0;
}

  

转载于:https://www.cnblogs.com/cglongge/p/9408398.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值