[POJ3608]Bridge Across Islands

Bridge Across Islands
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 11607 Accepted: 3419 Special Judge

Description

Thousands of thousands years ago there was a small kingdom located in the middle of the Pacific Ocean. The territory of the kingdom consists two separated islands. Due to the impact of the ocean current, the shapes of both the islands became convex polygons. The king of the kingdom wanted to establish a bridge to connect the two islands. To minimize the cost, the king asked you, the bishop, to find the minimal distance between the boundaries of the two islands.

Input

The input consists of several test cases.
Each test case begins with two integers NM. (3 ≤ NM ≤ 10000)
Each of the next N lines contains a pair of coordinates, which describes the position of a vertex in one convex polygon.
Each of the next M lines contains a pair of coordinates, which describes the position of a vertex in the other convex polygon.
A line with N = M = 0 indicates the end of input.
The coordinates are within the range [-10000, 10000].

Output

For each test case output the minimal distance. An error within 0.001 is acceptable.

Sample Input

4 4
0.00000 0.00000
0.00000 1.00000
1.00000 1.00000
1.00000 0.00000
2.00000 0.00000
2.00000 1.00000
3.00000 1.00000
3.00000 0.00000
0 0

Sample Output

1.00000

Source

POJ Founder Monthly Contest – 2008.06.29, Lei Tao


大致题意:

给你两个凸多边形,求这两个凸多边形间最小距离。

题解:



AC代码:

#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#define eps 1e-10
using namespace std;
const int Maxn=100005; 
struct node{
	double x,y;
}a1[Maxn],ans1[Maxn],a2[Maxn],ans2[Maxn];
double ojbk[Maxn];
int n,sum1,sum2,cnt,m;
bool cmp(node x,node y){
	if(x.y!=y.y)return x.y<y.y;
	return x.x<y.x;
}
node operator -(const node &x,const node &y){
	node c;
	c.x=x.x-y.x;c.y=x.y-y.y;
	return c;
}
inline double cro(node o,node x,node y){
	x=x-o;y=y-o;
	return x.x*y.y-x.y*y.x;
}
inline double dot(node o,node x,node y){
	x=x-o;y=y-o;
	return x.x*y.x+x.y*y.y;
}
inline double f(double x){
	return x*x;
}
inline double dis(node x,node y){
	return sqrt((x.x-y.x)*(x.x-y.x)+(x.y-y.y)*(x.y-y.y));
}
void convex1(){
	sort(a1+1,a1+1+n,cmp);
	for(int i=1;i<=n;i++){
		while(sum1>=2&&cro(ans1[sum1-1],ans1[sum1],a1[i])<=0){
			sum1--;
		}
		ans1[++sum1]=a1[i];
	}
	int lim=sum1;
	for(int i=n;i>=1;i--){
		while(sum1>=lim+1&&cro(ans1[sum1-1],ans1[sum1],a1[i])<=0){
			sum1--;
		}
		ans1[++sum1]=a1[i];
	}
	sum1--;
}
void convex2(){
	sort(a2+1,a2+1+m,cmp);
	for(int i=1;i<=m;i++){
		while(sum2>=2&&cro(ans2[sum2-1],ans2[sum2],a2[i])<=0){
			sum2--;
		}
		ans2[++sum2]=a2[i];
	}
	int lim=sum2;
	for(int i=m;i>=1;i--){
		while(sum2>=lim+1&&cro(ans2[sum2-1],ans2[sum2],a2[i])<=0){
			sum2--;
		}
		ans2[++sum2]=a2[i];
	}
	sum2--;
}
double mn(double x,double y){
	return x<y?x:y;
}
inline int add1(int x,int y){
	x+=y;
	x%=n+1;
	if(x==0)x=1;
	return x;
}
inline int add2(int x,int y){
	x+=y;
	x%=m+1;
	if(x==0)x=1;
	return x;
}
inline double ab(double x){
	return x>0?x:-x;
}
inline double disp(node x,node y,node z){
	if(dis(x,y)<eps)return dis(x,z);
	if(dot(x,y,z)<-eps)return dis(x,z);
	if(dot(y,x,z)<-eps)return dis(y,z);
	return ab(cro(x,y,z)/dis(x,y));
} 
inline double dist(node x1,node x2,node y1,node y2){
	double ansx;
	ansx=mn(mn(disp(x1,x2,y1),disp(x1,x2,y2)),mn(disp(y1,y2,x1),disp(y1,y2,x2)));
	return ansx;
}
void work(){
	double maxy=0,ansn=100000001,tmp;
	int i,j,nxi,nxj;
	for(int i=1;i<=n;i++){
		cin>>a1[i].x>>a1[i].y;
	}
	for(int i=1;i<=m;i++){
		cin>>a2[i].x>>a2[i].y;
	}
	sum1=sum2=0;
	convex1();
	convex2();
	n=sum1;m=sum2;
	for(int i=1;i<=m;i++){
		if(maxy<a2[i].y){
			maxy=a2[i].y;
			j=i;
		}
	}
	ans1[n+1]=ans1[1];
	ans2[m+1]=ans2[1];
	i=1;
	for(int p=1;p<=n;p++){
		nxi=add1(i,1);
		nxj=add2(j,1);
		while(cro(ans2[j],ans2[nxj],ans1[nxi])>cro(ans2[j],ans2[nxj],ans1[i])){
			i=nxi;
			nxi=add1(i,1);
		}
		ansn=mn(ansn,dist(ans1[i],ans1[nxi],ans2[j],ans2[nxj]));
		j=nxj; 
	}
	printf("%.5lf\n",ansn);
}
int main(){
	while(1){
		scanf("%d%d",&n,&m);
		if(n==0)return 0;
		work();
	} 
	return 0;
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值