POJ 1873 The Fortified Forest (状态压缩+凸包 WF 推荐)

77 篇文章 8 订阅
22 篇文章 0 订阅
The Fortified Forest
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 6366 Accepted: 1798

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


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


题目大意:给一些树,每个树有个坐标,价值和长度,现在要选其中一些树砍掉来给剩下的树作围栏,要求剩余树的价值尽可能大,价值相等的情况下,要求砍的树的数量尽可能少,输出要砍的树的编号和剩余的木材长度


题目分析:上古WF的水题,n非常小,直接二进制状压枚举,对于不砍的点集求凸包周长,按要求选最优即可,注意当只剩两棵树时,将它们围起来需要两倍其距离,这里不用特判,算凸包时直接把起点接到最后一个点后面即可,还有就是只剩一棵树时就不用围了,wa了n次一直以为什么地方有trick,其实没trick,自己凸包写错了- -

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int const INF = 0x6fffffff;
int n, top, cnt, ansSta;
struct POINT {
    int x, y;
}p[20], stk[20], base;

struct TREE {
    int x, y, val;
    double len;
}t[20];

double getDist(POINT p1, POINT p2) {
    return sqrt(1.0 * (p1.x - p2.x) * (p1.x - p2.x) + 1.0 * (p1.y - p2.y) * (p1.y - p2.y));
}

int getCross(POINT p0, POINT p1, POINT p2) {
    return (p1.x - p0.x) * (p2.y - p0.y) - (p2.x - p0.x) * (p1.y - p0.y);
}

bool cmp(POINT p1, POINT p2) {
    if (getCross(base, p1, p2) == 0) {
        return getDist(base, p1) < getDist(base, p2);
    }
    if (getCross(base, p1, p2) > 0) {
        return true;
    }
    return false;
}

void getConvex() {
    if (cnt == 1) {
        stk[0] = p[0];
        return;
    }
    base.x = p[0].x;
    base.y = p[0].y;
    int pos = 0;
    for (int i = 1; i < cnt; i ++) {
        if (p[i].y < base.y || (p[i].y == base.y && p[i].x < base.x)) {
            base.x = p[i].x;
            base.y = p[i].y;
            pos = i;
        }
    }
    swap(p[0], p[pos]);
    sort(p + 1, p + cnt, cmp);
    top = 1;
    stk[0] = p[0];
    stk[1] = p[1];
    for (int i = 2; i < cnt; i ++) {
        while (top > 0 && getCross(stk[top - 1], stk[top], p[i]) <= 0) {
            top --;
        }
        stk[++ top] = p[i];
    }
}

double getLen() {
    if (cnt == 1) {
        return 0;
    }
    stk[++ top] = p[0];
    double ans = 0;
    for (int i = 0; i < top; i ++) {
        ans += getDist(stk[i], stk[i + 1]);
    }
    return ans;
}

int main() {
    int ca = 1;
    while (scanf("%d", &n) != EOF && n) {
        for (int i = 0; i < n; i ++) {
            scanf("%d %d %d %lf", &t[i].x, &t[i].y, &t[i].val, &t[i].len);
        }
        int minVal = INF, minCnt = n, sumVal;
        double extra = 0, sumLen;
        for (int sta = 1; sta < (1 << n) - 1; sta ++) {
            cnt = sumVal = 0;
            sumLen = 0;
            for (int i = 0; i < n; i ++) {
                if ((1 << i) & sta) {   //cut
                    sumLen += t[i].len; 
                    sumVal += t[i].val;
                }
                else {                  //don't cut
                    p[cnt].x = t[i].x;
                    p[cnt ++].y = t[i].y;
                }
            }
            if (sumVal > minVal) {
                continue;
            }
            getConvex();
            double useLen = getLen();
            if (useLen <= sumLen) {
                if (minVal > sumVal || (minVal == sumVal && n - cnt < minCnt)) {
                    minVal = sumVal;
                    ansSta = sta;
                    extra = sumLen - useLen;
                    minCnt = n - cnt;
                }
            }
        }
        if (ca != 1) {
            printf("\n");
        }
        printf("Forest %d\n", ca ++);
        printf("Cut these trees:");
        for (int i = 0; i < n; i ++) {
            if ((1 << i) & ansSta) {
                printf(" %d", i + 1);
            }
        }
        printf("\nExtra wood: %.2f\n", extra);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值