poj1873 凸包+枚举

<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">链接:</span><a target=_blank href="http://poj.org/problem?id=1873" target="_blank" style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">http://poj.org/problem?id=1873</a>

The Fortified Forest
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 5582   Accepted: 1591

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

World Finals 1999

 犯了一个傻逼错误,一开始以为刘汝佳的大白书有问题,然而并不是,简单的枚举+凸包,套模板啦!!

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
using namespace std;
int n,m,k,bbb=0;
double ansv,ansl,ansn;
struct point {
    double x,y,v,l;
    point(double x=0,double y=0):x(x),y(y) {}
} s[22],p[22],tr[22];
int vis[22],anw[22];
const double eps=1e-8;
typedef point vec;
vec operator - (point a,point b) {
    return vec(a.x-b.x,a.y-b.y);
}
int dcmp(double x) {
    if(fabs(x)<=eps) return 0;
    else return x<0?-1:1;
}
double cross(vec a,vec b) {
    return a.x*b.y-a.y*b.x;
}
double disn(point a,point b) {
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
bool cmp(point a,point b) {
    if(fabs(a.x-b.x)<=eps) return a.y<b.y;
    return a.x<b.x;
}
void convexhull() {
    sort(p,p+k,cmp);
    m=0;
    for(int i=0; i<k; i++) {
        while(m>1 && dcmp(cross(tr[m-1]-tr[m-2],p[i]-tr[m-2]))<=0)
            m--;
        tr[m++]=p[i];
    }
    int t=m;
    for(int i=k-2; i>=0; i--) {
        while(m>t && dcmp(cross(tr[m-1]-tr[m-2],p[i]-tr[m-2]))<=0)
            m--;
        tr[m++]=p[i];
    }
}
void dfs(int u,int num) {
    if(u<n) {
        vis[u]=0;
        dfs(u+1,num);
        vis[u]=1;
        dfs(u+1,num+1);
        return ;
    }
    if(num==0 || num==n) return ;
    double sumv=0,suml=0,need=0;
    for(int i=0; i<n; i++) {
        if(vis[i]) {
            suml+=s[i].l;
            sumv+=s[i].v;
        }
    }
    if(sumv>ansv) return ;
    k=0;
    for(int i=0; i<n; i++) {
        if(vis[i]) continue;
        p[k].x=s[i].x;
        p[k].y=s[i].y;
        k++;
    }
    if(k==1) need=0;
    else if(k==2) need=2*disn(p[0],p[1]);
    else {
        convexhull();
        need=0;
        for(int i=0; i<m-1; i++) {
            need+=disn(tr[i],tr[i+1]);
        }
    }
    if(need<=suml) {
        ansv=sumv;
        ansl=suml-need;
        ansn=num;
        int ll=0;
        for(int i=0; i<n; i++) {
            if(vis[i]) {
                anw[ll]=i+1;
                ll++;
            }
        }
    }
}
int main() {
    int cas=1;
    while(scanf("%d",&n)!=EOF) {
        if(n==0) break;
        for(int i=0; i<n; i++) {
            scanf("%lf%lf%lf%lf",&s[i].x,&s[i].y,&s[i].v,&s[i].l);
        }
        ansv=maxn;
        ansl=ansn=0;
        mst(vis,0);
        dfs(0,0);
        printf("Forest %d\nCut these trees: ", cas ++);
        for(int i = 0; i < ansn; i ++)
            printf("%d ", anw[i]);
        printf("\nExtra wood: %.2f\n\n", ansl);
    }
    return 0;
}



你可能感兴趣的:(poj1873 凸包+枚举)