题目链接:UVA 811

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\le n\le 15\), the number of trees in the forest. The trees are identied by consecutive integers \(1\) to \(n\). Each of the subsequent lines contains \(4\) integers \(x_i,y_i,v_i,l_i\) that describe a single tree. \((x_i,y_i)\) is the position of the tree in the plane, \(v_i\) is its value, and \(l_i\) 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.

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

Solution

题意

有 \(n\) 颗树,每颗树的坐标为 \(x, y\) ,价值为 \(v_i\) 长度为 \(l_i\)。现在要用篱笆将其中一些树围起来,但篱笆制作来源于这些树,即要求砍掉的树能构成篱笆的长度 \(>=\) 剩余树的凸包周长。现在要使得砍掉树的价值之和最小,问需要砍掉哪些树(如果有价值相同的解,就输出砍的树最少的解)。

题解

凸包周长 状态压缩枚举

树的规模比较小,用二进制枚举所有情况即可。

Code

#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
const double eps = 1e-10;
const int inf = 0x3f3f3f3f;
const int maxn = 30; int n;
struct Point {
double x, y;
int v, l;
int id;
Point() {}
Point(double a, double b) : x(a), y(b) {}
bool operator<(const Point &b) const {
if (x < b.x) return 1;
if (x > b.x) return 0;
return y < b.y;
}
bool operator==(const Point &b) const {
if (x == b.x && y == b.y) return 1;
return 0;
}
Point operator-(const Point &b) {
return Point(x - b.x, y - b.y);
}
} p[maxn], stk[maxn], tmp[maxn];
typedef Point Vec; int sgn(double x) {
if (fabs(x) <= eps)
return 0;
return x > 0 ? 1 : -1;
} double dist(Point a, Point b) {
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
} double cross(Vec a, Vec b) {
return a.x * b.y - a.y * b.x;
} int Andrew(int l) {
sort(tmp + 1, tmp + 1 + l);
int len = 0;
for (int i = 1; i <= l; ++i) {
if(i > 1 && tmp[i] == tmp[i - 1]) continue; // 会有重复的点, WA了好几次
while (len > 1 && sgn(cross(stk[len] - stk[len - 1], tmp[i] - stk[len - 1])) == -1) {
len--;
}
stk[++len] = tmp[i];
}
int k = len;
for (int i = l - 1; i >= 1; --i) {
if(i > 1 && tmp[i] == tmp[i - 1]) continue;
while (len > k && sgn(cross(stk[len] - stk[len - 1], tmp[i] - stk[len - 1])) == -1) {
len--;
}
stk[++len] = tmp[i];
}
return len;
} void solve(int &min_val, int &cur_num, double &re_len, int &ans) {
int size = 1 << n;
for(int bit = 0; bit < size; ++bit) {
int t = 0, cur_val = 0;
double cur_len = 0 ;
for(int i = 0; i < n; ++i) {
if(bit & (1 << i)) {
cur_len += p[i].l;
cur_val += p[i].v;
} else {
tmp[++t] = p[i];
}
}
if(cur_val > min_val) continue;
int cnt = Andrew(t);
double c = 0;
for(int i = 1; i < cnt; ++i) {
c += dist(stk[i], stk[i + 1]);
} if(cur_len >= c) {
if(cur_val < min_val || (cur_val == min_val && n - t < cur_num)) {
min_val = cur_val;
cur_num = n - t;
re_len = cur_len - c;
ans = bit;
}
}
}
}
int main() {
int kase = 0;
while(scanf("%d",&n) && n) {
if(kase) printf("\n");
for(int i = 0; i < n; ++i) {
scanf("%lf%lf%d%d", &p[i].x, &p[i].y, &p[i].v, &p[i].l);
}
int min_val = inf;
int cur_num = inf;
double re_len = 0;
int ans = 0;
solve(min_val, cur_num, re_len, ans);
printf("Forest %d\n", ++kase);
printf("Cut these trees:");
for(int i = 0 ; i < n; ++i) {
if(ans & (1 << i)) {
printf(" %d", i + 1);
}
}
printf("\nExtra wood: %.2lf\n", re_len);
}
return 0;
}

POJ 1873 UVA 811 The Fortified Forest (凸包 + 状态压缩枚举)的更多相关文章

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

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

  2. UVA 1508 - Equipment 状态压缩 枚举子集 dfs

    UVA 1508 - Equipment 状态压缩 枚举子集 dfs ACM 题目地址:option=com_onlinejudge&Itemid=8&category=457& ...

  3. 状态压缩+枚举 POJ 3279 Fliptile

    题目传送门 /* 题意:问最少翻转几次使得棋子都变白,输出翻转的位置 状态压缩+枚举:和之前UVA_11464差不多,枚举第一行,可以从上一行的状态知道当前是否必须翻转 */ #include < ...

  4. 状态压缩+枚举 UVA 11464 Even Parity

    题目传送门 /* 题意:求最少改变多少个0成1,使得每一个元素四周的和为偶数 状态压缩+枚举:枚举第一行的所有可能(1<<n),下一行完全能够由上一行递推出来,b数组保存该位置需要填什么 ...

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

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

  6. Uva5211/POJ1873 The Fortified Forest 凸包

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

  7. POJ 1873 - The Fortified Forest 凸包 + 搜索 模板

    通过这道题发现了原来写凸包的一些不注意之处和一些错误..有些错误很要命.. 这题 N = 15 1 << 15 = 32768 直接枚举完全可行 卡在异常情况判断上很久,只有 顶点数 &g ...

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

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

  9. POJ 3311 Hie with the Pie(DP状态压缩+最短路径)

    题目链接:http://poj.org/problem?id=3311 题目大意:一个送披萨的,每次送外卖不超过10个地方,给你这些地方之间的时间,求送完外卖回到店里的总时间最小. Sample In ...

随机推荐

  1. CPU、内存、磁盘三者的关系

    参考:https://blog.csdn.net/weini1111/article/details/70849332 cpu是大脑,计算数据用的. 内存是草稿纸,开着电脑一直都在用里边的数据,如果断 ...

  2. 查看静态库(.lib)和动态库(.dll)的导出函数的信息 error LNK2001: 无法解析的外部符号 _Delete

    转自VC错误:http://www.vcerror.com/?p=1381 在window下查看动态库的导出函数可以用vs自带的Dependenc工具: 查看静态库的信息要用命令行来实现: 首先运行V ...

  3. upc组队赛5 Ground Defense【枚举】

    Ground Defense 题目描述 You are a denizen of Linetopia, whose n major cities happen to be equally spaced ...

  4. haproxy附加

    1.安装haproxy yum -y install haproxy 2.编写文件  vim /etc/haproxy/haproxy.cfg

  5. UVA1152_4 Values whose Sum is 0

    中途相遇法,这题目总结后我感觉和第一篇博客很像,他们都取了中间,也许这就是二分的魅力吧 这题题意就是从ABCD四个集合中选四个元素使他们的和为0 题意很简单,但是实现起来很容易超时,不能一个一个枚举 ...

  6. 基于MFC的Media Player播放器的制作介绍

    |   版权声明:本文为博主原创文章,未经博主允许不得转载. 因为这次多媒体课程设计做一个基于MFC的播放器,因为本人实力太菜,需要播放音乐或视频文件时候,自己写不出解码 函数,所以准备使用第三方多媒 ...

  7. [已解决]报错: No module named pip

    cmd中敲命令: python -m ensurepip 更新升级pip命令: python -m pip install --upgrade pip

  8. C# winform 将其他程序嵌入Form窗体

    嵌入类 public class ExeImpaction { public void FrmClosing() { try { if (!process.HasExited) process.Kil ...

  9. 39-python基础-python3-字典常用方法-get()

    在访问一个键的值之前,检查该键是否存在于字典中,这很麻烦. 好在,字典有一个 get()方法,它有两个参数:要取得其值的键,以及如果该键不存在时,返回的备用值. dict.get(键,默认值) 实例- ...

  10. springboot多数据库及分布式事务配置

    1.导入相应的jar包依赖 <!-- 集成mybatis --> <dependency> <groupId>org.mybatis.spring.boot< ...