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. javascript中的栈堆内存

    <--------栈内存---------> 俗称叫做作用域(全局作用域/私有作用域) >为js代码提供的执行环境(执行js代码的地方) >基本数据内省是直接存放在栈内存中的 ...

  2. 复习宝典之Git分布式版本控制

    查看更多宝典,请点击<金三银四,你的专属面试宝典> 第三章:Git分布式版本控制 1)git文件状态 git中的文件有以下几种状态: 未跟踪(untrack):表示文件为新增加的. 已修改 ...

  3. jquery的JSON字符串处理、单引号双引号的转换

    1.jquery的JSON字符串处理 var pwdlevel_val = "{"minLength":1,"maxLength":20," ...

  4. hadoop生态搭建(3节点)-10.spark配置

    # https://www.scala-lang.org/download/2.12.4.html# ================================================= ...

  5. 树莓派如何连接WIFI

    hello,大家好,我是叶秋! 上一期呢,给大家介绍了如何给树莓派安装系统,有细心的朋友就会发现上一期安装系统的文章漏了一点点知识,不知道机智的你是否有发现呢~~(尴尬

  6. easyui设置行的背景色

    var arr = new Array(3000082, 3000095); self.itemGrid.datagrid({ rowStyler: function (index, row) { f ...

  7. Fedora 下面安装FTP服务

    1. yum install vsftpd 2. systemctl disable vsftpd.service 3. systemctl stop vsftpd.service 4. system ...

  8. 分布式专题(二)——Zookeeper的ZAB协议介绍

  9. 全国Uber优步司机奖励政策 (1月4日-1月10日)

    本周已经公开奖励整的城市有:北 京.成 都.重 庆.上 海.深 圳.长 沙.佛 山.广 州.苏 州.杭 州.南 京.宁 波.青 岛.天 津.西 安.武 汉.厦 门,可按CTRL+F,搜城市名快速查找. ...

  10. 佛山Uber优步司机奖励政策(12月14日到12月20日)

    滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...