poj 1873

陈年WF题,纪念一下。。

说是练计算几何。。然而这题的主要思路是dfs+剪枝,其实学会恰当的剪枝是非常重要的。。。(到时或许全程就靠这个了?qaq我也想学A叉S搜索orz

暴力能300ms以内也算不错了,个人认为本题能够优化一点的地方就是把点都按照价值排序。。回溯时优先考虑不砍树或尽可能砍价值低的树,这样最优方案能够早些出来来排除很多不必要的搜索。。当然标号得记好。。。

然后代码看起来十分不友好。。还好1A了不然前程会十分黑暗w(゚Д゚)w


#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<queue>
#include<cmath>
#define inc(i,l,r) for(int i=l;i<=r;i++)
#define dec(i,l,r) for(int i=l;i>=r;i--)
#define link(x) for(edge *j=h[x];j;j=j->next)
#define inf 1e9
#define eps 1e-8
#define mem(a) memset(a,0,sizeof(a))
#define ll long long
#define succ(x) (1<<x)
#define lowbit(x) (x&&(-x))
#define sqr(x) ((x)*(x))
#define NM 10000
#define pi 3.141592653
using namespace std;

int read(){
	int x=0,f=1;char ch=getchar();
	while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
	while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
	return f*x;
}



struct P{
	int x,y,v,l,id;
	P(int x=0,int y=0,int v=0,int l=0,int id=0):id(id),x(x),y(y),v(v),l(l){}
	P operator-(const P&o){return P(x-o.x,y-o.y);}
	int operator*(const P&o){return x*o.y-y*o.x;}
}a[NM],s[NM],c[NM];
double dis(P o){return sqrt((double)sqr(o.x)+sqr(o.y));}
bool cmp(P x,P y){return x.v>y.v;}
bool _cmp(P x,P y){return (x.x<y.x)||(x.x==y.x&&x.y<y.y);}

int n,ans,ansb[NM],ansv,_l,_v,m,b[NM];
double ansl;
void solve(){
	mem(c);mem(s);int tot=0;
	inc(i,1,m)c[i]=a[b[i]];
	sort(c+1,c+1+m,_cmp);
	inc(i,1,m){
		while(tot>1&&(s[tot]-s[tot-1])*(c[i]-s[tot])<0)tot--;
		s[++tot]=c[i];
	}
	dec(i,m-1,1){
		while(tot>1&&(s[tot]-s[tot-1])*(c[i]-s[tot])<0)tot--;
		s[++tot]=c[i];
	}
	double t=0;
	inc(i,1,tot-1)t+=dis(s[i+1]-s[i]);
	if(t<=_l){
		inc(i,1,m)ansb[i]=a[b[i]].id;
		ansv=_v;ansl=_l-t;ans=m;
	}
}

void dfs(int x){
	if(_v>ansv||(ansv==_v&&m>=ans))return;
	if(x==n+1){solve();return;}
	b[++m]=x;
	dfs(x+1);
	b[m--]=0;
	_l+=a[x].l;_v+=a[x].v;
	dfs(x+1);
	_l-=a[x].l;_v-=a[x].v;
}

int main(){
	//freopen("data.in","r",stdin);
	int T=0;
	while(n=read()){
		ansv=inf;m=ans=_l=_v=0;mem(a);mem(b);mem(ansb);ansl=0;
		printf("Forest %d\n",++T);
		inc(i,1,n)a[i].x=read(),a[i].y=read(),a[i].v=read(),a[i].l=read(),a[i].id=i;
		sort(a+1,a+1+n,cmp);
		dfs(1);
		printf("Cut these trees:");
		inc(i,1,n){
			bool f=true;
			inc(j,1,ans)if(i==ansb[j])f=false;
			if(f)printf(" %d",i);
		}
		printf("\nExtra wood: %.2lf\n\n",ansl);
	}
	return 0;
}


The Fortified Forest
Time Limit: 1000MS Memory Limit: 30000K
Total Submissions: 6940 Accepted: 1931

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]


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值