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 }

时间: 2024-11-02 10:28:18

UVA_393_Doors_(线段相交+最短路)的相关文章

简单几何(线段相交+最短路) POJ 1556 The Doors

题目传送门 题意:从(0, 5)走到(10, 5),中间有一些门,走的路是直线,问最短的距离 分析:关键是建图,可以保存所有的点,两点连通的条件是线段和中间的线段都不相交,建立有向图,然后用Dijkstra跑最短路.好题! /************************************************ * Author :Running_Time * Created Time :2015/10/24 星期六 09:48:49 * File Name :POJ_1556.cpp

POJ_1556_The Doors_判断线段相交+最短路

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)

POJ (线段相交 最短路) The Doors

题意: 一个正方形中有n道竖直的墙,每道墙上开两个门.求从左边中点走到右边中点的最短距离. 分析: 以起点终点和每个门的两个端点建图,如果两个点可以直接相连(即不会被墙挡住),则权值为两点间的欧几里得距离. 然后求起点到终点的最短路即可. 1 #include <cstdio> 2 #include <cmath> 3 #include <cstring> 4 #include <vector> 5 #include <algorithm> 6

HDU 4063 线段与圆相交+最短路

Aircraft Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 980    Accepted Submission(s): 228 Problem Description You are playing a flying game. In the game, player controls an aircraft in a 2D-

poj1066Treasure Hunt(线段相交)

链接 很纠结的找到了所有线段的中点,又很纠结的找到了哪些中点可以直接相连,最后bfs一下求出了最短路.. 1 #include <iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<stdlib.h> 6 #include<vector> 7 #include<cmath> 8 #include<queue

hdu 4606 Occupy Cities(线段相交+最小路径覆盖+二分)

Occupy Cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1178    Accepted Submission(s): 388 Problem Description The Star Wars is coming to an end as the Apple Planet is beaten by the Ban

HDOJ1086-You can Solve a Geometry Problem too(线段相交)

Problem Description Many geometry(几何)problems were designed in the ACM/ICPC. And now, I also prepare a geometry problem for this final exam. According to the experience of many ACMers, geometry problems are always much trouble, but this problem is ve

POJ 2653 Pick-up sticks [线段相交 迷之暴力]

Pick-up sticks Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 12861   Accepted: 4847 Description Stan has n sticks of various length. He throws them one at a time on the floor in a random way. After finishing throwing, Stan tries to fin

poj 3304 直线与线段相交

Segments Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 12161   Accepted: 3847 Description Given n segments in the two dimensional space, write a program, which determines if there exists a line such that after projecting these segments