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

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

題目鏈接:http://poj.org/problem?id=1873

題目大意:有n棵樹,給出樹的座標,價值和做成的柵欄的長度 x,y,v,l,現在要砍掉一些樹做成柵欄把剩下的樹都圍起來,問花費的最小价值是多少,如果價值相同,選擇砍樹少的方案,輸出 要砍的樹 和做完柵欄後多餘的長度

思路:因爲n最大爲15,可以枚舉所有情況,再求凸包,記錄其中滿足情況的最小花費和最小砍樹數

代碼:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
const int N=1<<15+5;
struct node
{
    int x,y,v,l,id;
}e[20],a[20],s[20];
int vis[20];
bool cmp(node a,node b)
{
    if(a.y==b.y) return a.x<b.x;
    return a.y<b.y;
}
int cross(node a,node b,node c)
{
    return (b.x-a.x)*(c.y-a.y)-(b.y-a.y)*(c.x-a.x);
}
double dis(node a,node b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool cmp1(node a,node b)
{
    int mm=cross(e[0],a,b);
    if(mm>0) return 1;
    else if(mm==0&&dis(e[0],a)-dis(e[0],b)<=0)
        return 1;
    return 0;
}
int n,tot;
void graham(int len)
{
    sort(e,e+len,cmp);
    sort(e+1,e+len,cmp1);
    tot=1;
    s[0]=e[0],s[1]=e[1];
    for(int i=2;i<len;i++)
    {
        while(tot&&cross(s[tot-1],s[tot],e[i])<=0)
            tot--;
        s[++tot]=e[i];
    }
}
double fun()
{
    int l=0;
    for(int i=0;i<n;i++)
        if(!vis[i]) e[l++]=a[i];
    if(l==1) return 0;
    else if(l==2) return dis(e[0],e[1])*2;
    graham(l);
    double ans=0;
    s[++tot]=s[0];
    for(int i=0;i<tot;i++)
        ans+=dis(s[i],s[i+1]);
    return ans;
}
int main()
{
    int T=1;
    while(~scanf("%d",&n)&&n)
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d%d",&a[i].x,&a[i].y,&a[i].v,&a[i].l);
            a[i].id=i;
        }
        int minn=1e9,k=0,cn;
        double res;
        for(int i=1;i<(1<<n)-1;i++)
        {
            memset(vis,0,sizeof(vis));
            int sum=0,ans=0,cnt=0;
            for(int j=0;j<n;j++)
                if(i&(1<<j)) vis[j]=1,ans+=a[j].v,sum+=a[j].l,cnt++;
            if(ans>minn) continue;
            double p=fun();
            if(p>1.0*sum) ans=-1;
            if(ans!=-1&&ans<minn)
            {
                minn=ans;
                k=i;
                cn=cnt;
                res=1.0*sum-p;
            }
            else if(ans!=-1&&ans==minn&&cn<cnt)
            {
                k=i;
                cn=cnt;
                res=1.0*sum-p;
            }
        }
        printf("Forest %d\nCut these trees:",T++);
        for(int i=0;i<n;i++)
            if(k&(1<<i))printf(" %d",i+1);
        printf("\nExtra wood: %.2f\n\n",res);
    }
    return 0;
}


 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章