通过这道题发现了原来写凸包的一些不注意之处和一些错误..有些错误很要命..

这题 N = 15

1 << 15 = 32768 直接枚举完全可行

卡在异常情况判断上很久,只有 顶点数 >= 2,即 n >= 3 时凸包才有意义

顶点数为 1 时,tmp = - 1 要做特殊判断。

总结了一下凸包模板

//template Convex Hull

friend bool operator < (const point &p1, const point &p2){
return (p1.x < p2.x)||(p1.x == p2.x)&&(p1.y < p2.y);
} void BBS(point p[], int n){
for (int i = 0; i < n; i++){
for (int j = 0; j < i; j++){
if (p[i] < p[j])
swap(p[i], p[j]);
}
}
} void Convex_Hull(point p[], int n){
BBS(p, n); //先按横坐标升序排序,保证p[0]在凸包上
cur = 0;
while (1){
int tmp = - 1;
for (int i = 0; i < n; i++){
if (i != cur){
if (!(tmp + 1)||(((p[cur] >> p[i]) ^ (p[cur] >> p[tmp])) > 0))
tmp = i;
}
}
if (tmp + 1){
//找到凸包上的点p[tmp]
}
if (!tmp||!(tmp + 1)) break;
cur = tmp;
}
}

POJ1873.cpp

//POJ1873
//DFS + 凸包
//注意规避异常状况
//if (tmp + 1)
// d += (p[cur] >> p[tmp]).norm();
//写代码不认真,出现了许多错误,务必注意
//AC 2016-10-14 #include "cstdio"
#include "cstdlib"
#include "cmath"
#include "iostream"
#define MAXN 20 double sqr(double x){
return x * x;
} struct point{
int x, y, v, l;
bool cut;
point(){}
point(int X, int Y): x(X), y(Y), cut(0){}
friend point operator >> (const point &p1, const point &p2){
return point(p2.x - p1.x, p2.y - p1.y);
}
friend int operator ^ (const point &p1, const point &p2){
return p1.x * p2.y - p1.y * p2.x;
}
double norm(){
return sqrt(sqr(x) + sqr(y));
}
friend bool operator < (const point &p1, const point &p2){
return (p1.x < p2.x)||(p1.x == p2.x)&&(p1.y < p2.y);
}
friend bool operator > (const point &p1, const point &p2){
return (p1.x > p2.x)||(p1.x == p2.x)&&(p1.y > p2.y);
}
}pt[MAXN], p[MAXN], ans[MAXN]; int m0, minval, n, val, len;
double det; void GetPoints(point src[], point dest[], int n, int &m){
m = 0;
for (int i = 0; i < n; i++){
if (!src[i].cut){
dest[m++] = src[i];
}
}
} template <class T>
void swap(T &x, T &y){
T z = x;
x = y;
y = z;
} void BBS(point p[], int n){
for (int i = 0; i < n; i++){
for (int j = 0; j < i; j++){
if (p[i] < p[j])
swap(p[i], p[j]);
}
}
} void dfs(int x){
if (!(x + 1)){
int cur = 0, m, l = len;
double d = 0, v = val;
GetPoints(pt, p, n, m);
BBS(p, m);
for (int i = 0; i < m; i++){
v -= p[i].v;
l -= p[i].l;
}
while (1){
int tmp = - 1;
for (int i = 0; i < m; i++){
if (i != cur){
if (!(tmp + 1)||(((p[cur] >> p[i]) ^ (p[cur] >> p[tmp])) > 0))
tmp = i;
}
}
if (tmp + 1)
d += (p[cur] >> p[tmp]).norm();
if (!tmp||!(tmp + 1)) break;
cur = tmp;
}
if (d > l) return;
if ((v < minval)||(v == minval)&&(m < m0)){
minval = v, det = l - d, m0 = m;
for (int i = 0; i < n; i++)
ans[i] = pt[i];
}
}
else{
pt[x].cut = 0;
dfs(x - 1);
pt[x].cut = 1;
dfs(x - 1);
}
} int main(){
int irr = 0;
freopen("fin.c", "r", stdin);
while(scanf("%d", &n), n){
val = len = 0, irr++, det = 0;
m0 = 0x7f7f7f7f, minval = 0x7f7f7f7f;
for (int i = 0; i < n; i++){
scanf("%d%d%d%d", &pt[i].x, &pt[i].y, &pt[i].v, &pt[i].l);
val += pt[i].v, len += pt[i].l;
}
dfs(n - 1);
if (irr > 1) puts("");
printf("Forest %d\n", irr);
printf("Cut these trees:");
for (int i = 0; i < n; i++){
if (ans[i].cut)
printf(" %d", i + 1);
}
printf("\nExtra wood: %.2f\n", det);
}
}

POJ 1873 - The Fortified Forest 凸包 + 搜索 模板的更多相关文章

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

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

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

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

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

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

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

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

  5. POJ 1873 The Fortified Forest(枚举+凸包)

    Description Once upon a time, in a faraway land, there lived a king. This king owned a small collect ...

  6. ●POJ 1873 The Fortified Forest

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

  7. POJ 1873 The Fortified Forest

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

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

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

  9. Uva5211/POJ1873 The Fortified Forest 凸包

    LINK 题意:给出点集,每个点有个价值v和长度l,问把其中几个点取掉,用这几个点的长度能把剩下的点围住,要求剩下的点价值和最大,拿掉的点最少且剩余长度最长. 思路:1999WF中的水题.考虑到其点的 ...

随机推荐

  1. CC3000 SPI接口编程介绍

    CC3000 SPI 操作: CC3000的SPI 是基于五根线CLCK,CSn,IRQ,MISO,MOSI:通信模式如下图: CLCK:时钟频率0-26M从主机到从机,提供SPI接口时钟 CSn:低 ...

  2. App_Offline.htm 功能

    在ASP.NET 2.0 站点根目录下,只要存在 App_Offline.htm 文件,那么所有对.aspx的请求都将转向App_Offline.htm .而且浏览器的地址栏显示的是所请求的.aspx ...

  3. input:-webkit-autofill 导致chrome的输入框背景颜色变成黄色

    填写form表单时发现chrome的一个好坑啊! 当你之前有填写过表单,获取焦点时,input会有一个记录之前填写过的文本的下拉列表式的东东,就像这样:(抱歉丑了点,隐私问题打上了马赛克) 按理说,这 ...

  4. [转]centos 下 autoconf版本升级

    首先查看当前版本 #rpm -qf /usr/bin/autoconf autoconf-2.63-5.1.el6.noarch 卸载当前版本 rpm -e --nodeps autoconf-2.6 ...

  5. IOS开发-第三方SDWebImage下载网络图片的使用

    从网络上请求图片时,没有使用第三方的话,下载会很慢,而且堵塞线程,还要自己处理多线程问题,效果还非常不明显,使用了SDWebImage这个第三方类库之后,下载图片就变的容易多了. SDWebImage ...

  6. SVM之SMO最小序列

    转载自:JerryLead http://www.cnblogs.com/jerrylead/archive/2011/03/18/1988419.html 11 SMO优化算法(Sequential ...

  7. python---sys

    简介 sys模块用于提供对python解释器的相关操作 sys.argv 命令行参数List,第一个元素是程序本身路径 sys.modules 返回系统导入的模块字段,key是模块名,value是模块 ...

  8. 读CopyOnWriteArrayList有感

    除了加锁外,其实还有一种方式可以防止并发修改异常,这就是将读写分离技术(不是数据库上的). 先回顾一下一个常识: 1.JAVA中“=”操作只是将引用和某个对象关联,假如同时有一个线程将引用指向另外一个 ...

  9. 从头来之【图解针对虚拟机iOS开发环境搭建】

    1.下载Mac OSX10.9. 点击下载 2.下载VMware Workstation 10,点击下载,网页中包含序列号.安装VM. 3.VM10-MacOS补丁.用于创建苹果虚拟机. 安装VM就不 ...

  10. Semaphore tryAcquire release 正确的使用方法

    boolean permit = false; try { permit = semaphore.tryAcquire(1, TimeUnit.SECONDS); if (permit) { Syst ...