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.

 

题目大意:有n棵树,每棵树有坐标(x,y),价值v,长度l,问如何砍能砍掉最小价值为的树(价值相同则砍最少的树),能把其他树都围起来
思路:枚举所有砍树的方案(我用的递归,用二进制的方法理论上来说也可以),算一下能不能围起剩下的树(如果价值比当前答案要大就不用算了)。至于怎么围起剩下的树,一个点的明显是需要0长度,两个点就需要这两个点的距离*2,三个点或以上就要用到求凸包的方法(反正我的凸包是不能算三个点以下的)

PS:输出最好复制啊,我好像就是因为forest打错了WA了好几次啊……

 #include <cstdio>
#include <cmath>
#include <algorithm>
using namespace std; const double EPS = 1e-; inline int sgn(const double &x) {
if(fabs(x) < EPS) return ;
return x > ? : -;
} struct Point {
double x, y;
int v, l;
}; inline bool Cross(Point &sp, Point &ep, Point &op) {
return (sp.x - op.x) * (ep.y - op.y) - (ep.x - op.x) * (sp.y - op.y) >= ;
} inline double dist(Point &a, Point &b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
} inline bool cmp(const Point &a, const Point &b) {
if(a.y == b.y) return a.x < b.x;
return a.y < b.y;
} const int MAXN = ;
int stk[MAXN];
bool cut[MAXN], ans[MAXN];
Point p[MAXN], a[MAXN];
int n, top;
double answood; double Graham_scan(int n) {
sort(p, p + n, cmp);
top = ;
stk[] = ; stk[] = ;
for(int i = ; i < n; ++i) {
while(top && Cross(p[i], p[stk[top]], p[stk[top - ]])) --top;
stk[++top] = i;
}
int len = top;
stk[++top] = n - ;
for(int i = n - ; i >= ; --i) {
while(top != len && Cross(p[i], p[stk[top]], p[stk[top - ]])) --top;
stk[++top] = i;
}
double sum = ;
stk[++top] = stk[];
for(int i = ; i < top; ++i)
sum += dist(p[stk[i]], p[stk[i+]]);
return sum;
} int minval, mincut, sumval, sumlen;
double uselen; void setans(int cutcnt) {
for(int i = ; i <= n; ++i) ans[i] = cut[i];
minval = sumval;
mincut = cutcnt;
answood = sumlen - uselen;
} void dfs(int dep, int cutcnt) {
if(dep == n + ) {
if(n == cutcnt) return ;
sumval = sumlen = ;
for(int i = ; i <= n; ++i) {
if(!cut[i]) continue;
sumval += a[i].v;
sumlen += a[i].l;
}
if(sumval > minval) return ;
if(sumval == minval && cutcnt >= mincut) return ;
if(n - cutcnt == ) {
uselen = ;
setans(cutcnt);
}
else if(n - cutcnt == ) {
int i1 = , i2 = ;
for(int i = ; i <= n; ++i) {
if(cut[i]) continue;
if(!i1) i1 = i;
else i2 = i;
}
uselen = * dist(a[i1], a[i2]);
if(uselen <= sumlen) setans(cutcnt);
}
else {
int pcnt = ;
for(int i = ; i <= n; ++i) {
if(cut[i]) continue;
p[pcnt++] = a[i];
}
uselen = Graham_scan(pcnt);
if(sgn(uselen - sumlen) <= ) setans(cutcnt);
}
return ;
}
cut[dep] = false;
dfs(dep + , cutcnt);
cut[dep] = true;
dfs(dep + , cutcnt + );
} int main() {
int ca = ;
while(scanf("%d", &n) != EOF && n) {
for(int i = ; i <= n; ++i) {
scanf("%lf%lf%d%d", &a[i].x, &a[i].y, &a[i].v, &a[i].l);
}
mincut = MAXN;
minval = 0x7fffffff;
dfs(, );
if(ca != ) printf("\n");
printf("Forest %d\n", ca++);
printf("Cut these trees:");
for(int i = ; i <= n; ++i) if(ans[i]) printf(" %d", i);
printf("\nExtra wood: %.2f\n", answood);
}
}

POJ 1873 The Fortified Forest(枚举+凸包)的更多相关文章

  1. POJ 1873 The Fortified Forest(凸包)题解

    题意:二维平面有一堆点,每个点有价值v和删掉这个点能得到的长度l,问你删掉最少的价值能把剩余点围起来,价值一样求删掉的点最少 思路:n<=15,那么直接遍历2^15,判断每种情况.这里要优化一下 ...

  2. POJ 1873 The Fortified Forest [凸包 枚举]

    The Fortified Forest Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6400   Accepted: 1 ...

  3. ●POJ 1873 The Fortified Forest

    题链: http://poj.org/problem?id=1873 题解: 计算几何,凸包 枚举被砍的树的集合.求出剩下点的凸包.然后判断即可. 代码: #include<cmath> ...

  4. POJ 1873 The Fortified Forest

    题意:是有n棵树,每棵的坐标,价值和长度已知,要砍掉若干根,用他们围住其他树,问损失价值最小的情况下又要长度足够围住其他树,砍掉哪些树.. 思路:先求要砍掉的哪些树,在求剩下的树求凸包,在判是否可行. ...

  5. 简单几何(凸包+枚举) POJ 1873 The Fortified Forest

    题目传送门 题意:砍掉一些树,用它们做成篱笆把剩余的树围起来,问最小价值 分析:数据量不大,考虑状态压缩暴力枚举,求凸包以及计算凸包长度.虽说是水题,毕竟是final,自己状压的最大情况写错了,而且忘 ...

  6. POJ 1873 The Fortified Forest 凸包 二进制枚举

    n最大15,二进制枚举不会超时.枚举不被砍掉的树,然后求凸包 #include<stdio.h> #include<math.h> #include<algorithm& ...

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

    通过这道题发现了原来写凸包的一些不注意之处和一些错误..有些错误很要命.. 这题 N = 15 1 << 15 = 32768 直接枚举完全可行 卡在异常情况判断上很久,只有 顶点数 &g ...

  8. poj 1873 凸包+枚举

    The Fortified Forest Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6198   Accepted: 1 ...

  9. poj1873 The Fortified Forest 凸包+枚举 水题

    /* poj1873 The Fortified Forest 凸包+枚举 水题 用小树林的木头给小树林围一个围墙 每棵树都有价值 求消耗价值最低的做法,输出被砍伐的树的编号和剩余的木料 若砍伐价值相 ...

随机推荐

  1. Linux中文件函数(二)

    一.link.linkat.unlink.unlinkat.remove函数 创建一个指向现有文件的链接的方法是使用link函数或linkat函数.函数的原型为: #include <unist ...

  2. Flask入门数据库的查询集与过滤器(十一)

    1 查询集 : 指数据查询的集合 原始查询集: 不经过任何过滤返回的结果为原始查询集 数据查询集: 将原始查询集经过条件的筛选最终返回的结果 查询过滤器: 过滤器 功能 cls.query.filte ...

  3. 用原生JS写一个网页版的2048小游戏(兼容移动端)

    这个游戏JS部分全都是用原生JS代码写的,加有少量的CSS3动画,并简单的兼容了一下移动端. 先看一下在线的demo:https://yuan-yiming.github.io/2048-online ...

  4. 解决h5底部输入框在ios被软键盘顶飞 软键盘消失还下不来

    好吧,其实不是顶飞,准确点说应该是h5页面fiexed定位在底部的输入框在ios软键盘弹起的时候软键盘跟输入框有时会有一段悬空的距离,无法紧贴.在安卓机子上则没有这样的情况. 解决方法是通过h5的sc ...

  5. nginx通过upstream实现负载均衡

    随着业务和用户不断增加,单台服务器无法满足业务需求,产生服务器集群的场景.为了能充分利用服务器集群,最理想的方式就是整个集群的利用率都很平均且稳定在理想值范围内. 负载均衡(Load Balance) ...

  6. 【NXP开发板应用—智能插排】3.驱动GPIO点亮外接LED

    [前言] 首先感谢深圳市米尔科技有限公司举办的这次活动并予以本人参加这次活动的机会,以往接触过嵌入式,但那都是皮毛,最多刷个系统之类的,可以说对于嵌入式系统开发这件事情是相当非常陌生的,这次活动为我提 ...

  7. Leecode刷题之旅-C语言/python-349两个数组的交集

    /* * @lc app=leetcode.cn id=349 lang=c * * [349] 两个数组的交集 * * https://leetcode-cn.com/problems/inters ...

  8. FPGA算法学习(1) -- Cordic(Verilog实现)

    上两篇博文Cordic算法--圆周系统之旋转模式.Cordic算法--圆周系统之向量模式做了理论分析和实现,但是所用到的变量依然是浮点型,而cordic真正的用处是基于FPGA等只能处理定点的平台.只 ...

  9. 前端页面加载速度优化---Ngnix之GZIP压缩

    gzip on; #开启Gzip gzip_static on;#是否开启gzip静态资源 #nginx对于静态文件的处理模块,该模块可以读取预先压缩的gz文件,这样可以减少每次请求进行gzip压缩的 ...

  10. Java设计模式(10)——结构型模式之代理模式(Proxy)

    一.概述 概念 UML简图 实际使用的场景示例 如果那个对象是一个是很大的图片,需要花费很长时间才能显示出来,那么当这个图片包含在文档中时,使用编辑器或浏览器打开这个文档,打开文档必须很迅速,不能等待 ...