poj 1873 The Fortified Forest(凸包)

The Fortified Forest
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 4924 Accepted: 1410

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棵树,每棵树有一定价值和高度,需要砍掉一些树,把剩下的树围起来,问花费最少的价值要砍哪些树,把剩余数目围起来后剩余的木材长度是多少
题解:状态压缩枚举所有情况,然后用凸包判断是否可行后更新,需要注意的是凸包剩余1棵树和2棵树的情况

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define eps 1e-8
struct point{
    double x,y,value,len;
}p[18],res[18],temp,first;
int now_status;
double now_value,now_len;
double myabs(double x){ return x<0?-x:x; }
double dis(struct point p1,struct point p2)
{
    return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));
}
double mult(struct point p1,struct point p2,struct point p3)
{
    return (p2.x-p1.x)*(p3.y-p1.y)-(p2.y-p1.y)*(p3.x-p1.x);
}
int cmp(const void *a,const void *b)
{
    struct point c=*(struct point *)a;
    struct point d=*(struct point *)b;
    double temp=mult(first,c,d);
    if(temp<-eps) return 1;
    return -1;
}
double graham(int n)
{
    int top=1,i;
    double ans=0;

    for(i=1;i<n;i++)
    {
        if(res[0].y>res[i].y||myabs(res[0].y-res[i].y)<eps)
            temp=res[0],res[0]=res[i],res[i]=temp;
    }
    first=res[0];
    qsort(res+1,n-1,sizeof(res[0]),cmp);
    for(i=2;i<n;i++)
    {
        while(top>=1&&mult(res[top-1],res[top],res[i])<=0) top--;
        res[++top]=res[i];
    }
    for(res[top+1]=res[0],i=0;i<=top;i++) ans+=dis(res[i],res[i+1]);

    return ans;
}
void slove(int status,int n)
{
    int i,all=0;
    double value=0,len=0,temp;

    for(i=0;i<n;i++)
    {
        if((1<<i)&status)
        {
            value+=p[i].value;
            len+=p[i].len;
        }
        else res[all++]=p[i];
    }
    if(value-now_value<-eps)
    {
        if(all>1) temp=graham(all);
        else temp=0;
        if(temp<len+eps)
        {
            now_value=value;
            now_status=status;
            now_len=len-temp;
        }
    }
}
int main()
{
    int i,n,N,cas=1;

    //freopen("t.txt","r",stdin);
    while(scanf("%d",&n),n)
    {
        if(cas>1) printf("\n");
        for(i=0;i<n;i++)
            scanf("%lf%lf%lf%lf",&p[i].x,&p[i].y,&p[i].value,&p[i].len);

        now_value=0xffffff,now_len=0xffffff,N=1<<n;
        for(i=0;i<N;i++) slove(i,n);

        printf("Forest %d\n",cas++);
        printf("Cut these trees:");
        for(i=0;i<n;i++)
        {
            if(now_status&(1<<i)) printf(" %d",i+1);
        }
        printf("\nExtra wood: %.2lf\n",now_len);
    }

    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值