POJ 1873 The Fortified Forest(枚举+凸包)
Description
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 ends with an empty test case (n = 0).
Output
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(枚举+凸包)的更多相关文章
- POJ 1873 The Fortified Forest(凸包)题解
题意:二维平面有一堆点,每个点有价值v和删掉这个点能得到的长度l,问你删掉最少的价值能把剩余点围起来,价值一样求删掉的点最少 思路:n<=15,那么直接遍历2^15,判断每种情况.这里要优化一下 ...
- POJ 1873 The Fortified Forest [凸包 枚举]
The Fortified Forest Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6400 Accepted: 1 ...
- ●POJ 1873 The Fortified Forest
题链: http://poj.org/problem?id=1873 题解: 计算几何,凸包 枚举被砍的树的集合.求出剩下点的凸包.然后判断即可. 代码: #include<cmath> ...
- POJ 1873 The Fortified Forest
题意:是有n棵树,每棵的坐标,价值和长度已知,要砍掉若干根,用他们围住其他树,问损失价值最小的情况下又要长度足够围住其他树,砍掉哪些树.. 思路:先求要砍掉的哪些树,在求剩下的树求凸包,在判是否可行. ...
- 简单几何(凸包+枚举) POJ 1873 The Fortified Forest
题目传送门 题意:砍掉一些树,用它们做成篱笆把剩余的树围起来,问最小价值 分析:数据量不大,考虑状态压缩暴力枚举,求凸包以及计算凸包长度.虽说是水题,毕竟是final,自己状压的最大情况写错了,而且忘 ...
- POJ 1873 The Fortified Forest 凸包 二进制枚举
n最大15,二进制枚举不会超时.枚举不被砍掉的树,然后求凸包 #include<stdio.h> #include<math.h> #include<algorithm& ...
- POJ 1873 - The Fortified Forest 凸包 + 搜索 模板
通过这道题发现了原来写凸包的一些不注意之处和一些错误..有些错误很要命.. 这题 N = 15 1 << 15 = 32768 直接枚举完全可行 卡在异常情况判断上很久,只有 顶点数 &g ...
- poj 1873 凸包+枚举
The Fortified Forest Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6198 Accepted: 1 ...
- poj1873 The Fortified Forest 凸包+枚举 水题
/* poj1873 The Fortified Forest 凸包+枚举 水题 用小树林的木头给小树林围一个围墙 每棵树都有价值 求消耗价值最低的做法,输出被砍伐的树的编号和剩余的木料 若砍伐价值相 ...
随机推荐
- SpringBoot整合Eureka搭建微服务
1.创建一个services项目,添加三个子模块client(客户端).service(服务端).registry(注册中心) 1.1 创建一个services项目 1.2 添加pom.xml依赖 & ...
- jquery头像上传剪裁插件cropper的前后台demo
因为一个项目要做一个头像上传的功能,因此选择了使用jquery的头像插件cropper,cropper是一款使用简单且功能强大的图片剪裁jQuery插件,但是在使用的时候,有一个很大的坑需要注意,那就 ...
- 用JQ实现的一个简单轮播
<!DOCTYPE html><html><head> <meta charset="utf-8"> <title>lb ...
- HTML表格-table
表格 表格由 <table> 标签来定义. 每个表格均有若干行(由 <tr> 标签定义),每行被分割为若干单元格(由 <td> 标签定义). 字母 td 指表格数据 ...
- ruby 正则表达式Regexp
ruby正则表达式在线编辑器:rubular 一般规则: /a/匹配字符a. /\?/匹配特殊字符?.特殊字符包括^, $, ? , ., /, \, [, ], {, }, (, ), + ...
- 顺序表删除值为 x 的元素
思路: 不要去考虑删除的字眼,要考虑如何进行保存非 x 的值 这里提供两种解法,殊途同归: 1.将其中非 x 的元素统计并保存 2.统计为 x 的元素个数,并将非 x 的元素保存 注意事项: 注意这里 ...
- 观看杨老师(杨旭)Asp.Net Core MVC入门教程记录
观看杨老师(杨旭)Asp.Net Core MVC入门教程记录 ASP.NET Core MVC入门 Asp.Net Core启动和配置 Program类,Main方法 Startup类 依赖注入,I ...
- HyperLedger Fabric 1.4 Solo模式简介(10.1)
Solo模式指单节点通信模式,该环境中只有一个排序(orderer)服务,从节点(peer)发送来的消息由一个orderer进行排序和产生区块:由于排序(orderer)服务只有一个orderer为所 ...
- OracleLinux上安装Oracle11g图解
磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面: PostgreSQL杂记页 回到顶级页面:PostgreSQL索引页 [作者 高健@博客园 luckyjackgao@gmail. ...
- 北京Uber优步司机奖励政策(9月21日~9月27日)
用户组:优步北京人民优步A组(适用于9月21日-9月27日) 滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不 ...