POJ 1873 The Fortified Forest(凸包+枚举 World Finals 1999啊)

题目链接:http://poj.org/problem?id=1873

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

题意:

有n棵树(n<=15), 每棵树的价值为 v , 砍掉这个树能得到长度为 l
的木板。

现在要砍掉其中的一些树,用来造木板把剩余的所有树围起来,(每颗树只有砍与不砍两种状态),

问要造这种木板最少需要消耗多少的价值(砍掉的树的 v 的和),

输出需要砍掉哪些树和剩余的木板长度(砍掉的树形成的木板并且包围未砍掉的树没有用完的木板长度)。

PS:

n比较小,我们可以枚举砍掉的树!

代码如下:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <iostream>
using namespace std;
#define INF 0x3f3f3f3f
const double eps = 1e-8;
const double PI = acos(-1.0);
const int MAXN = 1010;
int sgn(double x)
{
    if(fabs(x) < eps)return 0;
    if(x < 0)return -1;
    else return 1;
}
struct Point
{
    int x, y;
    int v, l;
    Point() {}
    Point(int _x, int _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;
    }
};
Point list[MAXN], tr[MAXN];
int Stack[MAXN], top;
//*两点间距离
double dist(Point a,Point b)
{
    return sqrt((a-b)*(a-b));
}
//相对于list[0]的极角排序
bool _cmp(Point p1,Point p2)
{
    double tmp = (p1-list[0])^(p2-list[0]);
    if(sgn(tmp) > 0)return true;
    else if(sgn(tmp) == 0 && sgn(dist(p1,list[0]) - dist(p2,list[0])) <= 0)
        return true;
    else return false;
}
void Graham(int n)
{
    Point p0;
    int k = 0;
    p0 = list[0];
//找最下边的一个点
    for(int i = 1; i < n; i++)
    {
        if( (p0.y > list[i].y) || (p0.y == list[i].y && p0.x > list[i].x) )
        {
            p0 = list[i];
            k = i;
        }
    }
    swap(list[k],list[0]);
    sort(list+1,list+n,_cmp);
    if(n == 1)
    {
        top = 1;
        Stack[0] = 0;
        return;
    }
    if(n == 2)
    {
        top = 2;
        Stack[0] = 0;
        Stack[1] = 1;
        return ;
    }
    Stack[0] = 0;
    Stack[1] = 1;
    top = 2;
    for(int i = 2; i < n; i++)
    {
        while(top > 1 &&
                sgn((list[Stack[top-1]]-list[Stack[top-2]])^(list[i]-list[Stack[top-2]])) <=
                0)
            top--;
        Stack[top++] = i;
    }
}
int main()
{
    int n;
    int cas = 0;
    int ans[20];
    while(scanf("%d",&n) && n)
    {
        int x, y, v, l;
        int pnum;//最少砍掉的数量
        double rest = 0;//剩余的长度
        int min_tree = n+1;
        int min_val = INF;
        for(int i = 0; i < n; i++)
        {
            scanf("%d%d%d%d",&tr[i].x,&tr[i].y,&tr[i].v,&tr[i].l);
        }
        for(int i = 0; i < (1<<n); i++)
        {
            double len, val;
            int num;
            len = num = val = 0;
            int k = 0;
            int vis[27];
            memset(list,0,sizeof(list));
            for(int j = 0; j < n; j++)
            {
                vis[j] = 0;
                if(i & (1<<j))//砍掉
                {
                    val+=tr[j].v;
                    len+=tr[j].l;
                    num++;
                    vis[j] = 1;
                }
                else
                {
                    list[k++] = Point(tr[j].x, tr[j].y);
                }
            }
            if(val > min_val || (val==min_val && min_tree < num))
            {
                continue;
            }
            if(len == 0 && k != 1)
            {
                continue;
            }
            Graham(k);
            double res = 0;//凸包周长
            for(int h = 0; h < top-1; h++)
            {
                res+=dist(list[Stack[h]],list[Stack[h+1]]);
            }
            res+=dist(list[Stack[0]],list[Stack[top-1]]);

            //printf("len::%.2lf >>res:%.2lf, ans::%.2lf\n",len,res,len-res);
            if(res <= len)//能包围
            {
                min_val = val;
                min_tree = num;
                rest = len - res;
                pnum = 0;
                for(int f = 0; f < n; f++)
                {
                    if(vis[f])
                    {
                        ans[pnum++] = f;
                    }
                }
            }
        }
        printf("Forest %d\n",++cas);
        printf("Cut these trees:");
        for(int i = 0; i < pnum; i++)
        {
            printf(" %d",ans[i]+1);
        }
        printf("\nExtra wood: %.2f\n\n", rest);
    }
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-05 20:30:06

POJ 1873 The Fortified Forest(凸包+枚举 World Finals 1999啊)的相关文章

poj 1873 The Fortified Forest(凸包)

The Fortified Forest Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5737   Accepted: 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

POJ 1873 - The Fortified Forest 凸包 + 搜索 模板

通过这道题发现了原来写凸包的一些不注意之处和一些错误..有些错误很要命.. 这题 N = 15 1 << 15 = 32768 直接枚举完全可行 卡在异常情况判断上很久,只有 顶点数 >= 2,即 n >= 3 时凸包才有意义 顶点数为 1 时,tmp = - 1 要做特殊判断. 总结了一下凸包模板 //template Convex Hull friend bool operator < (const point &p1, const point &p2){

Poj 1873 The Fortified Forest

地址:http://poj.org/problem?id=1873 题目: The Fortified Forest Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6421   Accepted: 1811 Description Once upon a time, in a faraway land, there lived a king. This king owned a small collection of r

Uva5211/POJ1873 The Fortified Forest 凸包

LINK 题意:给出点集,每个点有个价值v和长度l,问把其中几个点取掉,用这几个点的长度能把剩下的点围住,要求剩下的点价值和最大,拿掉的点最少且剩余长度最长. 思路:1999WF中的水题.考虑到其点的数量最多只有15个,那么可以使用暴力枚举所有取点情况,二进制压缩状态,预处理出该状态下的价值,同时记录该状态拥有的点,并按价值排序.按价值枚举状态,并对拥有的这些点求凸包,check是否合法,找到一组跳出即可.然而POJ似乎没有SPJ,同样的代码POJ会超时,UVA60ms,可以在常数上优化,不预处

UVA 811 The Fortified Forest (凸包 + 状态压缩枚举)

题目链接:UVA 811 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 or

poj 1873 凸包+枚举

The Fortified Forest Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6198   Accepted: 1744 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

poj 1873(枚举所有的状态+凸包)

The Fortified Forest Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6115   Accepted: 1720 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

POJ 1873

按位枚举,按最小价值,最小砍掉树剪枝就好了. #include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; const int MAXN=20; const int inf=190000000; int n,l; int st[MAXN],stop,cnt; int ans[MA

poj1873The Fortified Forest

链接 居然是WF的水题~ 二进制枚举砍哪些树,剩余的树围成一个凸包. 因为传数组WA了两发,忘记修改排序数组中的p[0]; 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<cmath> 8 #include