poj1873The Fortified Forest

链接

居然是WF的水题~

二进制枚举砍哪些树,剩余的树围成一个凸包。

因为传数组WA了两发,忘记修改排序数组中的p[0];

  1 #include <iostream>
  2 #include<cstdio>
  3 #include<cstring>
  4 #include<algorithm>
  5 #include<stdlib.h>
  6 #include<vector>
  7 #include<cmath>
  8 #include<queue>
  9 #include<set>
 10 using namespace std;
 11 #define N 20
 12 #define LL long long
 13 #define INF 0xfffffff
 14 const double eps = 1e-8;
 15 const double pi = acos(-1.0);
 16 const double inf = ~0u>>2;
 17 struct point
 18 {
 19     int x,y;
 20     int vi,li;
 21     point(int x=0,int y=0):x(x),y(y){}
 22 }p[N],q[N],ch[N];
 23 int o[N],f[N];
 24 typedef point pointt;
 25 pointt operator -(point a,point b)
 26 {
 27     return point(a.x-b.x,a.y-b.y);
 28 }
 29 int dcmp(double x)
 30 {
 31     if(fabs(x)<eps) return 0;
 32     return x<0?-1:1;
 33 }
 34 double cross(point a,point b)
 35 {
 36     return 1.0*a.x*b.y-a.y*b.x;
 37 }
 38 double mul(point p0,point p1,point p2)
 39 {
 40     return cross(p1-p0,p2-p0);
 41 }
 42 double dis(point a)
 43 {
 44     return sqrt(1.0*a.x*a.x+a.y*a.y);
 45 }
 46 bool cmp(point a,point b)
 47 {
 48     if(dcmp(mul(q[0],a,b))==0)
 49         return dis(a-q[0])<dis(b-q[0]);
 50     else
 51         return dcmp(mul(q[0],a,b))>0;
 52 }
 53 double Graham(int n)
 54 {
 55     if(n<=1) return 0;
 56     if(n==2) return 2*dis(q[0]-q[1]);
 57     int i,k = 0,top;
 58     point tmp;
 59     for(i = 0 ; i < n; i++)
 60     {
 61         if(q[i].y<q[k].y||(q[i].y==q[k].y&&q[i].x<q[k].x))
 62             k = i;
 63     }
 64     if(k!=0)
 65     {
 66         tmp = q[0];
 67         q[0] = q[k];
 68         q[k] = tmp;
 69     }
 70     sort(q+1,q+n,cmp);
 71     ch[0] = q[0];
 72     ch[1] = q[1];
 73     top = 1;
 74     for(i = 2; i < n ; i++)
 75     {
 76         while(top>0&&dcmp(mul(ch[top-1],ch[top],q[i]))<0)
 77             top--;
 78         top++;
 79         ch[top] =q[i];
 80     }
 81     ch[top+1] = ch[0];
 82     double len = 0;
 83     for(i = 0 ; i <= top ; i++)
 84     len+=dis(ch[i]-ch[i+1]);
 85     return len;
 86 }
 87 int main()
 88 {
 89     int n,i,j,g;
 90     int kk = 0;
 91     while(scanf("%d",&n)&&n)
 92     {
 93         for(i = 0 ; i <n ;i++)
 94         scanf("%d%d%d%d",&p[i].x,&p[i].y,&p[i].vi,&p[i].li);
 95         int num;
 96         int ans = INF;
 97         double res = 0;
 98         for(i = 0 ; i < (1<<n) ; i++)
 99         {
100             g = 0;
101             int gg = 0;
102             int tv = 0;
103             double tlen = 0;
104             for(j = 0 ;j < n; j++)
105             {
106                 if((1<<j)&i)
107                 {
108                     f[gg++] = j;
109                     tlen+=p[j].li;
110                     tv += p[j].vi;
111                 }
112                 else q[g++] = p[j];
113             }
114             double len = Graham(g);
115             if(dcmp(tlen-len)>=0)
116             {
117                 if(ans>tv||(ans==tv&&num>gg))
118                 {
119                     ans = tv;
120                     num = gg;
121                     res = tlen-len;
122                     //cout<<len<<" "<<i<<endl;
123
124
125                     for(j = 0 ; j < gg ; j++)
126                     o[j] = f[j]+1;
127                 }
128             }
129         }
130         if(kk) puts("");
131         printf("Forest %d\n",++kk);
132         printf("Cut these trees: ");
133         for(i = 0; i < num-1; i++)
134         printf("%d ",o[i]);
135         if(num)
136         printf("%d\n",o[num-1]);
137         printf("Extra wood: %.2f\n",res);
138     }
139     return 0;
140 }

poj1873The Fortified Forest

时间: 2024-10-06 21:08:52

poj1873The Fortified Forest的相关文章

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 r

poj 1873 The Fortified Forest(凸包)

The Fortified Forest Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5737   Accepted: 1636 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

POJ 1873 The Fortified Forest(凸包+枚举 World Finals 1999啊)

题目链接:http://poj.org/problem?id=1873 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 fr

POJ 1873 - The Fortified Forest 凸包 + 搜索 模板

通过这道题发现了原来写凸包的一些不注意之处和一些错误..有些错误很要命.. 这题 N = 15 1 << 15 = 32768 直接枚举完全可行 卡在异常情况判断上很久,只有 顶点数 >= 2,即 n >= 3 时凸包才有意义 顶点数为 1 时,tmp = - 1 要做特殊判断. 总结了一下凸包模板 //template Convex Hull friend bool operator < (const point &p1, const point &p2){

Uva5211/POJ1873 The Fortified Forest 凸包

LINK 题意:给出点集,每个点有个价值v和长度l,问把其中几个点取掉,用这几个点的长度能把剩下的点围住,要求剩下的点价值和最大,拿掉的点最少且剩余长度最长. 思路:1999WF中的水题.考虑到其点的数量最多只有15个,那么可以使用暴力枚举所有取点情况,二进制压缩状态,预处理出该状态下的价值,同时记录该状态拥有的点,并按价值排序.按价值枚举状态,并对拥有的这些点求凸包,check是否合法,找到一组跳出即可.然而POJ似乎没有SPJ,同样的代码POJ会超时,UVA60ms,可以在常数上优化,不预处

UVA 811 The Fortified Forest (凸包 + 状态压缩枚举)

题目链接:UVA 811 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 or

poj 1873(枚举所有的状态+凸包)

The Fortified Forest Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6115   Accepted: 1720 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

poj 1873 凸包+枚举

The Fortified Forest Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 6198   Accepted: 1744 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

【转】计算几何题目推荐

打算转下来好好做计算几何了. 原文地址:http://blog.sina.com.cn/s/blog_49c5866c0100f3om.html 其实也谈不上推荐,只是自己做过的题目而已,甚至有的题目尚未AC,让在挣扎中.之所以推荐计算几何题,是因为,本人感觉ACM各种算法中计算几何算是比较实际的算法,在很多领域有着重要的用途计算几何题的特点与做题要领:1.大部分不会很难,少部分题目思路很巧妙2.做计算几何题目,模板很重要,模板必须高度可靠.3.要注意代码的组织,因为计算几何的题目很容易上两百行