简单几何(凸包+枚举) POJ 1873 The Fortified Forest
题意:砍掉一些树,用它们做成篱笆把剩余的树围起来,问最小价值
分析:数据量不大,考虑状态压缩暴力枚举,求凸包以及计算凸包长度。虽说是水题,毕竟是final,自己状压的最大情况写错了,而且忘记特判凸包点数 <= 1的情况。
/************************************************
* Author :Running_Time
* Created Time :2015/11/3 星期二 16:10:17
* File Name :POJ_1873.cpp
************************************************/ #include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std; #define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const double EPS = 1e-10;
const double PI = acos (-1.0);
int dcmp(double x) { //三态函数,减少精度问题
if (fabs (x) < EPS) return 0;
else return x < 0 ? -1 : 1;
}
struct Point { //点的定义
double x, y, v, l;
Point () {}
Point (double x, double y, double v, double l) : x (x), y (y), v (v), l (l) {}
Point operator + (const Point &r) const { //向量加法
return Point (x + r.x, y + r.y, 0, 0);
}
Point operator - (const Point &r) const { //向量减法
return Point (x - r.x, y - r.y, 0, 0);
}
Point operator * (double p) const { //向量乘以标量
return Point (x * p, y * p, 0, 0);
}
Point operator / (double p) const { //向量除以标量
return Point (x / p, y / p, 0, 0);
}
bool operator < (const Point &r) const { //点的坐标排序
return x < r.x || (x == r.x && y < r.y);
}
bool operator == (const Point &r) const { //判断同一个点
return dcmp (x - r.x) == 0 && dcmp (y - r.y) == 0;
}
};
typedef Point Vector; //向量的定义
Point read_point(void) { //点的读入
double x, y, v, l;
scanf ("%lf%lf%lf%lf", &x, &y, &v, &l);
return Point (x, y, v, l);
}
double dot(Vector A, Vector B) { //向量点积
return A.x * B.x + A.y * B.y;
}
double cross(Vector A, Vector B) { //向量叉积
return A.x * B.y - A.y * B.x;
}
double length(Vector A) { //向量长度,点积
return sqrt (dot (A, A));
}
Point qs[33], ps[33];
/*
点集凸包,输入点的集合,返回凸包点的集合。
如果不希望在凸包的边上有输入点,把两个 <= 改成 <
*/
int convex_hull(Point *ps, int n) {
sort (ps, ps+n); //x - y排序
int k = 0;
for (int i=0; i<n; ++i) {
while (k > 1 && cross (qs[k-1] - qs[k-2], ps[i] - qs[k-1]) <= 0) k--;
qs[k++] = ps[i];
}
for (int i=n-2, t=k; i>=0; --i) {
while (k > t && cross (qs[k-1] - qs[k-2], ps[i] - qs[k-1]) <= 0) k--;
qs[k++] = ps[i];
}
return k-1;
} double cal_fence(Point *ps, int n) {
if (n <= 1) {
return 0;
}
n = convex_hull (ps, n);
double ret = 0;
for (int i=0; i<n-1; ++i) {
ret += length (qs[i+1] - qs[i]);
}
ret += length (qs[n-1] - qs[0]);
return ret;
} int main(void) {
int n, cas = 0;
while (scanf ("%d", &n) == 1) {
if (!n) break;
vector<Point> pps;
for (int i=0; i<n; ++i) {
pps.push_back (read_point ());
}
int S = 1 << n, sz = 100;
vector<int> ans, num;
int m = 0;
double sum = 0, val = 999999999, tval = 0, len = 0, ex = 0;
for (int i=0; i<S; ++i) {
num.clear (); tval = 0; sum = 0; m = 0;
for (int j=0; j<n; ++j) {
if (i & (1 << j)) { //cut
num.push_back (j + 1);
tval += pps[j].v; sum += pps[j].l;
}
else {
ps[m++] = pps[j];
}
}
len = cal_fence (ps, m);
if (sum < len) continue;
if (val > tval || (dcmp (val - tval) == 0 && sz > num.size ())) {
val = tval; sz = num.size ();
ans = num; ex = sum - len;
}
}
printf ("Forest %d\n", ++cas);
printf ("Cut these trees: ");
printf ("%d", ans[0]);
for (int i=1; i<ans.size (); ++i) {
printf (" %d", ans[i]);
}
puts ("");
printf ("Extra wood: %.2f\n\n", ex);
} //cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << " s.\n"; return 0;
}
简单几何(凸包+枚举) POJ 1873 The Fortified Forest的更多相关文章
- 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(枚举+凸包)
Description Once upon a time, in a faraway land, there lived a king. This king owned a small collect ...
- 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(凸包)题解
题意:二维平面有一堆点,每个点有价值v和删掉这个点能得到的长度l,问你删掉最少的价值能把剩余点围起来,价值一样求删掉的点最少 思路:n<=15,那么直接遍历2^15,判断每种情况.这里要优化一下 ...
- POJ 1873 The Fortified Forest
题意:是有n棵树,每棵的坐标,价值和长度已知,要砍掉若干根,用他们围住其他树,问损失价值最小的情况下又要长度足够围住其他树,砍掉哪些树.. 思路:先求要砍掉的哪些树,在求剩下的树求凸包,在判是否可行. ...
- 简单几何(凸包+多边形面积) POJ 3348 Cows
题目传送门 题意:求凸包 + (int)求面积 / 50 /************************************************ * Author :Running_Tim ...
- 简单几何(凸包) POJ 1696 Space Ant
题目传送门 题意:一个蚂蚁一直往左边走,问最多能走多少步,且输出路径 分析:就是凸包的变形题,凸包性质,所有点都能走.从左下角开始走,不停排序.有点纠结,自己的凸包不能AC.待理解透凸包再来写.. 好 ...
随机推荐
- 避免使用CSS表达式
http://www.cnblogs.com/chenxizhang/archive/2013/05/01/3053439.html 这一篇我来和大家讨论个原则:Avoid CSS Expressio ...
- CSS3实现二十多种基本图形
CSS3可以实现很多漂亮的图形,我收集了32种图形,在下面列出.直接用CSS3画出这些图形,要比贴图性能更好,体验更加,是一种非常好的网页美观方式. 这32种图形分别为圆形,椭圆形,三角形,倒三角形, ...
- [Effective JavaScript笔记]第1条:了解使用的js版本
1997年 正式成为国际标准,官方名称为ECMAScript. 1999年 定稿第3版ECMAScript标准(简称ES3),最广泛的js版本. 2009年 发布第5版即ES5,引入了一些新特性,标准 ...
- 计蒜客 X的平方根
X的平方根 设计函数int sqrt(int x),计算x的平方根. 格式: 输入一个数x,输出它的平方根.直到碰到结束符号为止. 千万注意:是int类型哦- 输入可以如下操作: while(cin& ...
- struts/Servlet,action转到jsp后,路径问题(struts2,jsp路径,action路径,action跳转,相对路径,绝对路径)
问题:使用struts2,如何处理action的路径?还有,在action转到的jsp中,如何写js,css,图 片的路径?(例如访问 http://localhost/project/listUse ...
- 2015安徽省赛 D.锐雯上单不给就送
题目描述 <英雄联盟>(简称LOL)是由美国Riot Games开发,腾讯游戏运营的英雄对战网游.<英雄联盟>除了即时战略.团队作战外,还拥有特色的英雄.自动匹配的战网平台,包 ...
- 最长回文子串O(n)算法
原文链接:英文版链接 首先,我们将字符串S中插入符号“#”转化成另一个字符串T. 比如:S = "abaaba",T = “#a#b#a#a#b#a#”. 为了找到最长回文字串,我 ...
- PHP - 获取和设置include_path .
PHP - 获取和设置include_path 分类: PHP 2011-02-16 13:19 2818人阅读 评论(1) ...
- Linux下 ntp 时间同步服务ntpd 出现 the NTP socket is in use, exiting 解决
[root@EPDDB log]# [root@EPDDB log]# ntpdate 10.154.8.200 6 Sep 09:35:09 ntpdate[30210]: the NTP sock ...
- Android Debugging
Debugging methods for Android Contents [hide] 1 Debuggers 1.1 Kernel and User co-debug with GDB on ...