poj 1556(floyd)

来源:互联网 发布:淘宝卖家双11报名入口 编辑:程序博客网 时间:2024/06/05 16:41
The Doors
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 5596 Accepted: 2255

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 <cstdio>#include <vector>#include <cmath>using namespace std;const int maxn = 3*40;const double eps = 1e-7;struct wall{double y1;double y2;double x;wall(double a = 0, double b = 0 , double c = 0){x = a , y1 = b , y2 = c;}};struct point{double x;double y;int w_id;point(double a = 0 , double b = 0 , int id = -1){x = a , y = b , w_id = id;}}p[maxn];vector<wall> w[maxn];vector<int> block;double dis[maxn][maxn];int n , cnt;void initial(){for(int i = 0;i < maxn;i++){for(int j = 0;j < maxn;j++){dis[i][j] = 1000000;}w[i].clear();}block.clear();p[0] = point(0 , 5 , n+1);cnt = 1;}void readcase(){double x , y1 , y2 , y3 , y4;for(int i = 0;i < n;i++){scanf("%lf%lf%lf%lf%lf" , &x , &y1 , &y2 , &y3 , &y4);w[i].push_back(wall(x , 0.0 , y1));w[i].push_back(wall(x , y2 , y3));w[i].push_back(wall(x , y4 , 10.0));p[cnt++] = point(x , y1 , i);p[cnt++] = point(x , y2 , i);p[cnt++] = point(x , y3 , i);p[cnt++] = point(x , y4 , i);}p[cnt++] = point(10 , 5 , n+2);}bool check(point p1 , point p2){double k = (p2.y-p1.y)/(p2.x-p1.x);double b = p1.y - k*p1.x;for(int i = 0;i < block.size();i++){if(p1.w_id != block[i]){double y = k*w[block[i]][0].x+b;for(int j = 0;j < w[block[i]].size();j++){if(w[block[i]][j].y1 - y < 0 && w[block[i]][j].y2 - y > 0){return false;}}}}return true;}double d(point p1 , point p2){return (p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y);}void get_dis(){block.clear();for(int i = 0;i < cnt;i++){int t1 = p[i+1].w_id;for(int j = i+1;j < cnt;j++){if(p[j].w_id != t1){block.push_back(t1);t1 = p[j].w_id;}if(p[i].w_id != p[j].w_id && check(p[i] , p[j])){dis[i][j] = sqrt(d(p[i] , p[j]));}}block.clear();}}void floyd(){        for(int k = 0;k < cnt;k++){     for(int i = 0;i < cnt;i++){for(int j = 0;j < cnt;j++){if(dis[i][j] > dis[i][k]+dis[k][j]){dis[i][j] = dis[i][k]+dis[k][j];}}}}}void computing(){get_dis();floyd();printf("%.2lf\n" , dis[0][cnt-1]);}int main(){while(cin >> n && n != -1){initial();readcase();computing();}return 0;}


原创粉丝点击