poj1873 The Fortified Forest【枚举+凸包】
来源:互联网 发布:关于绘画的软件 编辑:程序博客网 时间:2024/05/29 10:57
题目链接:http://poj.org/problem?id=1873
题意:给你n棵树,让你从这n棵树中,看k棵树,把剩下的数围起来,砍每棵树的价值和每棵树的长度都告诉你,问你能否话最小的价值把剩下的书围起来,如果价值相同,则输出看的树最少的结果。最后需要输出的是,砍了哪几颗树,还剩下都少长度的木材
解析:由于n才15,于是可以考虑枚举砍的情况的所有子集,然后把剩下的树做凸包,然后不断更新答案即可
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>using namespace std;const int maxn = 1e5+100;const int inf = 0x7fffffff;const double eps = 1e-10;const double pi = acos(-1.0);struct point{ double x,y; double v,l; point() {} point(double _x,double _y,double _v,double _l) { x = _x; y = _y; v = _v; l = _l; } bool operator < (const point &b) const { if(y==b.y) return x<b.x; return y<b.y; } bool operator == (const point &b)const { return x==b.x && y==b.y; }}t1[maxn],a[maxn],ans[maxn];double x_mul(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((p2.x-p1.x)*(p2.x-p1.x)+(p2.y-p1.y)*(p2.y-p1.y));}bool cmp(point t1,point t2){ double tmp = x_mul(a[0],t1,t2); if(tmp==0) return dis(a[0],t1)<dis(a[0],t2); else return tmp>0;}double graham(int n){ if(n==0 || n==1) return 0; if (n == 2) return dis(a[0], a[1])*2; sort(a,a+n); sort(a+1,a+n,cmp); ans[0] = a[0]; ans[1] = a[1]; ans[2] = a[2]; int top = 2; for(int i=3;i<n;i++) { while(top>=2&&x_mul(ans[top-1],ans[top],a[i])<0) top--; ans[++top] = a[i]; } double res = 0; for(int i=0;i<top;i++) res += dis(ans[i],ans[i+1]); res += dis(ans[top],ans[0]); return res;}int main(void){ int n; int case_t = 0; while(~scanf("%d",&n)&&n) { if(case_t) puts(""); for(int i=0;i<n;i++) scanf("%lf %lf %lf %lf",&t1[i].x,&t1[i].y,&t1[i].v,&t1[i].l); double ans1 = inf; int ans = 0; double ans_len,cur = 0; for(int i=0;i<(1<<n);i++) { int cnt = 0; double v = 0,len = 0; for(int j=0;j<n;j++) { if(i&(1<<j)) { v += t1[j].v; len += t1[j].l; } else a[cnt++] = t1[j]; } if(v>ans1) continue; double tmp = graham(cnt); if(len>=tmp) { if(v<ans1 || (v==ans1 && cur>cnt)) { ans1 = v; ans = i; cur = cnt; ans_len = len-tmp; } } } printf("Forest %d\n",++case_t); printf("Cut these trees:"); for(int i=0;i<n;i++) { if(ans&(1<<i)) printf(" %d",i+1); } printf("\nExtra wood: %.2f\n",ans_len); } return 0;}
0 0
- poj1873 The Fortified Forest【枚举+凸包】
- poj1873 The Fortified Forest 凸包+枚举 水题
- The Fortified Forest(poj1873位运算枚举+凸包)
- poj1873 The Fortified Forest(位运算枚举+凸包)
- 【POJ1873】The Fortified Forest-凸包+枚举方案
- [POJ1873]The Fortified Forest(dfs+凸包)
- 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 凸包+枚举组合
- (beginer) 凸包+枚举 UVA 811 - The Fortified Forest
- POJ 1873 The Fortified Forest(凸包+枚举)
- POJ 1873 The Fortified Forest(枚举+凸包+剪枝)
- POJ 1873 The Fortified Forest(凸包+DFS枚举)
- POJ 1873 The Fortified Forest(二进制枚举+凸包)
- poj 1873The Fortified Forest (凸包 二进制枚举)
- IMWEB训练营作业TODOLIST
- 什么是java序列化,如何实现java序列化?
- 动态代理实现原理
- MobaXterm 登录出现 Network error :Connection timed out
- mysql 表分区
- poj1873 The Fortified Forest【枚举+凸包】
- Log4j配置学习文档之一 log4j配置
- jsp接收后台全数字的字符串注意事项!
- 9.Unity3D商业游戏源码研究-变身吧主公-PanelBase
- NameError: global name 'log_runtime' is not defined in scapy
- SparkSQL之JDBC
- 预置GMS包后,关机闹钟失效
- Android的事件分发机制(一):dispatchTouchEvent
- 采用Replica Sets + Sharding方式搭建MongoDB高可用集群