L - The Fortified Forest POJ - 1873 (凸包+枚举)

L - The Fortified Forest

 POJ - 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

 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
Cut these trees: 2 4 5 
Extra wood: 3.16

Forest 2
Cut these trees: 2 
Extra wood: 15.00

 题意:给定一些树,要求砍掉一些树来作为剩下的树的围栏,求砍掉这些树的费用最小,最后输出砍掉的树除了作为围栏

的部分,还剩下多少长度的木材(wood);

思路:用位运算枚举(1<<n)-1种情况(即某颗树是否砍掉),取砍树费用小的或费用相同但砍树数量少的情况,然后求剩下的树形成的凸包的周长,用砍掉的树可以形成的围栏长度-该周长即结果。

当然,如果枚举的时候,发现砍树的费用已经采购前面的树的费用,或费用相同但砍树数量相对多的情况,就应该过掉

#include<cstdio>
#include<stack>
#include<set>
#include<vector>
#include<queue>
#include<algorithm>
#include<cstring>
#include<string>
#include<map>
#include<iostream>
#include<cmath>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;
const int N=110;
const int nmax = 20;
const double esp = 1e-8;
const double PI=3.1415926;
int n,cnum,tnum;
int tubao[nmax],temp[nmax];
struct point
{
    double x,y,v,l;
    int id;
    point() {}
    point(double _x,double _y):x(_x),y(_y)
    {
    }
    point operator -(const point &b)const
    {
        return point(x-b.x,y-b.y);
    }
} tree[nmax],cut[nmax],tr[nmax],p0;
double cross(point p,point q)
{
    return p.x*q.y-p.y*q.x;
}
double dis(point p,point q)
{
    return sqrt((1.0*p.x-q.x)*(p.x-q.x)+(p.y-q.y)*(p.y-q.y));
}
bool cmp(point p,point q)
{
    double area=cross(p-p0,q-p0);
    if(fabs(area)<esp)
        return dis(p,p0)<dis(q,p0);
    else if(area>esp)
        return true;
    return false;
}
double len_fence()   //求凸包的周长
{
    int tid=0;
    for(int i=0; i<tnum; i++)
    {
        if(tr[i].y<p0.y)
        {
            tid=i;
            p0.y=tr[i].y;
        }
    }
    point tem=tr[tid];
    tr[tid]=tr[0];
    tr[0]=tem;
    p0=tr[0];
    sort(tr+1,tr+tnum,cmp);//求凸包前的极角排序
    int top=1;
    tubao[0]=0;
    tubao[1]=1;
    for(int i=2; i<tnum; i++)
    {
        while(top>0&&cross(tr[tubao[top]]-tr[tubao[top-1]],tr[i]-tr[tubao[top-1]])<=0)
            top--;
        tubao[++top]=i;
    }
    double ans=0.0;
    tubao[++top]=tubao[0];
    for(int i=0; i<top; i++)
        ans+=dis(tr[tubao[i]],tr[tubao[i+1]]);
    return ans;
}
void init()
{
    cnum=tnum=0;
    p0.id=p0.x=p0.v=p0.l=0;
    p0.y=inf;
}
void solve()
{
    double sum=0.0;
    int tcnum=inf;
    double tcutlen=0.0;
    double lost=inf;
    double cutlost,cutlen;
    for(int i=1; i<(1<<n); i++)
    {
        init();
        cutlost=cutlen=0.0;
        for(int j=0; j<n; j++)
        {
            if(i&(1<<j))
            {
                cut[cnum++]=tree[j];
                cutlost+=tree[j].v;
                cutlen+=tree[j].l;
            }
            else
                tr[tnum++]=tree[j];
        }
        if(cutlost>lost||(cutlost==lost&&cnum>=tcnum))  //砍树费用小的或费用相同但砍树数量少的才行
            continue;
        double ans;
        if(tnum<=1)
            ans=0.0;
        else if(tnum==2)
            ans=2*dis(tr[0],tr[1]);
        else
            ans=len_fence();
        if(cutlen-ans>-esp)//  砍掉的数要围得了剩下的树
        {
            lost=cutlost;
            tcutlen=cutlen;
            sum=ans;
            tcnum=cnum;
            for(int j=0; j<cnum; j++)
                temp[j]=cut[j].id;
        }
    }
    printf("Cut these trees:");
    for(int i=0; i<tcnum; i++)
        printf(" %d",temp[i]+1);
    printf("\n");
    printf("Extra wood: %.2lf\n",tcutlen-sum);
}
int main()
{
    int c=0;
    while(~scanf("%d",&n)&&n)
    {
        for(int i=0; i<n; i++)
        {
            scanf("%lf%lf%lf%lf",&tree[i].x,&tree[i].y,&tree[i].v,&tree[i].l);
            tree[i].id=i;
        }
        if(c!=0)
            printf("\n");
        printf("Forest %d\n",++c);
        solve();
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/clz16251102113/article/details/83217122