[POJ1873]The Fortified Forest(dfs+凸包)
来源:互联网 发布:3亿个精准客户数据库 编辑:程序博客网 时间:2024/05/16 09:27
题目描述
传送门
题意:给出一些树(坐标,价值,长度),要求砍下一些树做成篱笆,将剩下的树围起来。求砍下的树最小价值的方案,如果有多种方案,输出砍的树的个数最少的。
题解
n只有15用dfs就可以了
dfs之后求凸包的周长,然后再判断价值什么的
代码
#include<algorithm>#include<iostream>#include<cstring>#include<cstdio>#include<cmath>using namespace std;#define inf 1000000000#define N 20const double eps=1e-9;int dcmp(double x){ if (x<=eps&&x>=-eps) return 0; return (x>0)?1:-1;}struct Vector{ double x,y; Vector(double X=0,double Y=0) { x=X,y=Y; } bool operator < (const Vector &a) const { return x<a.x||(x==a.x&&y<a.y); }};typedef Vector Point;Vector operator - (Vector A,Vector B) {return Vector(A.x-B.x,A.y-B.y);}int T,n,Min,cnt,top;double ew;int v[N],wh[N],tr[N];double x[N],y[N],l[N];Point p[N],stack[N];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 Len(Vector A){ return sqrt(Dot(A,A));}void Graham(){ sort(p+1,p+cnt+1); top=0; for (int i=1;i<=cnt;++i) { while (top>1&&dcmp(Cross(stack[top]-stack[top-1],p[i]-stack[top-1]))<=0) --top; stack[++top]=p[i]; } int k=top; for (int i=cnt-1;i>=1;--i) { while (top>k&&dcmp(Cross(stack[top]-stack[top-1],p[i]-stack[top-1]))<=0) --top; stack[++top]=p[i]; } if (cnt>1) --top;}void check(){ double len=0,ans=0;int val=0; cnt=0; for (int i=1;i<=n;++i) if (wh[i]) p[++cnt]=Point(x[i],y[i]); else len+=l[i],val+=v[i]; Graham(); for (int i=1;i<=top;++i) ans+=Len(stack[i%top+1]-stack[(i+1)%top+1]); if (dcmp(len-ans)>=0) { if (val<Min) { Min=val;ew=len-ans;tr[0]=0; for (int i=1;i<=n;++i) if (!wh[i]) tr[++tr[0]]=i; } else if (val==Min&&cnt<tr[0]) { ew=len-ans;tr[0]=0; for (int i=1;i<=n;++i) if (!wh[i]) tr[++tr[0]]=i; } }}void dfs(int dep){ if (dep==n+1) { check(); return; } for (int i=1;i>=0;--i) { wh[dep]=i; dfs(dep+1); }}int main(){ while (~scanf("%d",&n)) { if (!n) break; for (int i=1;i<=n;++i) scanf("%lf%lf%d%lf",&x[i],&y[i],&v[i],&l[i]); Min=inf;tr[0]=0;dfs(1); printf("Forest %d\n",++T); printf("Cut these trees:"); for (int i=1;i<=tr[0];++i) printf(" %d",tr[i]);puts(""); printf("Extra wood: %.2lf\n",ew); puts(""); }}
0 0
- [POJ1873]The Fortified Forest(dfs+凸包)
- poj1873 The Fortified Forest【枚举+凸包】
- poj1873 The Fortified Forest 凸包+枚举 水题
- The Fortified Forest(poj1873位运算枚举+凸包)
- poj1873 The Fortified Forest(位运算枚举+凸包)
- 【POJ1873】The Fortified Forest-凸包+枚举方案
- POJ 1873 The Fortified Forest(凸包+DFS枚举)
- POJ 1873 The Fortified Forest(凸包+枚举)
- poj 1873 The Fortified Forest(凸包)
- UVA 811 - The Fortified Forest(凸包)
- uva 811 - The Fortified Forest(暴力+凸包)
- POJ 1873 The Fortified Forest(枚举+凸包+剪枝)
- POJ 1873 The Fortified Forest(二进制枚举+凸包)
- poj 1873 The Fortified Forest (凸包)
- POJ 1873 The Fortified Forest(凸包)
- poj 1873 The Fortified Forest(凸包)
- POJ 1873 The Fortified Forest 凸包+枚举
- poj 1873 The Fortified Forest(凸包+枚举)
- (HDU 5922)Minimum’s Revenge 思维水题 <2016CCPC东北地区大学生程序设计竞赛 - 重现赛 >
- 子集生成算法
- Toolbar的常规设置(包括标题样式,菜单样式等)
- 数字加减多线程
- Rsync使用非ssh默认端口从远程服务器同步文件到本地
- [POJ1873]The Fortified Forest(dfs+凸包)
- Go语言:前后端对接
- 用DP解决最长子序列问题
- bat定时检测系统服务是否开启
- HDU 3315 My Brute(KM算法)
- STM32F4-IAP学习笔记(二)
- Mayan游戏
- BZOJ4644 经典傻逼题
- 那些年,被问过的Java面试题