• poj 1556 The Doors(线段相交,最短路)


     
    The Doors
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 7430   Accepted: 2915

    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

    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

    Source

    【思路】

      枚举所有点,如果不与竖边相交则连边,做最短路即可。

    【代码】

     1 #include<cmath>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 #define FOR(a,b,c) for(int a=(b);a<=(c);a++)
     6 using namespace std;
     7 
     8 const int N = 300+10;
     9 const double INF = 1e9;
    10 const double eps = 1e-8;
    11 
    12 int dcmp(double x) {
    13     if(x<eps) return 0; else return x<0? -1:1;
    14 }
    15 
    16 struct Pt {
    17     double x,y;
    18     Pt(double x=0,double y=0):x(x),y(y) {};
    19 };
    20 struct Seg { Pt a1,a2; };
    21 typedef Pt vec;
    22 
    23 vec operator - (Pt A,Pt B) { return vec(A.x-B.x,A.y-B.y); }
    24 bool operator != (Pt A,Pt B) {
    25     if(dcmp(A.x-B.x)==0 && dcmp(A.y-B.y)==0) return 0;
    26     else return 1; 
    27 }
    28 
    29 double cross(Pt A,Pt B) { return A.x*B.y-A.y*B.x; }
    30 
    31 bool SegInter(Pt s1, Pt e1, Pt s2, Pt e2) {
    32     if( 
    33         cross(e1-s1,s2-s1) * cross(e1-s1,e2-s1) <= 0 &&
    34         cross(e2-s2,s1-s2) * cross(e2-s2,e1-s2) <= 0 
    35       ) return true;
    36     return false;
    37 }
    38 double dist(Pt a,Pt b) {
    39     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
    40 }
    41 double f[N][N];
    42 Seg L[N]; int lc;
    43 Pt P[N]; int pc;
    44 int n;
    45 
    46 int main() {
    47     while(scanf("%d",&n)==1 && n>0) {
    48         pc=lc=0;
    49         FOR(i,1,n) {
    50             double x,y1,y2,y3,y4;
    51             scanf("%lf%lf%lf%lf%lf",&x,&y1,&y2,&y3,&y4);
    52             L[++lc]=(Seg) {Pt(x,0),Pt(x,y1)};
    53             L[++lc]=(Seg) {Pt(x,y2),Pt(x,y3)};
    54             L[++lc]=(Seg) {Pt(x,y4),Pt(x,10)};
    55             P[++pc]=Pt(x,y1) , P[++pc]=Pt(x,y2);
    56             P[++pc]=Pt(x,y3) , P[++pc]=Pt(x,y4);
    57         }
    58         P[++pc]=Pt(0,5), P[++pc]=Pt(10,5);
    59         FOR(i,1,pc) FOR(j,1,pc) f[i][j]=INF;
    60         FOR(i,1,pc) FOR(j,i+1,pc) {
    61             bool flag=1;
    62             FOR(k,1,lc)
    63             if(SegInter(P[i],P[j],L[k].a1,L[k].a2))
    64                 { flag=0; break; }
    65             if(flag)
    66                 f[i][j]=f[j][i]=dist(P[i],P[j]);
    67         }
    68         FOR(i,1,n) {
    69             FOR(j,i+1,n) if(f[i][j]!=INF)
    70                 printf("%d,%d : %.2lf
    ",i,j,f[i][j]);
    71         }
    72         FOR(k,1,pc) FOR(i,1,pc) FOR(j,1,pc)
    73             f[i][j]=min(f[i][j],f[i][k]+f[k][j]);
    74         printf("%.2lf
    ",f[pc-1][pc]);
    75     }
    76     return 0;
    77 }
    View Code
  • 相关阅读:
    mysql 大数据提取
    Python 爬虫实例(11)—— 爬虫 苏宁易购
    Python 爬虫实例(10)—— 四行代码实现刷 博客园 阅读数量
    Python MySQLdb 批量插入 封装
    Python MySQLdb insert(插入) 封装
    Python MySQLdb select(选择) 封装
    iOS开发-iPad侧边栏Tab选项卡切换
    iOS开发-xCode6(iOS 8)中应用程序图标和启动页面设置
    iOS开发-CoreMotion框架(加速计和陀螺仪)
    iOS开发-JSON解析
  • 原文地址:https://www.cnblogs.com/lidaxin/p/5180548.html
Copyright © 2020-2023  润新知