UVA_393_Doors_(线段相交+最短路)

Posted 晴歌。

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVA_393_Doors_(线段相交+最短路)相关的知识,希望对你有一定的参考价值。

描述


https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=5&page=show_problem&problem=329

坐标系,x,y轴都是0~10.起点(0,5),终点(10,5),中间可能有墙,每一堵墙有两个门,给出门的上下定点的坐标,求从起点到终点的最短路.

 

The Doors 

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 file 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

 

1
5 4 6 7 8
2
4 2 7 8 9
7 3 4.5 6 7
-1

 

Sample Output

 

10.00
10.06

分析


在走的时候,如果起点和终点之间没有阻碍,那就直接过去,否则就要绕,假设要绕的门在原路线的上方,绕的话就是先向上走到这个门那里,再向下走,所以走到门的定点就可以了,不需要再走,所有最短路中只会走到门的顶点(想想自己走路绕墙的时候是不是这样...),这样一来把起点,终点,门的两个顶点都当做无向图中的点,两两连边.这时候要去掉和墙相交(严格相交)的边,然后随便用Dijkstra或者Spfa或者Floyd跑一遍最短路即可.

 

注意:

1.不能用小写的vector(如果有vector的头文件的话).

2.在segment_cross_simple函数中括号要特!别!小!心!调了一个多小时...

 

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int maxn=100;
 5 const double oo=~0u>>1,eps=1e-10;
 6 int wall_num,edge_num,point_num;
 7 double d[maxn][maxn];
 8 
 9 struct Point{
10     double x,y;
11     Point(double x=0,double y=0):x(x),y(y){}
12 }p[maxn];
13 typedef Point Vector;
14 Vector operator + (Vector a,Vector b){ return Vector(a.x+b.x,a.y+b.y); }
15 Vector operator - (Vector a,Vector b){ return Vector(a.x-b.x,a.y-b.y); }
16 Vector operator * (Vector a,double p){ return Vector(a.x*p,a.y*p); }
17 Vector operator / (Vector a,double p){ return Vector(a.x/p,a.y/p); }
18 struct edge{
19     double x1,y1,x2,y2;
20     edge(double x1=0,double y1=0,double x2=0,double y2=0):x1(x1),y1(y1),x2(x2),y2(y2){}
21 }g[maxn];
22 void add_point(double x,double y){
23     p[++point_num]=Point(x,y);
24 }
25 void add_edge(double x1,double y1,double x2,double y2){
26     g[++edge_num]=edge(x1,y1,x2,y2);
27 }
28 inline int dcmp(double x){
29     if(fabs(x)<eps) return 0;
30     return x<0?-1:1;
31 }
32 inline double cross(Vector a,Vector b){
33     return a.x*b.y-a.y*b.x;
34 }
35 inline bool segment_cross_simple(Point a,Point b,Point c,Point d){
36 return (dcmp(cross(b-a,c-a))^dcmp(cross(b-a,d-a)))==-2&&(dcmp(cross(d-c,a-c))^dcmp(cross(d-c,b-c)))==-2;
37 }
38 inline double dis(Point a,Point b){
39     return sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2));
40 }
41 void Floyd(){
42     for(int k=1;k<=point_num;k++)
43         for(int i=1;i<=point_num;i++)
44             for(int j=1;j<=point_num;j++)
45                 d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
46 }
47 void outit(){
48     for(int i=1;i<=point_num;i++){
49         for(int j=1;j<=point_num;j++){
50             printf("d[%d][%d]=%.2lf\\t",i,j,d[i][j]);
51         }
52         printf("\\n");
53     }
54 }
55             
56 void solve(){
57     for(int i=1;i<=point_num;i++)
58         for(int j=i+1;j<=point_num;j++){
59             bool link=true;
60             for(int k=1;k<=edge_num;k++){
61                 Point t1=Point(g[k].x1,g[k].y1);
62                 Point t2=Point(g[k].x2,g[k].y2);
63                 if(segment_cross_simple(p[i],p[j],t1,t2)){
64                     link=false;
65                     break;
66                 }
67             }
68             if(link) d[i][j]=d[j][i]=dis(p[i],p[j]);
69         }
70     Floyd();
71     printf("%.2lf\\n",d[1][2]);
72 }
73 int main(){
74     while(scanf("%d",&wall_num)&&wall_num!=-1){
75         point_num=edge_num=0;
76         add_point(0,5);
77         add_point(10,5);
78         for(int i=1;i<=wall_num;i++){
79             double x,y1,y2,y3,y4;
80             scanf("%lf%lf%lf%lf%lf",&x,&y1,&y2,&y3,&y4);
81             add_point(x,y1); add_point(x,y2); add_point(x,y3); add_point(x,y4);
82             add_edge(x,0,x,y1); add_edge(x,y2,x,y3); add_edge(x,y4,x,10);
83         }
84         for(int i=1;i<=point_num;i++){
85             for(int j=1;j<=point_num;j++)
86                 d[i][j]=oo;
87             d[i][i]=0;
88         }
89         solve();
90     }
91     return 0;
92 }
View Code

 

以上是关于UVA_393_Doors_(线段相交+最短路)的主要内容,如果未能解决你的问题,请参考以下文章

[日常摸鱼]UVA393 The Doors 简单计算几何+最短路

POJ-1556 The Doors---线段相交+最短路

POJ 1556The Doors 判断线段相交+SPFA

POJ1556 The Doors 叉积+最短路

POJ 1556 The Doors 线段交 dijkstra

POJ 1556 The Doors(线段交+最短路)