您的位置:首页 > 其它

poj 1873 The Fortified Forest(凸包)

2015-08-11 23:47 465 查看
The Fortified Forest

Time Limit: 1000MSMemory Limit: 30000K
Total Submissions: 5737Accepted: 1636
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棵树,现在要砍掉其中的一部分来做成篱笆将剩下的树包围起来,现在给出每棵树的坐标、价值和可以制造篱笆的长度,

求砍掉最少价值的树,将剩下的树包围起来,当两种方式的价值相同时,取砍掉树更少的方式。
题解:直接二进制暴力枚举砍掉的树,剩下的树求出凸包,再求周长,可行的话更新ans。

#include<cstring>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
#define PI 3.1415926
#define INF 0x3f3f3f3f

const double eps=1e-12;
#define _sign(x) ((x)>eps?1:((x)<-eps?2:0))

using namespace std;
const int MAXN = 30;

struct Point {
    double x,y;
    double l;
    int 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);
    }
//叉积
    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);
    }
};
Point L[MAXN],P[MAXN];
int Stack[MAXN],top;
int n;

double dist(Point a,Point b) {
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}

int sgn(double x) {
    if(fabs(x)<eps)return 0;
    if(x<0)return -1;
    return 1;
}

//相对于L[0]的极角排序
bool _cmp(Point p1,Point p2) {
    double tmp = (p1-L[0])^(p2-L[0]);
    if(sgn(tmp) > 0)return true;
    else if(sgn(tmp) == 0 && sgn(dist(p1,L[0]) - dist(p2,L[0])) <= 0)
        return true;
    else return false;
}

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

int main() {
    //freopen("test.in","r",stdin);
    int ca=1;
    while(cin>>n&&n) {
        for(int i=0; i<n; i++) {
            scanf("%lf%lf%d%lf",&P[i].x,&P[i].y,&P[i].v,&P[i].l);
        }
        int m=0;
        int ans=INF,id=0;
        int num=0;
        double ss=99999999.0;
        for(int i=0; i<(1<<n); i++) {
            double ls=0;
            int sum=0;
            m=0;
            for(int j=0; j<n; j++) {
                if(i&(1<<j)) {  ///选到的树
                    sum+=P[j].v;
                    ls+=P[j].l;
                } else
                    L[m++]=P[j];///剩下的树
            }
            if(sum>ans)continue;
            Graham(m);
            double s=0;
            for(int j=0; j<top; j++) { ///求凸包周长
                int u=Stack[j],v=Stack[(j+1)%top];
                s+=dist(L[u],L[v]);
            }
            if(m<=1)s=0;
            if(ls>=s) {
                if(ans>sum) {
                    ans=sum;
                    num=n-m;
                    id=i;
                    ss=ls-s;
                } else if(ans==sum) {
                    if(num>n-m) {
                        ss=ls-s;
                        id=i;
                        num=n-m;
                    }
                }
            }
        }
        if(ca!=1)cout<<endl;
        printf("Forest %d\n",ca++);
        printf("Cut these trees:");
        for(int i=0; i<n; i++) {
            if(id&(1<<i))printf(" %d",i+1);
        }
        printf("\nExtra wood: %.2f\n",ss);
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: