poj 1873 凸包+枚举
| Time Limit: 1000MS | Memory Limit: 30000K | |
| Total Submissions: 6198 | Accepted: 1744 |
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.
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
/*
poj 1873 凸包+枚举 给你n棵树,已知树的树的长度以及他们的价值。要砍掉一些树来给剩下的数围一个篱笆
要求剩下的数价值尽可能大,如果价值相同则希望剩下的树尽可能多 因为最多15棵,枚举需要砍掉的树,然后通过凸包判断是否能围成,记录一下即可 hhh-2016-05-07 21:27:41
*/
#include <iostream>
#include <vector>
#include <cstring>
#include <string>
#include <cstdio>
#include <queue>
#include <cmath>
#include <algorithm>
#include <functional>
#include <map>
using namespace std;
#define lson (i<<1)
#define rson ((i<<1)|1)
typedef long long ll;
using namespace std;
const int maxn = 20;
double PI = 3.1415926;
double eps = 1e-8; int sgn(double x)
{
if(fabs(x) < eps) return 0;
if(x < 0)
return -1;
else
return 1;
} struct Point
{
double x,y;
Point() {}
Point(double _x,double _y)
{
x = _x,y = _y;
}
Point operator -(const Point &b)const
{
return Point(x-b.x,y-b.y);
}
double operator ^(const Point &b)const
{
return x*b.y-y*b.x;
}
double operator *(const Point &b)const
{
return x*b.x + y*b.y;
}
}; struct Line
{
Point s,t;
Line() {}
Line(Point _s,Point _t)
{
s = _s;
t = _t;
}
pair<int,Point> operator &(const Line&b)const
{
Point res = s;
if( sgn((s-t) ^ (b.s-b.t)) == 0) //通过叉积判断
{
if( sgn((s-b.t) ^ (b.s-b.t)) == 0)
return make_pair(0,res);
else
return make_pair(1,res);
}
double ta = ((s-b.s)^(b.s-b.t))/((s-t)^(b.s-b.t));
res.x += (t.x-s.x)*ta;
res.y += (t.y-s.y)*ta;
return make_pair(2,res);
}
};
Point lis[maxn];
int Stack[maxn],top; double dist(Point a,Point b)
{
return sqrt((a-b)*(a-b));
}
Point ta[20];
bool cmp(Point a,Point b)
{
double t = (a-ta[0])^(b-ta[0]);
if(sgn(t) == 0)
{
return dist(a,ta[0]) <= dist(b,ta[0]);
}
if(sgn(t) < 0)
return false;
else
return true;
} int tot;
double Graham(int n)
{
Point p;
if(n == 1 || n == 0)
{
return 0;
}
if(n == 2)
{
return dist(ta[0],ta[1])*2;
}
int k = 0;
p = ta[0];
for(int i = 1; i < n; i++)
{
if(p.y > ta[i].y || (p.y == ta[i].y && p.x > ta[i].x))
p = ta[i],k = i;
}
swap(ta[0],ta[k]);
sort(ta+1,ta+n,cmp);
Stack[0] = 0;
Stack[1] = 1;
top = 2;
for(int i = 2; i < n; i++)
{
while(top > 1 && sgn((ta[Stack[top-1]]-ta[Stack[top-2]])
^ (ta[i]-ta[Stack[top-2]])) <= 0)
top --;
Stack[top++] = i;
}
double len = 0;
for(int i = 0; i < top; i++)
{
if(i == top - 1)
len += dist(ta[Stack[i]],ta[Stack[0]]);
else
len += dist(ta[Stack[i]],ta[Stack[i+1]]);
}
return len;
} int val[maxn];
double lent[maxn]; int main()
{
//freopen("in.txt","r",stdin);
int n;
int cas = 1;
while(scanf("%d",&n) && n)
{
if(cas != 1)
printf("\n");
for(int i = 0; i < n; i++)
{
scanf("%lf%lf%d%lf",&lis[i].x,&lis[i].y,&val[i],&lent[i]);
}
int ansV = 0x7fffffff,ansN = 0x7fffffff,ansX = 0;
double ansL = 0;
for(int i = 0; i < (1<<n); i++)
{
tot = 0;
double lans = 0;
int vans = 0;
for(int j = 0; j < n; j++)
{
if(i & (1 << j))
{
vans += val[j];
lans += lent[j];
}
else
{
ta[tot++] = lis[j];
}
}
if(vans > ansV)
continue;
double t = Graham(tot);
if(lans >= t)
{
if(vans < ansV || (n-tot < ansN && vans == ansV))
{
ansV = vans;
ansL = lans-t;
ansX = i;
ansN = n-tot;
}
}
}
printf("Forest %d\n",cas++);
printf("Cut these trees:");
for(int i = 0; i < n; i++)
{
if(ansX&(1 << i))
printf(" %d",i+1);
}
printf("\n");
printf("Extra wood: %.2f\n",ansL);
}
return 0;
}
poj 1873 凸包+枚举的更多相关文章
- poj 1873(枚举所有的状态+凸包)
The Fortified Forest Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6115 Accepted: 1 ...
- The Fortified Forest - POJ 1873(状态枚举+求凸包周长)
题目大意:有个国王他有一片森林,现在他想从这个森林里面砍伐一些树木做成篱笆把剩下的树木围起来,已知每个树都有不同的价值还有高度,求出来砍掉那些树可以做成篱笆把剩余的树都围起来,要使砍伐的树木的价值最小 ...
- ●POJ 1873 The Fortified Forest
题链: http://poj.org/problem?id=1873 题解: 计算几何,凸包 枚举被砍的树的集合.求出剩下点的凸包.然后判断即可. 代码: #include<cmath> ...
- poj1873 The Fortified Forest 凸包+枚举 水题
/* poj1873 The Fortified Forest 凸包+枚举 水题 用小树林的木头给小树林围一个围墙 每棵树都有价值 求消耗价值最低的做法,输出被砍伐的树的编号和剩余的木料 若砍伐价值相 ...
- poj 1113 凸包周长
Wall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 33888 Accepted: 11544 Descriptio ...
- Poj 2187 凸包模板求解
Poj 2187 凸包模板求解 传送门 由于整个点数是50000,而求凸包后的点也不会很多,因此直接套凸包之后两重循环即可求解 #include <queue> #include < ...
- 简单几何(凸包+枚举) POJ 1873 The Fortified Forest
题目传送门 题意:砍掉一些树,用它们做成篱笆把剩余的树围起来,问最小价值 分析:数据量不大,考虑状态压缩暴力枚举,求凸包以及计算凸包长度.虽说是水题,毕竟是final,自己状压的最大情况写错了,而且忘 ...
- POJ 1873 The Fortified Forest [凸包 枚举]
The Fortified Forest Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 6400 Accepted: 1 ...
- POJ 1873 The Fortified Forest(枚举+凸包)
Description Once upon a time, in a faraway land, there lived a king. This king owned a small collect ...
随机推荐
- 【iOS】单元测试
iOS单元测试(作用及入门提升) 字数1704 阅读16369 评论26 喜欢247 由于只是一些简单实用的东西,学学还是挺不错的.其实单元测试用的好,开发起来也会快很多.单元测试对于我目前来说,就是 ...
- 第二次作业:APP案例分析
App案例分析 产品:三国杀-页游手游双通 选择理由 当今社会手机已经渐渐取代了电脑在人们日常生活的需求,既然要选择APP进行案例分析,首推的估计就是手机APP了.三国杀是陪伴我高中时代的主要娱乐方式 ...
- 第四十三条:返回零长度的数组或者集合,而不是null
如果一个方法的返回值类型是集合或者数组 ,如果在方法内部需要返回的集合或者数组是零长度的,也就是没有实际对象在里面, 我们也应该放回一个零长度的数组或者集合,而不是返回null.如果返回了null,客 ...
- LR回放https协议脚本失败: 错误 -27778: 在尝试与主机“www.baidu.com”connect 时发生 SSL 协议错误
今天用LR录制脚本协议为https协议,回放脚本时出现报错: Action.c(14): 错误 -27778: 在尝试与主机"www.baidu.com"connect 时发生 S ...
- Centos6.7下面配置vim及其插件
Vim是在vi的基础上升级而来的,比vi更强大,提供代码补全,编译功能 [4]vim Vim是从 vi 发展出来的一个文本编辑器.代码补完.编译及错误跳转等方便编程的功能特别丰富,在程序员中被广泛使用 ...
- awk sed tr替换换行符为逗号,并合并为一行
在群里看到的.记录以备用. sed 帮助命令:http://man.linuxde.net/sed 文件里有如下行,我想将每行的回车符替换为逗号,并将所有行合并到一行,用awk或sed怎么写啊TOP ...
- 写一个vue组件
写一个vue组件 我下面写的是以.vue结尾的单文件组件的写法,是基于webpack构建的项目.如果还不知道怎么用webpack构建一个vue的工程的,可以移步到vue-cli. 一个完整的vue组件 ...
- HTTP协议扫盲(一)HTTP协议的基本概念和通讯原理
一.HTTP协议的概念 1.引子 - 从url开始 URL(Uniform Resource Locator) 地址用于描述一个网络上的资源, 基本格式如下 schema://host[:port# ...
- Django之中间件
中间件简介 什么是中间件 中间件是一个用来处理Django的请求和响应的框架级别的钩子.它是一个轻量.低级别的插件系统,用于在全局范围内改变Django的输入和输出.每个中间件组件都负责做一些特定的功 ...
- 将Tomcat添加进服务启动
tomcat有解压版和安装版2种版本,安装版已经做好了将tomcat添加进服务的操作,而解压版需要我们自己来实现,应用场景主要是在服务器端需要在服务器启动时就启动tomcat. 1.首先需要配置好jd ...