poj1873(二进制枚举+求凸包周长)
题目链接:https://vjudge.net/problem/POJ-1873
题意:n个点(2<=n<=15),给出n个点的坐标(x,y)、价值v、做篱笆时的长度l,求选择哪些点来做篱笆围住另一些点,使得选出的这些点的价值和最小,如果价值和相等要求个数最小。
思路:
看来这是WF的签到题吧。数据很小,直接二进制枚举 (1<<n),然后对未选出的点求凸包的周长,仅当选出点的长度l的和>=凸包周长时才更新答案。
AC code:
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std; const int maxn=;
const int inf=0x3f3f3f3f; struct Point{
int x,y;
Point():x(),y(){}
Point(int x,int y):x(x),y(y){}
}; int n,cas,val,num,res[maxn],v[maxn],l[maxn],vis[maxn];
double ext;
int top,stack[maxn];
Point pt[maxn],list[maxn]; int cross(Point p0,Point p1,Point p2){
return (p1.x-p0.x)*(p2.y-p0.y)-(p2.x-p0.x)*(p1.y-p0.y);
} double dis(Point p1,Point p2){
return sqrt((double)(p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));
} bool cmp(Point p1,Point p2){
int tmp=cross(list[],p1,p2);
if(tmp>) return true;
else if(tmp==&&dis(list[],p1)<dis(list[],p2)) return true;
else return false;
} void init(int m){
Point p0=list[];
int k=;
for(int i=;i<m;++i){
if((p0.y>list[i].y)||((p0.y==list[i].y)&&(p0.x>list[i].x))){
p0=list[i];
k=i;
}
}
list[k]=list[];
list[]=p0;
sort(list+,list+m,cmp);
} double graham(int m){
if(m==){
top=;
stack[]=;
}
else{
top=;
stack[]=;
stack[]=;
for(int i=;i<m;++i){
while(top>&&cross(list[stack[top-]],list[stack[top]],list[i])<=) --top;
stack[++top]=i;
}
}
double ret=;
for(int i=;i<top;++i)
ret+=dis(list[stack[i]],list[stack[i+]]);
ret+=dis(list[stack[top]],list[stack[]]);
return ret;
} int main(){
while(scanf("%d",&n),n){
val=num=inf;
for(int i=;i<n;++i)
scanf("%d%d%d%d",&pt[i].x,&pt[i].y,&v[i],&l[i]);
for(int i=;i<(<<n)-;++i){
int t1=,t2=,cnt=;
for(int j=;j<n;++j){
if((i>>j)&){
t1+=v[j],t2+=l[j];
}
else{
list[cnt++]=pt[j];
}
}
init(cnt);
int cnt2=n-cnt;
if((t1<val)||((t1==val)&&(cnt2<num))){
double tmp=graham(cnt);
if(tmp>t2) continue;
val=t1,num=cnt2,ext=t2-tmp;
int t=;
for(int j=;j<n;++j){
if((i>>j)&)
res[t++]=j;
}
}
}
printf("Forest %d\nCut these trees:",++cas);
for(int i=;i<num;++i)
printf(" %d",res[i]+);
printf("\nExtra wood: %.2f\n\n",ext);
}
return ;
}
poj1873(二进制枚举+求凸包周长)的更多相关文章
- The Fortified Forest - POJ 1873(状态枚举+求凸包周长)
题目大意:有个国王他有一片森林,现在他想从这个森林里面砍伐一些树木做成篱笆把剩下的树木围起来,已知每个树都有不同的价值还有高度,求出来砍掉那些树可以做成篱笆把剩余的树都围起来,要使砍伐的树木的价值最小 ...
- HDU 1392 凸包模板题,求凸包周长
1.HDU 1392 Surround the Trees 2.题意:就是求凸包周长 3.总结:第一次做计算几何,没办法,还是看了大牛的博客 #include<iostream> #inc ...
- poj 1113:Wall(计算几何,求凸包周长)
Wall Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28462 Accepted: 9498 Description ...
- Wall---hdu1348(求凸包周长 模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1348 求凸包周长+2*PI*L: #include <stdio.h> #include ...
- POJ 1113 Wall(Graham求凸包周长)
题目链接 题意 : 求凸包周长+一个完整的圆周长. 因为走一圈,经过拐点时,所形成的扇形的内角和是360度,故一个完整的圆. 思路 : 求出凸包来,然后加上圆的周长 #include <stdi ...
- hdu 1392:Surround the Trees(计算几何,求凸包周长)
Surround the Trees Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1348:Wall(计算几何,求凸包周长)
Wall Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- HDU 1392 Surround the Trees (Graham求凸包周长)
题目链接 题意 : 让你找出最小的凸包周长 . 思路 : 用Graham求出凸包,然后对每条边求长即可. Graham详解 #include <stdio.h> #include < ...
- poj1113Wall 求凸包周长 Graham扫描法
#include<iostream> #include<algorithm> #include<cmath> using namespace std; typede ...
随机推荐
- pyzabbix
pyzabbix
- 递归回溯生成和解决数独问题c/c++
数独 程序地址https://github.com/papicheng/blog/tree/master/%E6%95%B0%E7%8B%AC 一.游戏规则介绍: 数独是源自18世纪瑞士的一种数学游戏 ...
- python 最小二乘 leastsq 函数实现
代码修改自 http://www.cnblogs.com/NanShan2016/p/5493429.html 网上百度了一下,主要是两个例子,一个利用了多项式函数,一个就是这个.有些细节没看懂,主要 ...
- 将List<E>内对象按照某个字段排序
主要用到java.util的Collections类 Collections.sort(list); 其中,E必须实现Comparable<E>接口
- Ubuntu系统下安装完成tomcat进入管理页面
首先先启动tomcat cd /usr/local/tomcat8. ./bin/startup.sh 然后再打开浏览器 在地址栏中输入 http:/localhost:
- LeetCode 209. 长度最小的子数组(Minimum Size Subarray Sum)
题目描述 给定一个含有 n 个正整数的数组和一个正整数 s ,找出该数组中满足其和 ≥ s 的长度最小的连续子数组.如果不存在符合条件的连续子数组,返回 0. 示例: 输入: s = 7, nums ...
- 程序中的.htaccess文件是做什么的
程序中的.htaccess文件是做什么的 一.总结 一句话总结: htaccess=ht(Hypertext)+access=超文本入口 覆盖Apache服务器的默认配置 .htaccess(超文本访 ...
- Java 中的JSON 字符串
类库选择 Java中并没有内置JSON的解析,因此使用JSON需要借助第三方类库. 下面是几个常用的 JSON 解析类库: Gson: 谷歌开发的 JSON 库,功能十分全面. FastJson: 阿 ...
- ES6展开运算符数组合并,函数传参
定义: .展开运算符允许一个表达式在某处展开. 使用场景 1.展开函数在多个参数的地方使用 .意指用于函数传参 2.多个元素的地方使用,意指用于数组字面量 3.多个边框的地方使用,意指用于解构赋值 函 ...
- pytorch常用损失函数
损失函数的基本用法: criterion = LossCriterion() #构造函数有自己的参数 loss = criterion(x, y) #调用标准时也有参数 得到的loss结果已经对min ...