poj1873——求凸包周长+二进制枚举

题目链接:http://poj.org/problem?id=1873

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

题意理解:

给出一些树,每棵树有坐标,高度,以及价值,要求砍掉一些树,用那些木材,将其它树围起来,要求花最小的代价,代价相同,要求砍掉最少的树。

 

这个题的关键点在于数据范围(n<=15),很小的范围,我们可以直接二进制枚举所有情况,求出剩下没砍树形成的凸包的周长,然后判断木材是否够用,并同时求出题目要求的值。

(附:未砍树的数目等于1和等于2的情况要特判)

#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
const double eps=1e-6;
const int INF=0x3f3f3f3f;
struct point
{
    int x,y,v,l;
}list[15];
vector<point> p;
int n;
int cross(point p0,point p1,point p2) //计算叉积  p0p1 X p0p2 
{
    return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
}    
double dist(point p1,point p2)  //计算 p1p2的 距离 
{
    return sqrt((double)(p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
}    
bool cmp(point p1,point p2) //极角排序函数 , 角度相同则距离小的在前面 
{
    int tmp=cross(p[0],p1,p2);
    if(tmp>0) return true;
    else if(tmp==0&&dist(p[0],p1)<dist(p[0],p2)) return true;
    else return false;
}      
double Graham(int len){
    if(p.size()==1) return len;
    else if(p.size()==2) return len-dist(p[0],p[1])*2;
    for(int i=1;i<p.size();i++)
        if(p[i].y<p[0].y||(p[i].y==p[0].y&&p[i].x<p[0].x))
            swap(p[0],p[i]);
    sort(p.begin()+1,p.end(),cmp);
    vector<point>stack;
    stack.push_back(p[0]);
	stack.push_back(p[1]);
	stack.push_back(p[2]);
    for(int i=3;i<p.size();i++){
        while(stack.size()>=2&&cross(stack[stack.size()-2],stack[stack.size()-1],p[i])<eps)
            stack.pop_back();
        stack.push_back(p[i]);
    }
    stack.push_back(stack[0]);
    double ans=0;
    for(int i=0;i<stack.size()-1;i++)
        ans+=dist(stack[i],stack[i+1]);
    return len-ans;
}
int main()
{
    int kcase=0;
    while(~scanf("%d",&n)&&n)
    {
        for(int i=0;i<n;i++)
        	scanf("%d%d%d%d",&list[i].x,&list[i].y,&list[i].v,&list[i].l);
        int best_val=INF,best_num,best_state;
        double best_extra;        
		for(int i=1;i<(1<<n)-1;i++){
        	double temp_val=0,temp_len=0;
        	p.clear();
        	for(int j=0;j<n;j++){
        		if(!((1<<j)&i)) p.push_back(list[j]);
        		else {
        			temp_val+=list[j].v;
        			temp_len+=list[j].l;
				}
			}
			if(temp_val>best_val) continue;
			double extra=Graham(temp_len);
			if(extra>=0){
				if(temp_val<best_val){
					best_val=temp_val;
					best_num=n-p.size();
					best_state=i;
					best_extra=extra;
				}
				else if(temp_val==best_val&&n-p.size()<best_num){
					best_num=n-p.size();
					best_state=i;
					best_extra=extra;
				}
			}
		}
		printf("Forest %d\n",++kcase);
		printf("Cut these trees:");
		for(int i=0;i<n;i++)
			if((1<<i)&best_state) 
				printf(" %d",i+1);
		printf("\n");
		printf("Extra wood: %.2f\n\n",best_extra);
    }    
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值