[poj1873]The Fortified Forest--计算几何,暴搜+凸包

题目描述

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棵树的坐标,价值和砍下这棵树来可以做成栏杆的长度,要求你花费最小的代价来砍下一些树,还要保证可以围住其他的树。

看到这道题目的数据范围就可以想到,这道题目要不是状压,要不是暴搜。

这道题目显然可以先暴搜出每棵树是砍还是不砍,然后对于那些不砍的树构建凸包,求出周长,再判断是否能围住,最后更新最小代价,维护答案即可。

这道题目的时间复杂度用暴搜实际上能过,不知道网上的那种加的优化有什么用。

这道题目的理论复杂度为 O(2nlogn) O ( 2 n l o g n ) 2nlogn1.92×106 2 n l o g n ≈ 1.92 × 10 6 ,显然能过。

这道题目就讲完了,下面上代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int n;
struct P{
    double x,y;
}p[20];
P operator - (P a,P b)
{
    P tmp;
    tmp.x=a.x-b.x;
    tmp.y=a.y-b.y;
    return tmp;
}
double operator * (P a,P b)
{
    return a.x*b.y-b.x*a.y;
}
double dist(P a,P b)
{
    P tmp=a-b;
    return sqrt(tmp.x*tmp.x+tmp.y*tmp.y);
}
bool cmp(P p1,P p2)
{
    double s=(p1-p[1])*(p2-p[1]);
    return s>0||(s==0&&dist(p1,p[1])>=dist(p2,p[1]));
}
bool judge(int p0,int p1,int p2)
{
    double s=(p[p1]-p[p0])*(p[p2]-p[p0]);
    return s>0||(s==0&&dist(p[p1],p[p0])>=dist(p[p2],p[p0])); 
}
struct X{
    P o;
    int v,l;
}a[20];
int b[20],tot;
int q[20],top;
double Graham()
{
    if(tot==1) return 0.0;
    if(tot==2){
        return dist(p[1],p[2])*(double)2.0;
    }
    int fir=1;
    for(int i=2;i<=tot;i++)
    {
        if(p[i].y<p[fir].y||(p[i].y==p[fir].y&&p[i].x<p[fir].x)){
            fir=i;
        }
    }
    swap(p[1],p[fir]);
    sort(p+2,p+tot+1,cmp);  
    p[tot+1]=p[1];
    top=0;
    q[++top]=1;q[++top]=2;
    for(int i=3;i<=tot+1;i++)
    {
        while(top>1&&judge(q[top-1],i,q[top])==true)  top--;
        q[++top]=i;
    }
    double res=0.0;
    for(int i=1;i<top;i++)  res+=dist(p[q[i]],p[q[i+1]]);
    return res;
}
int ans=99999999;
double ret;
int c[20];
void dfs(int x)
{
    if(x==n+1)
    {
        tot=0;
        int sum1=0,sum2=0;
        for(int i=1;i<=n;i++){
            if(b[i]==1){
                sum1+=a[i].l;
                sum2+=a[i].v;
            }
            else{
                p[++tot].x=a[i].o.x;
                p[tot].y=a[i].o.y;
            }
        }
        if(tot==0)  return;
        double L=Graham();
        if((double)sum1>=L){
            if(sum2<ans)
            {
                ans=min(ans,sum2);
                ret=(double)sum1-L;
                for(int i=1;i<=n;i++)  c[i]=b[i];
            }
        }
        return;
    }
    b[x]=0;dfs(x+1);
    b[x]=1;dfs(x+1);
}
int main()
{
    int o=0;
    while(~scanf("%d",&n)&&n)
    {
        ans=99999999;
        ret=0;
        memset(b,0,sizeof(b));
        memset(c,0,sizeof(c));
        memset(q,0,sizeof(q));
        top=0;
        for(int i=1;i<=n;i++)   scanf("%lf%lf%d%d",&a[i].o.x,&a[i].o.y,&a[i].v,&a[i].l);
        dfs(1);
        o++;
        printf("Forest %d\n",o);
        printf("Cut these trees: ");
        for(int i=1;i<=n;i++)  if(c[i]==1)  printf("%d ",i); 
        puts("");
        printf("Extra wood: %.2lf\n",ret);
        puts("");
    }
    return 0;
}

谢谢大家!!

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值