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 ...
随机推荐
- [luogu] zpl的数学题1
https://www.luogu.org/problemnew/show/U16887 $f[1] + f[2] + f[3] + .... + f[n] = f[n + 2] - 1$ 矩阵快速幂 ...
- Ubuntu 14.04 indigo 安装 cartographer 1.0.0
安装依赖(cmake 版本为2.8,我的是自带的)sudo apt-get updatesudo apt-get install -y g++ git google-mock libboost-all ...
- codeforces#1120C. Compress String(dp+后缀自动机)
题目链接: https://codeforces.com/contest/1120/problem/C 题意: 从前往后压缩一段字符串 有两种操作: 1.对于单个字符,压缩它花费$a$ 2.对于末尾一 ...
- lucene正向索引(续)——一个文档的所有filed+value都在fdt文件中!!!
4.1.3. 域(Field)的数据信息(.fdt,.fdx) 域数据文件(fdt): 真正保存存储域(stored field)信息的是fdt文件 在一个段(segment)中总共有segment ...
- Buuctf pwn1 详细wp
目录 程序基本信息 程序溢出点 确定返回地址 编写exp脚本 成功getshell 程序基本信息 我们可以看到这是一个64程序,没有保护开启. 程序溢出点 gets函数可以读取无限字符,存在栈溢出. ...
- OpenResty之ngx.var.VARIABLE
1. ngx.var.VARIABLE syntax: ngx.var.VAR_NAME context: set_by_lua*, rewrite_by_lua*, access_by_lua*, ...
- php Class 'ZipArchive' not found怎么解决?
情况1: 服务器php zip模块没有安装 情况2: Php.ini 中Php zlip扩展没有开 文章来源:外星人来地球 欢迎关注,有问题一起学习欢迎留言.评论
- Locust - A modern load testing framework https://locust.io/
Locust - A modern load testing frameworkhttps://locust.io/
- vue - 设置全局html背景
需求 有时候有些组件需要全局设置body背景,有些不需要在组件中设置就行了 解决思路 1. 全局设置可以是html,body,这里大家可以试一下,这两个只要其中一个设置了background,另一个的 ...
- keras启用tensorboard
在callback函数中添加tensorboard,启用tensorboard. # TensorBoard callback tensorboard_cb = K.callbacks.TensorB ...