POJ 1873 The Fortified Forest [凸包 枚举]

The Fortified Forest
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 6400   Accepted: 1808

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

 

题意:每棵树坐标价值长度,砍掉一些树把剩下的围起来,最小价值最小数量问砍掉了那些树以及剩下的长度

二进制枚举然后把没砍的扔一起求凸包行了
 
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
typedef long long ll;
const int N=1005,INF=1e9;
const double eps=1e-8;
const double pi=acos(-1);

inline int read(){
    char c=getchar();int x=0,f=1;
    while(c<'0'||c>'9'){if(c=='-')f=-1; c=getchar();}
    while(c>='0'&&c<='9'){x=x*10+c-'0'; c=getchar();}
    return x*f;
}

inline int sgn(double x){
    if(abs(x)<eps) return 0;
    else return x<0?-1:1;
}

struct Vector{
    double x,y;
    Vector(double a=0,double b=0):x(a),y(b){}
    bool operator <(const Vector &a)const{
        //return x<a.x||(x==a.x&&y<a.y);
        return sgn(x-a.x)<0||(sgn(x-a.x)==0&&sgn(y-a.y)<0);
    }
};
typedef Vector Point;
Vector operator +(Vector a,Vector b){return Vector(a.x+b.x,a.y+b.y);}
Vector operator -(Vector a,Vector b){return Vector(a.x-b.x,a.y-b.y);}
Vector operator *(Vector a,double b){return Vector(a.x*b,a.y*b);}
Vector operator /(Vector a,double b){return Vector(a.x/b,a.y/b);}
bool operator ==(Vector a,Vector b){return sgn(a.x-b.x)==0&&sgn(a.y-b.y)==0;}

double Dot(Vector a,Vector b){return a.x*b.x+a.y*b.y;}
double Cross(Vector a,Vector b){return a.x*b.y-a.y*b.x;}
double DisPP(Point a,Point b){
    Point t=b-a;
    return sqrt(t.x*t.x+t.y*t.y);
}

int cas=0;
int n,x,y,v[N],l[N];
double ans;
Point p[N],ch[N],rp[N];
int rn;

double ConvexHull(Point p[],int n,Point ch[]){
    sort(p+1,p+1+n);
    int m=0,cnt=0;
    for(int i=1;i<=n;i++) {
        while(m>1&&sgn(Cross(ch[m]-ch[m-1],p[i]-ch[m-1]))<=0) m--;
        ch[++m]=p[i];
    }
    int k=m;
    for(int i=n-1;i>=1;i--) {
        while(m>k&&sgn(Cross(ch[m]-ch[m-1],p[i]-ch[m-1]))<=0) m--;
        ch[++m]=p[i];
    }
    if(n>1) m--;
    double ans=0;
    for(int i=1;i<=m;i++) ans+=DisPP(ch[i],ch[i%m+1]);
    return ans;
}

void solve(){
    int ansV=INF,ansS=0,ansCnt=INF,S=(1<<n);
    double extra;
    for(int i=1;i<S;i++){
        double len=0;
        int val=0,cnt;
        rn=0;
        for(int j=0;j<n;j++){
            if(i&(1<<j)){
                j++;
                len+=l[j],val+=v[j],cnt++;
                j--;
            }else rp[++rn]=p[j+1];
        }
        if(val>ansV||(val==ansV&&cnt>ansCnt)) continue;
        double peri=ConvexHull(rp,rn,ch);
        if(sgn(peri-len)>0) continue;
        if(val<ansV||(val==ansV&&cnt<ansCnt)){
            ansV=val;
            ansS=i;
            ansCnt=cnt;
            extra=len-peri;
        }
    }
    printf("Forest %d\nCut these trees: ",++cas);
    for(int j=0;j<=16;j++) if(ansS&(1<<j)) printf("%d ",j+1);
    printf("\nExtra wood: ");
    printf("%.2f\n\n",extra);
}

int main(int argc, const char * argv[]) {
    while(scanf("%d",&n)!=EOF&&n){
        for(int i=1;i<=n;i++) p[i].x=read(),p[i].y=read(),v[i]=read(),l[i]=read();
        solve();
    }
    return 0;
}

 

 
posted @ 2017-01-28 19:51  Candy?  阅读(364)  评论(0编辑  收藏  举报