poj 1873 The Fortified Forest (凸包)

The Fortified Forest
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 6383 Accepted: 1804

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

Source

[Submit]   [Go Back]   [Status]   [Discuss]


题目大意:有N颗树,给出每棵树额坐标,权值和长度。选取其中的某些树砍掉,使选中的木材可以围住剩下的树,并且权值和最小。输出选中的木材和多余的木料。

题解:凸包。

因为n很小,所以直接枚举砍树的方案,然后判断是否可行即可。

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define N 100
using namespace std;
int n,vis[N],m,mark[N];
struct point{
	int num,v;
	double x,y,len;
	point (double X=0,double Y=0){
		x=X,y=Y;
	}
}a[N],ch[N],p[N];
double ansv,ansx;
typedef point vector;
bool operator <(point a,point b){
	return a.x<b.x||a.x==b.x&&a.y<b.y;
}
vector operator -(vector a,vector b){
	return vector (a.x-b.x,a.y-b.y);
}
double cross(vector a,vector b){
	return a.x*b.y-a.y*b.x;
}
void solve(point *p,int n,point *ch)
{
	sort(p+1,p+n+1);
	if (n==1) {
		ch[m++]=p[1];
		return;
	}
	m=0;
	for (int i=1;i<=n;i++){
		if (vis[p[i].num]) continue;
		while (m>1&&cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<0) m--;
		ch[m++]=p[i];
	}
	int k=m;
	for (int i=n-1;i>=1;i--){
		if (vis[p[i].num]) continue;
		while (m>k&&cross(ch[m-1]-ch[m-2],p[i]-ch[m-2])<0) m--;
		ch[m++]=p[i];
	}
	m--;
}
double len(vector a)
{
	return sqrt(a.x*a.x+a.y*a.y);
}
void dfs(int x,int sum,double tot)
{
	if (x==n+1) {
		m=0;
		solve(p,n,ch);
		double wood=0;
		for (int i=0;i<m-1;i++) wood+=len(ch[i]-ch[i+1]);
		if (m>=2)  wood+=len(ch[m-1]-ch[0]);
		if (m==1) wood=0;
		if (wood<=tot) {
			if (sum<ansv) {
			 ansv=sum,ansx=tot-wood;
			 for (int i=1;i<=n;i++) mark[i]=vis[i];
		    }
		}
		return;
	}
	vis[x]=1; dfs(x+1,sum+a[x].v,tot+a[x].len);
	vis[x]=0; dfs(x+1,sum,tot);
}
int main()
{
	freopen("a.in","r",stdin);
	int t=0;
	while (true) {
		scanf("%d",&n); t++;
		if (!n) break;
		memset(vis,0,sizeof(vis));
		memset(mark,0,sizeof(mark));
		for (int i=1;i<=n;i++){
		  scanf("%lf%lf%d%lf",&a[i].x,&a[i].y,&a[i].v,&a[i].len);
		  a[i].num=i; p[i]=a[i];
		}
		ansv=10000000;
		dfs(1,0,0);
		printf("Forest %d\n",t);
		printf("Cut these trees: ");
		for (int i=1;i<=n;i++)
		 if (mark[i]) printf("%d ",i);
		printf("\n");
		printf("Extra wood: %.2lf\n",ansx);
		printf("\n");
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值