POJ 3608 Bridge Across Islands (计算几何+三分)

Bridge Across Islands
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 7395 Accepted: 2183 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 N, M. (3 ≤ N, M ≤ 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

 
 
 
#include <iostream>
#include <cstdio>
#include <vector>
#include <cmath>
#include <algorithm>
#include <cstdlib>
using namespace std;

const int maxn=1000;
const double eps=1e-10;


struct point{
	double x,y;
	point(double x0=0,double y0=0){
		x=x0;
		y=y0;
	}
	friend bool operator < (point a,point b){
		if(abs(a.y-b.y) > eps) return a.y<b.y;
		else return a.x<b.x;
	}
	double getdis(point p){
		return sqrt((x-p.x)*(x-p.x)+(y-p.y)*(y-p.y));
	}
};

int m,n;
vector <point> vm,vn,p;

double xchen(point a,point b,point c){//ab 叉乘 ac
	return (b.x-a.x)*(c.y-a.y)-(c.x-a.x)*(b.y-a.y);
}

double dchen(point p0,point p1,point p2){//p0p1 点乘 p0p2;
	return (p1.x-p0.x)*(p2.x-p0.x)+(p1.y-p0.y)*(p2.y-p0.y);
}

bool cmp(point a,point b){
	return xchen(p[0],a,b)>0;
}

double getldis(point a,point b,point p){//点到线段的最小距离
	if(dchen(a,p,b)<0 || dchen(b,p,a)<0) return min(p.getdis(a),p.getdis(b));
	else{
		double a0,b0,c0;
		a0=a.y-b.y;
		b0=b.x-a.x;
		c0=a.x*b.y-b.x*a.y;
		return fabs(a0*p.x+b0*p.y+c0)/sqrt(a0*a0+b0*b0);
	}
}

void qiutubao(){
	p.resize(m);
	for(int i=0;i<m;i++){
		scanf("%lf%lf",&p[i].x,&p[i].y);
	}
	sort(p.begin(),p.end());
	sort(p.begin()+1,p.end(),cmp);
	vm=p;
	vm.push_back(p[0]);
	p.resize(n);
	for(int i=0;i<n;i++){
		scanf("%lf%lf",&p[i].x,&p[i].y);
	}
	sort(p.begin(),p.end());
	sort(p.begin()+1,p.end(),cmp);
	vn=p;
	vn.push_back(p[0]);
}

void computing(){
	qiutubao();
	double mindis=1e12;
	for(int i=0;i<m;i++){
		int l=0,r=n-1;
		while(l<r){
			//cout<<l<<" "<<r<<endl;
			//system("pause");
			int lmd=(2*l+r)/3;
			int rmd=(l+2*r)/3;
			double dislmd=getldis(vm[i],vm[i+1],vn[lmd]);
			double disrmd=getldis(vm[i],vm[i+1],vn[rmd]);
			if(dislmd<=disrmd) r=rmd;
			else l=lmd+1;
		}
		mindis=min(mindis,getldis(vm[i],vm[i+1],vn[r]));
	}
	for(int i=0;i<n;i++){
		int l=0,r=m-1;
		while(l<r){
			int lmd=(2*l+r)/3;
			int rmd=(l+2*r)/3;
			double dislmd=getldis(vn[i],vn[i+1],vm[lmd]);
			double disrmd=getldis(vn[i],vn[i+1],vm[rmd]);
			if(dislmd<=disrmd) r=rmd;
			else l=lmd+1;
		}
		mindis=min(mindis,getldis(vn[i],vn[i+1],vm[r]));
	}
	printf("%.5lf\n",mindis);
}

int main(){
	while(scanf("%d%d",&m,&n)!=EOF && (m||n)){
		computing();
	}
	return 0;
}

转载于:https://www.cnblogs.com/toyking/p/3797390.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值