The Fortified Forest POJ - 1873(凸包+状压)

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

题意:求砍掉一些树,满足价值最小,并且砍下的树的长度,能够包围剩下的树木(剩下的凸包)

思路:状压一下,然后用vector记录砍掉的树木,再跑Graham_scan 凸包算法就行

代码:

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <vector>
#include <set>
#include <string>
#include <math.h>
#include <stack>
#define Lson l,m,rt<<1
#define Rson m+1,r,rt<<1|1
typedef long long ll;
#define INF 0x3f3f3f3f
const int maxn=20;
const int MAXN=1e3+10;
const long long mod=100000000;
using namespace std;
const double eps=1e-8;
const double PI=acos(-1.0);
struct point
{
	int x,y,v,l;
}p[maxn],pt[maxn];
int n;
int sta[30],top;
 
int cross(point p0,point p1,point p2)//p0p1 * p0p2叉积  判断顺/逆时针 
{
	return (p1.x-p0.x)*(p2.y-p0.y)-(p1.y-p0.y)*(p2.x-p0.x);
}
double dis(point p1,point p2)
{
	return sqrt((double)(p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
}
bool cmp(point p1,point p2)//极角排序 p[0]为最下方&&最左边的点 
{
	int tmp=cross(p[0],p1,p2);
	if(tmp>0) return true;
	else if(tmp==0&&dis(p[0],p1)<dis(p[0],p2)) return true;//角度相同,距离小在前
	else return false;
}
void Graham_scan(int n)
{
    if(n==1)
    {
        sta[0]=0;
        top=0;
        return;
    }
    int index=0;
    for(int i=1;i<n;i++)
    {
        if(p[i].y<p[index].y||(p[i].y==p[index].y&&p[i].x<p[index].x))
        {
            index=i;
        }
    }
    swap(p[0],p[index]);
    sort(p+1,p+n,cmp);
    if(n==2)
    {
        sta[0]=0,sta[1]=1,top=1;
    }
    else if(n>2)
    {
        for(int i=0;i<=1;i++) sta[i]=i;
        top=1;
        for(int i=2;i<n;i++)
        {
            while(top>0&&cross(p[sta[top-1]],p[sta[top]],p[i])<=0)
            {
                top--;
            }
            sta[++top]=i;
        }
    }
}
int main(int argc, char const *argv[])
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    int Case=0;
    while(scanf("%d",&n)!=EOF&&n)
    {
        for(int i=0;i<n;i++)
        {
            scanf("%d%d%d%d",&pt[i].x,&pt[i].y,&pt[i].v,&pt[i].l);
        }
        int up=(1<<n)-1;
        int M=INF;
        double ans=INF;
        int tmp=INF;
        vector<int> res;
        for(int s=1;s<=up;s++)
        {
            int cnt=0,m=0,L=0;
            vector<int> use;
            for(int i=0;i<n;i++)
            {
                if(s&(1<<i))
                {
                    use.push_back(i+1);
                    m+=pt[i].v;
                    L+=pt[i].l;
                }
                else
                {
                    p[cnt]=pt[i];
                    cnt++;
                }
            }
            if(cnt==0) continue;
            Graham_scan(cnt);
            double cost=0.0;
            for(int i=0;i<=top;i++)
            {
                cost+=dis(p[sta[i]],p[sta[(i+1)%(top+1)]]);
            }
            if(L>=cost)
            {
                if(m<tmp)
                {
                    tmp=m;
                    ans=L-cost;
                    res.clear();
                    for(auto &t:use)
                    {
                        res.push_back(t);
                    }
                    use.clear();
                }
                else if(m==tmp)
                {
                    if(use.size()<res.size())
                    {
                        res.clear();
                        for(auto &t:use)
                        {
                            res.push_back(t);
                        }
                        use.clear();
                    }
                }
            }
        }
        printf("Forest %d\n",++Case);
        printf("Cut these trees:");
        for(auto &t:res)
        {
            printf(" %d",t);
        }
        printf("\n");
        printf("Extra wood: %.2lf\n",ans);
        printf("\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_40774175/article/details/81908783