zoukankan      html  css  js  c++  java
  • poj 1556 (Dijkstra + Geometry 线段相交)

    链接:http://poj.org/problem?id=1556

    The Doors
    Time Limit: 1000MS   Memory Limit: 10000K
    Total Submissions: 6216   Accepted: 2495

    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. 


    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



    ////////////////////////////////////////////////////////////////////
    这题处理起来挺难的,要把输入的点存到图里,用Dijkstra求出最短路径,存图的过程是,判断任意两点连成的线,横坐标不能相同,并且如果,横坐标与线上的横坐标不相同,就要判断是否相交,相交则行不通
    否则存图,用Dijkstra搜出最短的路径即可
    还有,要注意细节

      1 #include <stdio.h>
      2 #include <string.h>
      3 #include <stdlib.h>
      4 #include <iostream>
      5 #include <math.h>
      6 #include <algorithm>
      7 
      8 #define eps 1e-6
      9 #define INF 1000000000
     10 typedef struct point
     11 {
     12     double x,y;
     13 }point;
     14 
     15 typedef struct beline
     16 {
     17     point st,ed;
     18 }beline;
     19 
     20 using namespace std;
     21 
     22 point p[1005];
     23 double mp[1005][1005];
     24 double d[1005];
     25 int visit[1005];
     26 
     27 bool  dy(double x,double y){ return x > y+eps; }
     28 bool  xy(double x,double y){ return x < y-eps; }
     29 bool dyd(double x,double y){ return x > y-eps; }
     30 bool xyd(double x,double y){ return x < y+eps; }
     31 bool  dd(double x,double y){ return fabs(x - y)<eps; }
     32 
     33 double crossProduct(point a,point b,point c)
     34 {
     35     return (c.x-a.x)*(b.y-a.y)-(c.y-a.y)*(b.x-a.x);
     36 }
     37 double Dist(point a,point b)
     38 {
     39     return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
     40 }
     41 
     42 bool onSegment(point a,point b,point c)
     43 {
     44     double maxx=max(a.x,b.x);
     45     double maxy=max(a.y,b.y);
     46     double minx=min(a.x,b.x);
     47     double miny=min(a.y,b.y);
     48     if(dd(crossProduct(a,b,c),0.0)&&dy(c.x,minx)&&xy(c.x,maxx)
     49        &&dy(c.y,miny)&&xy(c.y,maxy))
     50         return true;
     51     return false;
     52 }
     53 
     54 bool segIntersect(point p1,point p2,point p3,point p4)
     55 {
     56     double d1 = crossProduct(p3,p4,p1);
     57     double d2 = crossProduct(p3,p4,p2);
     58     double d3 = crossProduct(p1,p2,p3);
     59     double d4 = crossProduct(p1,p2,p4);
     60     if(xy(d1*d2,0.0)&&xy(d3*d4,0.0))
     61         return true;
     62     if(dd(d1,0.0)&&onSegment(p3,p4,p1))
     63         return true;
     64     if(dd(d2,0.0)&&onSegment(p3,p4,p2))
     65         return true;
     66     if(dd(d3,0.0)&&onSegment(p1,p2,p3))
     67         return true;
     68     if(dd(d4,0.0)&&onSegment(p1,p2,p4))
     69         return true;
     70     return false;
     71 }
     72 
     73 void Dijkstra(int n)
     74 {
     75     int i,y;
     76     memset(visit,0,sizeof(visit));
     77     for(i=0; i<n; i++)
     78         d[i] = mp[0][i];
     79     d[0] = 0;
     80     for(i=0; i<n; i++)
     81     {
     82         int m=INF,x;
     83         {
     84             for(y=0; y<n; y++)
     85             {
     86                 if(!visit[y] && d[y]<=m)
     87                 {
     88                     m = d[ x = y ];
     89                 }
     90             }
     91             visit[x]=1;
     92             for(y=0; y<n; y++)
     93             {
     94                 if(!visit[y] && d[y] > d[x]+mp[x][y])
     95                 {
     96                     d[y] = d[x] + mp[x][y];
     97                 }
     98             }
     99         }
    100     }
    101 }
    102 
    103 int main()
    104 {
    105     int n,m,i,j,k,t;
    106     double a,b,c,d1,e;
    107     beline li[10005];
    108     beline tmp;
    109     p[0].x=0;p[0].y=5;//freopen("in.txt","r",stdin);
    110     while(scanf("%d",&n)!=EOF && n!=-1)
    111     {
    112         for(i=0; i<1005; i++)
    113             for(j=0; j<1005; j++)
    114                 mp[i][j] = INF;
    115         int cas=1,css=0;
    116         for(i=0; i<n; i++)
    117         {
    118                scanf("%lf%lf%lf%lf%lf",&a,&b,&c,&d1,&e);
    119                li[css].st.x=a;
    120                li[css].st.y=0;
    121                p[cas].x=a;  li[css].ed.x=a;
    122                p[cas++].y=b;li[css++].ed.y=b;
    123                p[cas].x=a;  li[css].st.x=a;
    124                p[cas++].y=c;li[css].st.y=c;
    125                p[cas].x=a;  li[css].ed.x=a;
    126                p[cas++].y=d1;li[css++].ed.y=d1;
    127                p[cas].x=a;  li[css].st.x=a;
    128                p[cas++].y=e;li[css].st.y=e;
    129                li[css].ed.x=a;
    130                li[css++].ed.y=10;
    131         }
    132         p[cas].x=10.0;p[cas].y=5.0;
    133         for(i=0; i<=cas; i++)
    134         {
    135             for(j=i+1; j<=cas; j++)
    136             {
    137                 int ok=0;
    138                 for(k=0; k<css; k++)
    139                 {
    140                     if(dd(p[i].x,p[j].x)||!dd(p[i].x,li[k].st.x)&&!dd(p[j].x,li[k].st.x)&&(segIntersect(p[i],p[j],li[k].st,li[k].ed)))
    141                     {
    142                         ok=1;
    143                         break;
    144                     }
    145                 }
    146                 if(!ok)
    147                 {
    148                     mp[j][i] = mp[i][j] = Dist(p[i],p[j]);//printf("%d %d %lf ^^
    ",i,j,mp[i][j]);
    149                 }
    150             }
    151         }
    152         Dijkstra(cas+1);
    153         printf("%.2lf
    ",d[cas]);
    154     }
    155     return 0;
    156 }
    View Code
  • 相关阅读:
    mbed TLS 介绍
    PostGIS:Working with Raster Data
    TIN数据格式:DEM的三种表示方法之一
    ArcScene显示DEM
    Python与MapNik 等高线渲染&抽稀
    Android GPS定位
    osmdroid通过点击获取当前坐标
    osmdroid高级教程
    mongodb 用户 权限 设置 详解
    Mongodb设置用户权限(整理版)
  • 原文地址:https://www.cnblogs.com/ccccnzb/p/3893820.html
Copyright © 2011-2022 走看看