poj1556(計算幾何+最短路)

#include<iostream>
#include<cstdio>
#include<math.h>
#include<algorithm>
#define inf 0x3f3f3f
const int maxn = 1e3+10;
const double eps = 1e-8;
using namespace std;
struct Poit{
	double x;
	double y;
	Poit(){	}
	Poit(double _x,double _y){
		x=_x,y=_y;
	}
}pt[maxn];
double M[maxn][maxn];
struct Line{
	double x1,x2;
	double y1,y2;
	Line(){}
	Line(double _x1,double _y1,double _x2,double _y2){
		x1=_x1;
		x2=_x2;
		y1=_y1;
		y2=_y2; 
	}
}le[maxn];
void init(){
	for(int i=0;i<=maxn;i++){
		for(int j=0;j<=maxn;j++){
			M[i][j]=inf;
		}
	}
}
	int n;
double dis(Poit a, Poit b) {
    return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
}
 
double cross(double x1, double y1, double x2, double y2) {
    return x1*y2 - x2*y1;
}

bool judge(Poit a, Poit b) {//向量叉積判斷是否相交
    for (int i = 1; i <= n*3; i++) {
        if (cross(le[i].x1-a.x,le[i].y1-a.y,b.x-a.x,b.y-a.y) *
            cross(b.x-a.x,b.y-a.y,le[i].x2-a.x,le[i].y2-a.y) > eps
            && cross(a.x-le[i].x2,a.y-le[i].y2,le[i].x1-le[i].x2,le[i].y1-le[i].y2) *
            cross(le[i].x1-le[i].x2,le[i].y1-le[i].y2,b.x-le[i].x2,b.y-le[i].y2) > eps)
                return false;
    }
    return true;
}
int main(){
	while(scanf("%d",&n)&&n!=-1){
		init();
		for(int i=1;i<=n;i++){
			double a,b,c,d,e;
			cin>>a>>b>>c>>d>>e;
			pt[i*4-3]=Poit(a,b);
			pt[i*4-2]=Poit(a,c);
			pt[i*4-1]=Poit(a,d);
			pt[i*4]=Poit(a,e);
			 le[i*3-2] = Line(a, 0, a, b);
            le[i*3-1] = Line(a, c, a, d);
            le[i*3-0] = Line(a, e, a, 10);
		}
		   pt[n*4+1] = Poit(0, 5); pt[n*4+2] = Poit(10, 5);
        for (int i = 1; i <= n*4+2; i++) {
            for (int j = i+1; j <= n*4+2; j++) {
                if (fabs(pt[i].x-pt[j].x) < eps) continue;
                if (judge(pt[i], pt[j])) {
                    //printf("%f %f %f %f --- %f\n",pt[i].x, pt[i].y, pt[j].x, pt[j].y,abs(pt[i].x-pt[j].x));
                    M[i][j] = M[j][i] = dis(pt[i],pt[j]);
                }
            }
    	}
      //  printf("%.2f\n", mat[4*n+1][4*n+2]);
        for(int k=1;k<=4*n+2;k++){
        	for(int i=1;i<=4*n+2;i++){
        		for(int j=1;j<=4*n+2;j++){
        			M[i][j]=min(M[i][j],M[i][k]+M[k][j]);
				}
			}
		}
		printf("%.2f\n", M[4*n+1][4*n+2]);
	}
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章