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. SQLMAP注入常见用法

    1.检查注入点 sqlmap -u http://www.com.tw/star_photo.php?artist_id=11 2.列数据库信息当前用户和数据库 sqlmap -u http://ww ...

  2. Linux中文件I/O函数

    一.lseek函数 每个打开文件都有一个与其相关联的“当前文件偏移量”.它通常是一个非负整数,用以度量从文件开始处 计算的字节数.通常,读.写操作都从当前文件偏移量处开始,并使偏移量增加所读写的字节数 ...

  3. chromium之message_pump_win之一

    写了22篇博文,终于到这里了———— MessagePumpWin!!! MessagePumpWin这个类还是挺复杂的,可以分成好几部分.接下来分块分析 从介绍看,MessagePumpWin 是M ...

  4. 简易坦克大战python版

      #! /usr/bin/env python # -*- coding:utf8 -*- ''' *author:wasua *purpose:学习python语言,其中的类以及pygame应用 ...

  5. jquery仿移动端支付宝键盘

    最近做项目时碰到一个需求,就是在移动端支付页面点击支付按钮弹出一个支付键盘,类似于支付宝的那种.由于项目只是单纯的手机网站,而并非app,所以这个功能得由前端来实现.话不多说,先上图看看效果. 尼玛, ...

  6. SSM+poi导入和导出

    最原始数据 导入成功后 下载数据 下载后的数据显示 数据变成16条 点击导出可选择 导了两次  看数据变化 数据库字段在下面地址给出 首先贴出Dao层 List<User> findAll ...

  7. XSS攻击 && CSRF攻击 基础理解

    一个网站,不管多么的帅气,多么的风骚,如果你不安全,那始终都是一个弟弟啊~ 今天又看了下XSS和CSRF攻击的文章,我也想发点什么普及下大家的安全意识,毕竟作为一名拥有伟大梦想的程序员,基本的安全意识 ...

  8. UEditor代码实现高亮显示

    在公司开发一个论坛系统,由于用的是UEditor(百度编辑器),单独使用的话,里面的代码不会高亮,网上找了很多,最后决定使用 highlight.js 实现代码高亮显示.效果如下: 这个是我修改其他的 ...

  9. 『Python基础-1 』 编程语言Python的基础背景知识

    #『Python基础-1 』 编程语言Python的基础背景知识 目录: 1.编程语言 1.1 什么是编程语言 1.2 编程语言的种类 1.3 常见的编程语言 1.4 编译型语言和解释型语言的对比 2 ...

  10. python闭包的概念及使用

    闭包:在函数里定义了另外一个函数(函数嵌套),内函数里运用了外函数的变量,外函数返回内函数的函数引用(函数名). nonlocal 的使用:闭包内部函数可直接调用外部函数的变量,如果修改需要使用non ...