POJ - 1873 The Fortified Forest

The Fortified Forest
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 6960 Accepted: 1939

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


题意:现在有一些树,给出树的坐标价值和高度,现在需要砍一些树把剩下的树做成栅栏把剩下的树全部围起来,砍树的规则要求剩下的树的价值最大,价值相同的情况下,砍的树最少,输出当前是第几组,输出砍掉的树的编号,最后输出砍掉的树做成栅栏之后剩下的高度。

思路:这个题除了暴力枚举需要砍的树我实在想不起来其他办法,而且给的数据最多只有15颗树明摆着让我们暴力枚举的,枚举的方法,从0到2的n次方减1全部转换成2进制数,0对应的表示要砍的树1对应的保留的树,也就是最大循环2的15次方次,完全可行。



#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <stack>
#include <queue>
#include <map>
#include <vector>
#include <algorithm>
#define inf 0x3f3f3f3f
#define eps 1e-9

using namespace std;

struct node
{
    double x, y;
    double v, l;
};

struct tree
{
    double l, ml;
};

node P[11000], p[11000];
node s[11000], minp;
double v, minv;
int cut, minc;
tree p1, p2;

double dist(node a, node b)
{
    return sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2));
}

double multi(node p1, node p2, node p3)
{
    return p1.x*p2.y+p3.x*p1.y+p2.x*p3.y-p3.x*p2.y-p2.x*p1.y-p1.x*p3.y;
}

bool cmp(node a, node b)
{
    if(abs(multi(minp,a,b) - 0) <= eps)
    {
        return dist(minp,a) < dist(minp,b);
    }
    return multi(minp,a,b) > 0;
}

void Graham(int x, int n)
{
    minp.x = inf;
    minp.y = inf;
    int m = 0;
    int top = 0;
    double sum = 0, l = 0;
    v = 0;
    cut = 0;
    for(int i = 0; i < n; i++)
    {
        if((x | (1 << i)) == x)
        {
            p[m++] = P[i];
            if(P[i].y < minp.y || (abs(P[i].y - minp.y) <= eps && P[i].x < minp.x))
                minp = P[i];
        }
        else
        {
            cut++;
            l += P[i].l;
            v += P[i].v;
            if(v > minv)
                return;
        }
    }
    if(m < 1)
    {
        cut = inf;
        v = inf;
        return;
    }
    if(m >= 2)
    {
        sort(p,p+m,cmp);
        s[0] = p[0];
        s[1] = p[1];
        top = 2;
        for(int i = 2; i < m; i++)
        {
            while(multi(s[top-2],s[top-1],p[i]) < 0)
                top--;
            s[top++] = p[i];
        }
        for(int i = 0; i < top; i++)
        {
            sum += dist(s[i%top],s[(i+1)%top]);
        }
        if(sum > l)
        {
            cut = inf;
            v = inf;
            return;
        }
    }
    p1.l = sum;
    p1.ml = l;
}

int main()
{
    int n, i, minx, x = 1;
    while(cin>>n && n)
    {
        minv = inf;
        minc = inf;
        p2.l = p2.ml = 0;
        minx = (1 << n) - 1;
        for(i = 0; i < n; i++)
            cin>>P[i].x>>P[i].y>>P[i].v>>P[i].l;
        for(i = 0; i < (1 << n); i++)
        {
            Graham(i,n);
            if(v < minv || (abs(v - minv) <= eps && minc < cut))
            {
                p2 = p1;
                minv = v;
                minc = cut;
                minx = i;
            }
        }
        cout<<"Forest "<<x++<<endl;
        cout<<"Cut these trees:";
        for(i = 0; i < n; i++)
        {
            if((minx | (1 << i)) != minx)
                cout<<' '<<i + 1;
        }
        cout<<endl;
        printf("Extra wood: %.2lf\n\n",p2.ml - p2.l);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值