题目描述
传送门
题意:给出一些树(坐标,价值,长度),要求砍下一些树做成篱笆,将剩下的树围起来。求砍下的树最小价值的方案,如果有多种方案,输出砍的树的个数最少的。
题解
n只有15用dfs就可以了
dfs之后求凸包的周长,然后再判断价值什么的
代码
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
using namespace std;
#define inf 1000000000
#define N 20
const double eps=1e-9;
int dcmp(double x)
{
if (x<=eps&&x>=-eps) return 0;
return (x>0)?1:-1;
}
struct Vector
{
double x,y;
Vector(double X=0,double Y=0)
{
x=X,y=Y;
}
bool operator < (const Vector &a) const
{
return x<a.x||(x==a.x&&y<a.y);
}
};
typedef Vector Point;
Vector operator - (Vector A,Vector B) {return Vector(A.x-B.x,A.y-B.y);}
int T,n,Min,cnt,top;
double ew;
int v[N],wh[N],tr[N];
double x[N],y[N],l[N];
Point p[N],stack[N];
double Dot(Vector A,Vector B)
{
return A.x*B.x+A.y*B.y;
}
double Cross(Vector A,Vector B)
{
return A.x*B.y-A.y*B.x;
}
double Len(Vector A)
{
return sqrt(Dot(A,A));
}
void Graham()
{
sort(p+1,p+cnt+1);
top=0;
for (int i=1;i<=cnt;++i)
{
while (top>1&&dcmp(Cross(stack[top]-stack[top-1],p[i]-stack[top-1]))<=0)
--top;
stack[++top]=p[i];
}
int k=top;
for (int i=cnt-1;i>=1;--i)
{
while (top>k&&dcmp(Cross(stack[top]-stack[top-1],p[i]-stack[top-1]))<=0)
--top;
stack[++top]=p[i];
}
if (cnt>1) --top;
}
void check()
{
double len=0,ans=0;int val=0;
cnt=0;
for (int i=1;i<=n;++i)
if (wh[i]) p[++cnt]=Point(x[i],y[i]);
else len+=l[i],val+=v[i];
Graham();
for (int i=1;i<=top;++i)
ans+=Len(stack[i%top+1]-stack[(i+1)%top+1]);
if (dcmp(len-ans)>=0)
{
if (val<Min)
{
Min=val;ew=len-ans;tr[0]=0;
for (int i=1;i<=n;++i)
if (!wh[i]) tr[++tr[0]]=i;
}
else if (val==Min&&cnt<tr[0])
{
ew=len-ans;tr[0]=0;
for (int i=1;i<=n;++i)
if (!wh[i]) tr[++tr[0]]=i;
}
}
}
void dfs(int dep)
{
if (dep==n+1)
{
check();
return;
}
for (int i=1;i>=0;--i)
{
wh[dep]=i;
dfs(dep+1);
}
}
int main()
{
while (~scanf("%d",&n))
{
if (!n) break;
for (int i=1;i<=n;++i) scanf("%lf%lf%d%lf",&x[i],&y[i],&v[i],&l[i]);
Min=inf;tr[0]=0;dfs(1);
printf("Forest %d\n",++T);
printf("Cut these trees:");
for (int i=1;i<=tr[0];++i) printf(" %d",tr[i]);puts("");
printf("Extra wood: %.2lf\n",ew);
puts("");
}
}