您的位置:首页 > 其它

POJ 1873 The Fortified Forest 暴力凸包

2017-06-28 22:08 260 查看
The Fortified Forest

Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 6615 Accepted: 1845
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  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


题意:n个树各自有价值,长度和坐标,要砍掉一部分树做围墙把剩下的树都围起来,求最多保留多少价值的树,相同价值下选保留价值最小的方法

思路:暴力枚举,凸包判断是否能围成

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
#define ll long long
#define ms(a,b)memset(a,b,sizeof(a))
#define eps 1e-10
#define inf 1e8

double a[4][4] = { {0,0,0,0},{0,-1,0,0},{0,0,0,-1},{0,-1,0,-1} } ;
int n;
double add(double a,double b)
{
if(abs(a+b)<eps*(abs(a)+abs(b))) return 0;
return a+b;
}

struct P
{
double x,y;
double val;
double len;
P(){}
P(double x,double y): x(x),y(y){}
P operator + (P p)
{
return P(add(x,p.x),add(y,p.y));
}
P operator - (P p)
{
return P(add(x,-p.x),add(y,-p.y));
}
P operator *(double d)
{
return P(x*d,y*d);
}
double dot (P p)
{
return add(x*p.x,y*p.y);
}
double det(P p)
{
return add(x*p.y,-y*p.x);
}
}p[2000];

bool on_seg(P p1,P p2,P q)
{
return (p1-q).det(p2-q)==0&&(p1-q).dot(p2-q)<=0;
}
P intersection(P p1,P p2,P q1,P q2)
{
return p1+(p2-p1)*((q2-q1).det(q1-p1)/(q2-q1).det(p2-p1));
}

bool cmp_x(const P& p,const P & q)
{
if(p.x!=q.x) return p.x<q.x;
return p.y<q.y;
}

vector<P> convex_hull(P *ps,int n)
{
sort(ps,ps+n,cmp_x);
int k=0;
vector<P> qs(n*2);
if(n==0) return qs;
for(int i=0;i<n;i++)
{
while(k>1&&(qs[k-1]-qs[k-2]).det(ps[i]-qs[k-1])<=0) k--;
qs[k++]=ps[i];
}
for(int i=n-2,t=k;i>=0;i--)
{
while(k>t&&(qs[k-1]-qs[k-2]).det(ps[i]-qs[k-1])<=0) k--;
qs[k++]=ps[i];
}
qs.resize(k-1);
return qs;
}

struct node
{
double extrawood;
double val;
int flag;
int cnt;
}ans;

int main()
{
int n;
int casi=1;
while(~scanf("%d",&n))
{
if(n==0) break;
ans.val=0;
ans.extrawood=0;
double totalval=0;
for(int i=0;i<n;i++)
scanf("%lf%lf%lf%lf",&p[i].x,&p[i].y,&p[i].val,&p[i].len),totalval+=p[i].val;
for(int i=0;i<(1<<n)-1;i++)
{
int cnt=0;
double cutlen=0;
double cutval=0;
P pp[20];
int flag=0;
for(int j=0;j<n;j++)
{
if(i>>j&1)
{
cutlen+=p[j].len;
cutval+=p[j].val;
flag+=1<<j;
}
else
{
pp[cnt++]=p[j];
}
}
if(ans.val>totalval-cutval) continue;
vector<P> qs;
qs=convex_hull(pp,cnt);
int q_size=qs.size();
double limitlen=0;
if(q_size>=2)
{
for(int k=0;k<qs.size();k++)
{
limitlen+=sqrt((qs[k]-qs[(k+1)%q_size]).dot(qs[k]-qs[(k+1)%q_size]));
}
}

if(cutlen-limitlen>=-eps)
{
if(ans.val<totalval-cutval)
{
ans.cnt=cnt;
ans.val=totalval-cutval;
ans.flag=flag;
ans.extrawood=cutlen-limitlen;
}
else if(ans.val==totalval-cutval&&ans.cnt<cnt)
{
ans.flag=flag;
ans.cnt=cnt;
ans.extrawood=cutlen-limitlen;
}
}
}
printf("Forest %d\nCut these trees:",casi++);
for(int k=0;k<n;k++)
{
if(ans.flag>>k&1)
{
printf(" %d",k+1);
}
}
printf("\n");
printf("Extra wood: %.2lf\n",ans.extrawood);
printf("\n");
}
return 0 ;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: