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.
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;}
- poj 1556(floyd)
- POJ 1847 Floyd应用
- poj 1125 floyd算法
- POJ 2240 Arbitrage (Floyd)
- POJ 1125 Frogger (Floyd)
- poj 2240Arbitrage(Floyd)
- 【floyd】poj 3615
- POJ 2240 Arbitrage Floyd
- POJ 2253 Frogger Floyd
- POJ 1125 floyd水题
- POJ 1161 Floyd 构图
- POJ 2240 Floyd
- POJ-1125 FLOYD
- POJ 3613 floyd+倍增
- poj 1847 floyd
- poj 1603 floyd
- poj 1570 floyd
- POJ 1125 floyd算法
- Spring.net 在mvc中的用法
- 初探js特效魅力之延时提示框07
- 最长递增子序列(LIS)
- 权限管理系统--Bootstrap框架/JasigCAS单点登录/Dubbo接口授权
- git使用ssh密钥
- poj 1556(floyd)
- 树的层次遍历
- eclipse配置Android sdk 时,网址:http://dl-ssl.google.com/android/ 无法连接解决办法
- 获取IP地址
- 基于Tomcat和Oracle的连接池技术的Jdbc连接
- 平衡树(AVL)详解
- android系统目录各文件简介
- hibernate之及时加载
- 关于align属性