poj 1873 The Fortified Forest (暴力搜索+凸包求周长)

来源:互联网 发布:止汗露的危害 知乎 编辑:程序博客网 时间:2024/05/15 13:25

The Fortified Forest
Time Limit: 1000MS Memory Limit: 30000KB 64bit IO Format: %I64d & %I64u

[Submit]   [Go Back]   [Status]  

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

6 0  0  8  3 1  4  3  2 2  1  7  1 4  1  2  3 3  5  4  6 2  3  9  83 3  0 10  2 5  5 20 25 7 -3 30 320

Sample Output

Forest 1Cut these trees: 2 4 5 Extra wood: 3.16Forest 2Cut these trees: 2 Extra wood: 15.00


#include <cstdio>#include <cmath>#include <algorithm>const long M=110;const double Exp=1e-8;struct Point{double x,y;}P;inline double Det(Point p,Point p1,Point p2){return (p1.x-p.x)*(p2.y-p.y)-(p2.x-p.x)*(p1.y-p.y);}inline double Dis(Point p1,Point p2){return sqrt((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y));}long cmp(Point a,Point b){double k=Det(P,a,b);return (k>0 || (k==0 && Dis(P,a)<Dis(P,b)));}struct Poly{Point point[M];long stack[M],top,cnt;void Convext(){for (long i=1;i<cnt;++i)if (point[i].y<point[0].y || (point[i].y==point[0].y && point[i].x<point[0].x)){Point tmp=point[0]; point[0]=point[i]; point[i]=tmp;}P=point[0];std::sort(point+1,point+cnt,cmp);for (long i=0;i<=2;++i) stack[i]=i; top=2;for (long i=3;i<cnt;++i){while (top>=2 && Det(point[stack[top-1]],point[stack[top]],point[i])<=0)top--;stack[++top]=i;}}double Circle_len(long n){double len=0; cnt=n;if (n==1) return 0.0;if (n==2) return Dis(point[0],point[1]);Convext();for (long i=1;i<=top;++i)len+=Dis(point[stack[i-1]],point[stack[i]]);len+=Dis(point[stack[top]],point[stack[0]]);return len;}}tree;long n,cut[M],ans_cut[M];double val[M],l[M],ans_val,ans_len;Point p[M];void init(){memset(cut,0,sizeof(cut));ans_val=1e100;}void toAns(double cut_val,double extra_len){ans_val=cut_val; ans_len=extra_len;for (long i=1;i<=n;++i) ans_cut[i]=cut[i];}void dfs(long x,long cut_cnt){double cut_len=0,cut_val=0,need_len;if (cut_cnt==n) return;if (x>n){long cnt=0;for (long i=1;i<=n;++i)if (cut[i]){ cut_val+=val[i]; cut_len+=l[i];}else tree.point[cnt++]=p[i];need_len=tree.Circle_len(cnt);if (cnt==2) need_len=need_len*2;if (need_len>cut_len) return;double extra_len=cut_len-need_len;if (cut_val<ans_val|| (cut_val==ans_val && extra_len<ans_len))toAns(cut_val,extra_len);}else{cut[x]=1; dfs(x+1,cut_cnt+1);cut[x]=0; dfs(x+1,cut_cnt);}}void output(long total){printf("Forest %d\n",total);printf("Cut these trees:");for (long i=1;i<=n;++i)if (ans_cut[i]) printf(" %d",i);printf("\nExtra wood: %.2f\n",ans_len);}int main(){long total=0;while (~scanf("%d",&n) && n){for (long i=1;i<=n;++i)scanf("%lf%lf%lf%lf",&p[i].x,&p[i].y,&val[i],&l[i]);init();dfs(1,0);if (total) printf("\n");output(++total);}return 0;}



原创粉丝点击