poj 1873 The Fortified Forest(凸包)
2015-08-11 23:47
465 查看
The Fortified Forest
Description
Once upon a time, in a faraway land, there lived a king. This king owned a small collection of rare and valuable trees, which had been gathered by his ancestors on their travels. To protect his trees from thieves, the king ordered that a high fence be built
around them. His wizard was put in charge of the operation.
Alas, the wizard quickly noticed that the only suitable material available to build the fence was the wood from the trees themselves. In other words, it was necessary to cut down some trees in order to build a fence around the remaining trees. Of course, to
prevent his head from being chopped off, the wizard wanted to minimize the value of the trees that had to be cut. The wizard went to his tower and stayed there until he had found the best possible solution to the problem. The fence was then built and everyone
lived happily ever after.
You are to write a program that solves the problem the wizard faced.
Input
The input contains several test cases, each of which describes a hypothetical forest. Each test case begins with a line containing a single integer n, 2 <= n <= 15, the number of trees in the forest. The trees are identified by consecutive integers 1 to n.
Each of the subsequent n lines contains 4 integers xi, yi, vi, li that describe a single tree. (xi, yi) is the position of the tree in the plane, vi is its value, and li is the length of fence that can be built using the wood of the tree. vi and li are between
0 and 10,000.
The input ends with an empty test case (n = 0).
Output
For each test case, compute a subset of the trees such that, using the wood from that subset, the remaining trees can be enclosed in a single fence. Find the subset with minimum value. If more than one such minimum-value subset exists, choose one with the smallest
number of trees. For simplicity, regard the trees as having zero diameter.
Display, as shown below, the test case numbers (1, 2, ...), the identity of each tree to be cut, and the length of the excess fencing (accurate to two fractional digits).
Display a blank line between test cases.
Sample Input
Sample Output
Source
题意:平面上有n棵树,现在要砍掉其中的一部分来做成篱笆将剩下的树包围起来,现在给出每棵树的坐标、价值和可以制造篱笆的长度,
求砍掉最少价值的树,将剩下的树包围起来,当两种方式的价值相同时,取砍掉树更少的方式。
题解:直接二进制暴力枚举砍掉的树,剩下的树求出凸包,再求周长,可行的话更新ans。
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 5737 | Accepted: 1636 |
Once upon a time, in a faraway land, there lived a king. This king owned a small collection of rare and valuable trees, which had been gathered by his ancestors on their travels. To protect his trees from thieves, the king ordered that a high fence be built
around them. His wizard was put in charge of the operation.
Alas, the wizard quickly noticed that the only suitable material available to build the fence was the wood from the trees themselves. In other words, it was necessary to cut down some trees in order to build a fence around the remaining trees. Of course, to
prevent his head from being chopped off, the wizard wanted to minimize the value of the trees that had to be cut. The wizard went to his tower and stayed there until he had found the best possible solution to the problem. The fence was then built and everyone
lived happily ever after.
You are to write a program that solves the problem the wizard faced.
Input
The input contains several test cases, each of which describes a hypothetical forest. Each test case begins with a line containing a single integer n, 2 <= n <= 15, the number of trees in the forest. The trees are identified by consecutive integers 1 to n.
Each of the subsequent n lines contains 4 integers xi, yi, vi, li that describe a single tree. (xi, yi) is the position of the tree in the plane, vi is its value, and li is the length of fence that can be built using the wood of the tree. vi and li are between
0 and 10,000.
The input ends with an empty test case (n = 0).
Output
For each test case, compute a subset of the trees such that, using the wood from that subset, the remaining trees can be enclosed in a single fence. Find the subset with minimum value. If more than one such minimum-value subset exists, choose one with the smallest
number of trees. For simplicity, regard the trees as having zero diameter.
Display, as shown below, the test case numbers (1, 2, ...), the identity of each tree to be cut, and the length of the excess fencing (accurate to two fractional digits).
Display a blank line between test cases.
Sample Input
6 0 0 8 3 1 4 3 2 2 1 7 1 4 1 2 3 3 5 4 6 2 3 9 8 3 3 0 10 2 5 5 20 25 7 -3 30 32 0
Sample Output
Forest 1 Cut these trees: 2 4 5 Extra wood: 3.16 Forest 2 Cut these trees: 2 Extra wood: 15.00
Source
题意:平面上有n棵树,现在要砍掉其中的一部分来做成篱笆将剩下的树包围起来,现在给出每棵树的坐标、价值和可以制造篱笆的长度,
求砍掉最少价值的树,将剩下的树包围起来,当两种方式的价值相同时,取砍掉树更少的方式。
题解:直接二进制暴力枚举砍掉的树,剩下的树求出凸包,再求周长,可行的话更新ans。
#include<cstring> #include<iostream> #include<cstdio> #include<algorithm> #include<cmath> #include<vector> #define PI 3.1415926 #define INF 0x3f3f3f3f const double eps=1e-12; #define _sign(x) ((x)>eps?1:((x)<-eps?2:0)) using namespace std; const int MAXN = 30; struct Point { double x,y; double l; int v; Point() {} Point(double _x,double _y) { x = _x; y = _y; } Point operator -(const Point &b)const { return Point(x - b.x,y - b.y); } //叉积 double operator ^(const Point &b)const { return x*b.y - y*b.x; } //点积 double operator *(const Point &b)const { return x*b.x + y*b.y; } //绕原点旋转角度B(弧度值),后x,y的变化 void transXY(double B) { double tx = x,ty = y; x = tx*cos(B) - ty*sin(B); y = tx*sin(B) + ty*cos(B); } }; Point L[MAXN],P[MAXN]; int Stack[MAXN],top; int n; double dist(Point a,Point b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } int sgn(double x) { if(fabs(x)<eps)return 0; if(x<0)return -1; return 1; } //相对于L[0]的极角排序 bool _cmp(Point p1,Point p2) { double tmp = (p1-L[0])^(p2-L[0]); if(sgn(tmp) > 0)return true; else if(sgn(tmp) == 0 && sgn(dist(p1,L[0]) - dist(p2,L[0])) <= 0) return true; else return false; } void Graham(int m) { if(m<=1)return; Point p0; int k = 0; p0 = L[0]; //找最下边的一个点 for(int i = 1; i < m; i++) { if( (p0.y > L[i].y) || (p0.y == L[i].y && p0.x > L[i].x) ) { p0 = L[i]; k = i; } } swap(L[k],L[0]); sort(L+1,L+m,_cmp); if(m == 1) { top = 1; Stack[0] = 0; return; } if(m == 2) { top = 2; Stack[0] = 0; Stack[1] = 1; return ; } Stack[0] = 0; Stack[1] = 1; top = 2; for(int i = 2; i < m; i++) { while(top > 1 && sgn((L[Stack[top-1]]-L[Stack[top-2]])^(L[i]-L[Stack[top-2]])) <= 0) top--; Stack[top++] = i; } } int main() { //freopen("test.in","r",stdin); int ca=1; while(cin>>n&&n) { for(int i=0; i<n; i++) { scanf("%lf%lf%d%lf",&P[i].x,&P[i].y,&P[i].v,&P[i].l); } int m=0; int ans=INF,id=0; int num=0; double ss=99999999.0; for(int i=0; i<(1<<n); i++) { double ls=0; int sum=0; m=0; for(int j=0; j<n; j++) { if(i&(1<<j)) { ///选到的树 sum+=P[j].v; ls+=P[j].l; } else L[m++]=P[j];///剩下的树 } if(sum>ans)continue; Graham(m); double s=0; for(int j=0; j<top; j++) { ///求凸包周长 int u=Stack[j],v=Stack[(j+1)%top]; s+=dist(L[u],L[v]); } if(m<=1)s=0; if(ls>=s) { if(ans>sum) { ans=sum; num=n-m; id=i; ss=ls-s; } else if(ans==sum) { if(num>n-m) { ss=ls-s; id=i; num=n-m; } } } } if(ca!=1)cout<<endl; printf("Forest %d\n",ca++); printf("Cut these trees:"); for(int i=0; i<n; i++) { if(id&(1<<i))printf(" %d",i+1); } printf("\nExtra wood: %.2f\n",ss); } return 0; }
相关文章推荐
- android studio 新建module
- 2015-08-11 [豌豆荚]--研发--1面
- java基础--多线程
- hdu 3309 Roll The Cube(bfs)
- 如果牛顿是程序员,那么?
- AC自动机模板
- 在活动中使用menu
- POJ 3468 Simple Problem with Integers
- ios单元测试之GHUnit
- Gevent 资料整理
- 前端面经系列(二)
- Gray code(hdu5375+异或二进制的规律)
- 单点登录(跨域)
- light oj 1066- Gathering Food (bfs)
- [问与答]怎样在 Android Stuido中删除一个project
- Cookie深度解析
- 黑马程序员——面向对象(上)— 类、对象、封装、继承、构造方法
- [问与答]怎样在 Android Stuido中删除一个project
- CodeForces 344
- ios的单元测试OCUnit以及更新了之后的XCTestCase