Poj 1873 The Fortified Forest
地址:http://poj.org/problem?id=1873
题目:
The Fortified Forest
Time Limit: 1000MS | Memory Limit: 30000K | |
Total Submissions: 6421 | Accepted: 1811 |
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.
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).
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.
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
思路:这题思路很简单 枚举+凸包就好了,不过我还是wa了好久,,,凸包卷积法的模板挂了
我的模板有毒!
1 /* 二维几何 */
2 /* 需要包含的头文件 */
3 #include<cstdio>
4 #include <cstring>
5 #include <cmath >
6 #include <iostream>
7 #include <algorithm>
8 #include <vector>
9 using namespace std;
10 /** 常用的常量定义 **/
11 const double INF = 1e200;
12 const double eps = 1e-8;
13 const double PI = acos(-1.0);
14 const int Max = 1e6;
15 /** 基本几何结构 **/
16 struct Point
17 {
18 double x,y;
19 Point(double a=0, double b=0){x=a,y=b;}
20 bool operator<(const Point &ta)const
21 {
22 if(x==ta.x) return y<ta.y;
23 return x<ta.x;
24 }
25 friend Point operator+(const Point &ta,const Point &tb)
26 {
27 return Point(ta.x+tb.x,ta.y+tb.y);
28 }
29 friend Point operator-(const Point &ta,const Point &tb)
30 {
31 return Point(ta.x-tb.x,ta.y-tb.y);
32 }
33 };
34 struct LineSeg ///线段,重载了/作为叉乘运算符,*作为点乘运算符
35 {
36 Point s,e;
37 LineSeg(){s=Point(0,0),e=Point(0,0);}
38 LineSeg(Point a, Point b){s=a,e=b;}
39 double lenth(void)
40 {
41 return sqrt((s.x-e.x)*(s.x-e.x)+(s.y-e.y)*(s.y-e.y));
42 }
43 friend double operator*(const LineSeg &ta,const LineSeg &tb)
44 {
45 return (ta.e.x-ta.s.x)*(tb.e.x-tb.s.x)+(ta.e.y-ta.s.y)*(tb.e.y-tb.s.y);
46 }
47 friend double operator/(const LineSeg &ta,const LineSeg &tb)
48 {
49 return (ta.e.x-ta.s.x)*(tb.e.y-tb.s.y)-(ta.e.y-ta.s.y)*(tb.e.x-tb.s.x);
50 }
51 LineSeg operator=(const LineSeg &ta)
52 {
53 s=ta.s,e=ta.e;
54 return *this;
55 }
56 };
57
58 int sgn(double ta,double tb);
59 double getdis(const Point &ta,const Point &tb);
60 double graham(Point tb[],double len);
61
62 int n,miv,v[20],cut[2][20];
63 double ex,use[20];
64 Point pt[20],tb[20];
65 vector<Point>va;
66 int main(void)
67 {
68 int k=0;
69 while(1==scanf("%d",&n)&&n)
70 {
71 miv=1e9,ex=0;
72 for(int i=0;i<n;i++)
73 scanf("%lf%lf%d%lf",&pt[i].x,&pt[i].y,&v[i],&use[i]);
74 for(int i=1,sz=(1<<n)-1;i<sz;i++)
75 {
76 va.clear();
77 int tv=0;
78 double len=0,lf=0;
79 cut[1][0]=0;
80 for(int j=0;j<n;j++)
81 if(i&(1<<j))
82 cut[1][++cut[1][0]]=j,len+=use[j],tv+=v[j];
83 else
84 va.push_back(pt[j]);
85 if(cut[1][0]==3)
86 {
87 134;
88 }
89 lf=graham(tb,len);
90 if(sgn(lf,0)<0)
91 continue;
92 if(miv>tv ||(tv==miv&&cut[1][0]<cut[0][0]))
93 {
94 for(int j=0,ls=cut[1][0];j<=ls;j++)
95 cut[0][j]=cut[1][j];
96 miv=tv,ex=lf;
97 }
98
99 }
100 printf("Forest %d\nCut these trees:",++k);
101 for(int i=1;i<=cut[0][0];i++)
102 printf(" %d",1+cut[0][i]);
103 printf("\nExtra wood: %.2f\n\n",ex);
104 }
105 return 0;
106 }
107
108
109
110
111 /*******判断ta与tb的大小关系*******/
112 int sgn(double ta,double tb)
113 {
114 if(fabs(ta-tb)<eps)return 0;
115 if(ta<tb) return -1;
116 return 1;
117 }
118 /*********求两点的距离*************/
119 double getdis(const Point &ta,const Point &tb)
120 {
121 return sqrt((ta.x-tb.x)*(ta.x-tb.x)+(ta.y-tb.y)*(ta.y-tb.y));
122 }
123
124 bool cmp(const Point &ta,const Point &tb)/// 选取与最后一条确定边夹角最小的点,即余弦值最大者
125 {
126 double tmp=LineSeg(va[0],ta)/LineSeg(va[0],tb);
127 if(sgn(tmp,0)==0)
128 return getdis(va[0],ta)<getdis(va[0],tb);
129 else if(tmp>0)
130 return 1;
131 return 0;
132 }
133 double graham(Point tb[],double len)
134 {
135 if(va.size()==1) return len;
136 else if(va.size()==2) return len-2.0*getdis(va[0],va[1]);
137 int cur=0,top=2;
138 for(int i=1;i<va.size();i++)
139 if(sgn(va[cur].y,va[i].y)>0 || (sgn(va[cur].y,va[i].y)==0 && sgn(va[cur].x,va[i].x)>0))
140 cur=i;
141 swap(va[cur],va[0]);
142 sort(va.begin()+1,va.end(),cmp);
143 tb[0]=va[0],tb[1]=va[1],tb[2]=va[2];
144 for(int i=3;i<va.size();i++)
145 {
146 while(sgn(LineSeg(tb[top-1],tb[top])/LineSeg(tb[top-1],va[i]),0)<0)
147 top--;
148 tb[++top]=va[i];
149 }
150 double tmp=0;
151 tb[++top]=tb[0];
152 for(int i=0;i<top;i++)
153 tmp+=getdis(tb[i],tb[i+1]);
154 return len-tmp;
155 }
作者:weeping
出处:www.cnblogs.com/weeping/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。