The Fortified Forest - POJ 1873(状态枚举+求凸包周长)

题目大意:有个国王他有一片森林,现在他想从这个森林里面砍伐一些树木做成篱笆把剩下的树木围起来,已知每个树都有不同的价值还有高度,求出来砍掉那些树可以做成篱笆把剩余的树都围起来,要使砍伐的树木的价值最小,如果有价值相同的尽量使砍伐的树木少一些。

分析:因为树木的数量是比较少的,所以枚举所有的状态,判断那个树需要砍那个树不需要,然后按照要求求出来答案即可。

代码如下:

==============================================================================================

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std; const double EPS = 1e-;
const int MAXN = ;
const int oo = 1e9+; int vi[MAXN], li[MAXN], sta[MAXN], top; struct point
{
double x, y; point(double x=, double y=):x(x), y(y){}
point operator - (const point &t)const{
return point(x-t.x, y-t.y);
}
double operator *(const point &t)const{
return x*t.x + y*t.y;
}
double operator ^(const point &t)const{
return x*t.y - y*t.x;
}
}p[MAXN], date[MAXN];
int Sign(double t)
{
if(t > EPS)return ;
if(fabs(t) < EPS)return ;
return -;
}
double Dist(point a, point b)
{
return sqrt((a-b)*(a-b));
}
bool cmp(point a, point b)
{
int t = Sign((a-p[])^(b-p[])); if(t == )
return Dist(a, p[]) < Dist(b, p[]);
return t > ;
}
void Graham(int N)
{
int k=; for(int i=; i<N; i++)
{
if(p[k].y>p[i].y || (Sign(p[k].y-p[i].y)== && p[k].x > p[i].x))
k = i;
}
swap(p[], p[k]);
sort(p+, p+N, cmp); sta[]=, sta[]=, top=; if(N < )
{
top = N-;
return ;
} for(int i=; i<N; i++)
{
while(top> && Sign((p[i]-p[sta[top]])^(p[sta[top-]]-p[sta[top]])) <= )
top--;
sta[++top] = i;
}
}
bool Solve(int bitNum, int N, int &val, int &nOne, double &lastLen)
{
int cut_val=, cnt=, cut_len=; for(int i=; i<N; i++)
{
if(bitNum & )
{
cut_val += vi[i];
cut_len += li[i];
}
else
p[cnt++] = date[i];
bitNum >>= ;
} Graham(cnt); double len=;
sta[++top] = sta[]; for(int i=; i<=top; i++)
{
len += Dist(p[sta[i]], p[sta[i+]]);
} if(Sign(cut_len-len) >= && (val > cut_val || (val==cut_val&&nOne > N-cnt)))
{
lastLen = cut_len - len;
val = cut_val;
nOne = N-cnt;
return true;
}
return false;
} int main()
{
int N, t=; while(scanf("%d", &N), N)
{
int val=oo, ans, nOne=oo;
double lastLen; for(int i=; i<N; i++)
scanf("%lf%lf%d%d", &date[i].x, &date[i].y, &vi[i], &li[i]); int Len = (<<N)-; for(int i=; i<Len; i++)
{
if(Solve(i, N, val, nOne, lastLen) == true)
ans = i;
}
if(t!=)printf("\n");
printf("Forest %d\n", t++);
printf("Cut these trees:");
for(int i=; ans; i++)
{
if(ans & )
printf(" %d", i);
ans >>= ;
}
printf("\nExtra wood: %.2f\n", lastLen);
} return ;
}
上一篇:单片机TM4C123学习(八):SPI接口D/A


下一篇:Developing Backbone.js Applications