POJ-1873 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棵树的笛卡尔坐标,他们的价值以及他们的长度,让你解决这个问题。

解题思路:

n只有15,枚举\large 2^{15}个答案,然后看看是否能够围起来,如果能够,我们取最小值。

#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>

using namespace std;
const int maxn = 20;
const double eps = 1e-8;
const double PI = acos(-1.0);
const int inf = 0x3f3f3f3f;

//对于浮点数的,><=0的判断。
int sgn(double x)
{
    if(fabs(x)<eps) return 0;
    if(x<0) return -1;
    else return 1;
}
struct Point{
    double x, y, l, v;
    Point(){}
    Point(double _x, double _y)
    {
        x = _x;y = _y;
    }
    Point operator -(const Point &b)const{
        return Point(x-b.x,y-b.y);
    }
    Point operator +(const Point &b)const{
        return Point(x+b.x,y+b.y);
    }
    //叉积
    double operator ^(const Point &b)const{
        return x*b.y-y*b.x;
    }
    //点积
    double operator *(const Point &b)const{
        return x*b.x+y*b.y;
    }
    //绕原点旋转角度B(弧度制)后,x,y的变化。
    void transXY(double B)
    {
        double tx = x, ty = y;
        x = tx*cos(B)-ty*sin(B);
        y = tx*sin(B)+ty*cos(B);
    }
};
double dist(Point a, Point b)
{
    //勾股定理,使用点积
    return sqrt((a-b)*(a-b));
}

Point p[maxn];
int n;

//做极角排序
bool _cmp(Point p1,Point p2)
{
    double tmp = (p1-p[1])^(p2-p[1]); //向量叉乘
    if(sgn(tmp)>0) return true;
    else if(sgn(tmp)==0&&sgn(dist(p1,p[1])-dist(p2,p[1]))<=0)
        return true;
    else return false;
}

int Stack[maxn],top;

void Graham()
{
    Point p0;
    int k = 1;
    p0 = p[1];
    //找最下方的一个点
    for(int i = 2; i <= n; i++)
    {
        if(p0.y>p[i].y||(p0.y==p[i].y&&p0.x>p[i].x))
        {
            p0 = p[i];
            k = i;
        }
    }
    swap(p[1],p[k]);
    sort(p+2,p+n+1,_cmp);
    if(n == 1)
    {
        top = 1;
        Stack[0] = 1;
        return ;
    }
    if(n == 2)
    {
        top = 2;
        Stack[0] = 1;
        Stack[1] = 2;
        return ;
    }
    Stack[0] = 1;
    Stack[1] = 2;
    top = 2;
    for(int i = 3; i <= n; i++)
    {
        while(top>1&&sgn((p[Stack[top-1]]-p[Stack[top-2]])^(p[i]-p[Stack[top-2]]))<=0) top--;
        Stack[top++] = i;
    }

}


Point pp[maxn];
int main()
{
    int m;
    int cnt = 1;
    while(scanf("%d", &m)&&m)
    {
        for(int i = 1; i <= m; i++)
            scanf("%lf%lf%lf%lf", &pp[i].x, &pp[i].y, &pp[i].v, &pp[i].l);
        int M = (1<<m);
        int ans= inf,g;
        double extra,L,V,C,maxx = 1e9;
        for(int i = 1; i < M; i++)
        {
            n = 0;
            L=C=V=0.0;
            for(int j = 0; j < m; j++)
            {
                if((i>>j)&1)
                {
                    p[++n] = pp[j+1];
                }
                else
                {
                    L+=pp[j+1].l;
                    V+=pp[j+1].v;
                }
            }
            Graham();
            for(int j = 1; j < top; j++)
            {
                C+=dist(p[Stack[j-1]],p[Stack[j]]);
            }
            C+=dist(p[Stack[0]],p[Stack[top-1]]);
            if(sgn(L-C)<0)
                continue;

            if(sgn(maxx-V)>0)
            {
                maxx = V;
                extra = L-C;
                ans = m-n;
                g = i;
            }
            else if(sgn(maxx-V)==0&&ans>m-n)
            {
                extra = L-C;
                ans = m-n;
                g = i;
            }
        }
        printf("Forest %d\n", cnt++);
        printf("Cut these trees: ");
        for(int i = 0; i < m; i++)
        {
            if(((g>>i)&1)==0)
            {
                printf("%d ", i+1);
            }
        }
        printf("\n");
        printf("Extra wood: %.2f\n\n",extra);
    }
    return 0;
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值