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

题目

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

题意

每个点坐标已知,并被赋予价值与长度,从当中选出一部分点的长度要大于等于剩下点的组成的凸包周长,并且选 让选出的点价值总和小的点集, 如果相同就选择点数少的点集。
用压状dp枚举每一种情况,1代表选出的点,0代表用来组成凸包的点。下面的judge函数(选 让选出的点价值总和小的点集, 如果相同就选择点数少的点集。)判断了是否要更新答案。此处有一个坑点,最后输出%.2f,%.2lf会错。原因是printf函数中没有对%lf的用法规定,%lf在printf中属于未定义行为。有的编译器会转化为%f,有的则不会。但scanf是支持%lf与%f读入的。

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <cmath>
#define ll long long
const int maxn = 17;
const double inf = 0x3f3f3f3f;
using namespace std;
int n;
struct point{
    double x, y, vl, len;
    int id;
    point (){}
    point (double a, double b):x(a),y(b){}
    point operator - (const point &b) const{
        return point(x-b.x, y-b.y);
    }
}p[maxn], c[maxn], s[maxn], res[maxn];
double cross(point a, point b){
    return a.x*b.y-a.y*b.x;
}
int cmp(point a, point b){
    return a.x < b.x ||(a.x==b.x && a.y < b.y);
}
int andrew( point a[], int n){
    sort(a, a+n, cmp);
    int m = 0, i;
    for(i = 0; i < n; i++){
        while(m > 1 && cross(res[m-1]-res[m-2], a[i] - res[m-2])<0)
            m--;
        res[m++] = a[i];
    }
    int k = m;
    for(i = n-2; i >= 0; i--){
        while(m > k && cross(res[m-1]-res[m-2], a[i] - res[m-2])<0)
            m--;
        res[m++] = a[i];
    }
    if(m>1) m--;
    return m;
}
double dis(point a, point b){
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double arealen(point res[], int k){
    double ans = 0;
    int i;
    for(i = 0; i < k; i++){
        ans += dis(res[i], res[(i+1)%k]);
    }
    return ans;
}
int judge(double vsum,double vans, int lenc, int cans){
    if(vsum < vans) return 1;
    else if(vsum == vans){
        if(lenc < cans)
            return 1;
        else
            return 0;
    }
    return 0;
}
int main(){
    int n, i, m, j, cont = 1;
    int lenc, lens, cans;
    double vsum, lsum;
    double vans, andlen;
    double wood;
    int id[20];
    while(~scanf("%d", &n) && n){
        cans = 20;vans = inf;
        for(i = 0; i < n; i++){
            scanf("%lf%lf%lf%lf", &p[i].x, &p[i].y, &p[i].vl, &p[i].len);
            p[i].id = i+1;
        }
        m = (1<<n)-1;
        for(i = 0; i <= m; i++){
            memset(c, 0, sizeof(c));
            memset(s, 0, sizeof(s));
            vsum = lsum = 0;
            lenc = lens = 0;
            for(j = 0; j < n; j++){
                if( i&(1<<j) ){
                    vsum += p[j].vl;
                    lsum += p[j].len;
                    c[lenc++] = p[j];
                }
                else
                    s[lens++] = p[j];
            }
            int k = andrew(s, lens);
            andlen = arealen(res, k);
            if(andlen <= lsum && judge(vsum, vans, lenc, cans)){
                vans = vsum;
                wood = lsum-andlen;
                cans = lenc;
                for(int q = 0; q < lenc; q++)
                    id[q] = c[q].id;
            }

        }
        printf("Forest %d\nCut these trees:", cont++);
        for(int q = 0; q < cans; q++){
            printf(" %d", id[q]);
        }
        printf("\nExtra wood: %.2f\n\n", wood);
    }
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值