首页 > 代码库 > poj 1556 The Doors

poj 1556 The Doors

The Doors

Time Limit: 1000 MS Memory Limit: 10000 KB

64-bit integer IO format: %I64d , %I64u   Java class name: Main

[Submit] [Status] [Discuss]

Description

You are to find the length of the shortest path through a chamber containing obstructing walls. The chamber will always have sides at x = 0, x = 10, y = 0, and y = 10. The initial and final points of the path are always (0, 5) and (10, 5). There will also be from 0 to 18 vertical walls inside the chamber, each with two doorways. The figure below illustrates such a chamber and also shows the path of minimal length. 

Input

The input data for the illustrated chamber would appear as follows.
2  4 2 7 8 9  7 3 4.5 6 7
The first line contains the number of interior walls. Then there is a line for each such wall, containing five real numbers. The first number is the x coordinate of the wall (0 < x < 10), and the remaining four are the y coordinates of the ends of the doorways in that wall. The x coordinates of the walls are in increasing order, and within each line the y coordinates are in increasing order. The input file will contain at least one such set of data. The end of the data comes when the number of walls is -1.

Output

The output should contain one line of output for each chamber. The line should contain the minimal path length rounded to two decimal places past the decimal point, and always showing the two decimal places past the decimal point. The line should contain no blanks.

Sample Input

15 4 6 7 824 2 7 8 97 3 4.5 6 7-1

Sample Output

10.0010.06

#include <iostream>#include <string.h>#include <stdio.h>#include <cmath>using namespace std;#define maxx 100#define INF 10000000struct Node{    double x;    double y;} p[maxx]; ///每扇门的终点 起点 和门的两个端点的平面坐标struct EDGE{    int u;    int v;} Edge[maxx*maxx]; ///存构造的边    因为之前是孤立的点int n;      ///n个墙double wX[20];  ///输入每堵墙的横坐标double py[20][4]; ///每堵墙横坐标对应的纵坐标 0 1 2 3double g[maxx][maxx];  ///存邻接矩阵  配合dis[]的double dis[maxx];  ///beg到其他点的最短距离int Psize;  ///边的数量int Esize;  ///点的数量double Dis(Node a,Node 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,double x3,double y3) ///判断(x3,y3)与(x1,y1)(x2,y2)是否交叉{ return (x2-x1)*(y3-y1)-(x3-x1)*(y2-y1);}bool IsOk(Node a,Node b) ///判断两点之间是否可以连线{ if(a.x>=b.x) return false; bool falg=true; int i=0; while(wX[i]<=a.x&&i<n) i++; while(wX[i]<b.x&&i<n) { if(cross(a.x,a.y,b.x,b.y,wX[i],0)*cross(a.x,a.y,b.x,b.y,wX[i],py[i][0])<0 ||cross(a.x,a.y,b.x,b.y,wX[i],py[i][1])*cross(a.x,a.y,b.x,b.y,wX[i],py[i][2])<0 ||cross(a.x,a.y,b.x,b.y,wX[i],py[i][3])*(cross(a.x,a.y,b.x,b.y,wX[i],10))<0) { falg=false; break; } i++; } return falg;}double Bellman(int beg,int end){ for(int i=0;i<maxx;i++) dis[i]=INF; dis[beg]=0; bool EX=true; for(int i=0;i<=Psize&&EX;i++) { EX=false; for(int j=0;j<Esize;j++) { if(dis[Edge[j].u]<INF&&dis[Edge[j].v]>(dis[Edge[j].u]+g[Edge[j].u][Edge[j].v])) { dis[Edge[j].v]=(dis[Edge[j].u]+g[Edge[j].u][Edge[j].v]); EX=true; } } } return dis[end];}int main(){ while(scanf("%d",&n)!=EOF) { if(n==-1) break; p[0].x=0; p[0].y=5; Psize=1; for(int i=0; i<n; i++) { cin>>wX[i]; for(int j=0; j<4; j++) { p[Psize].x=wX[i]; cin>>p[Psize].y; py[i][j]=p[Psize].y; Psize++; } } p[Psize].x=10; p[Psize].y=5; for(int i=0; i<=Psize; i++) { for(int j=0; j<=Psize; j++) { g[i][j]==INF; } } Esize=0; for(int i=0; i<=Psize; i++) { for(int j=i+1; j<=Psize; j++) { if(IsOk(p[i],p[j])) { g[i][j]=Dis(p[i],p[j]); Edge[Esize].u=i; Edge[Esize].v=j; Esize++; } } } printf("%.2lf\n",Bellman(0,Psize)); } return 0;}

 

poj 1556 The Doors