• POJ-2502 Subway( 最短路 )


    题目链接:http://poj.org/problem?id=2502

    Description

    You have just moved from a quiet Waterloo neighbourhood to a big, noisy city. Instead of getting to ride your bike to school every day, you now get to walk and take the subway. Because you don't want to be late for class, you want to know how long it will take you to get to school.
    You walk at a speed of 10 km/h. The subway travels at 40 km/h. Assume that you are lucky, and whenever you arrive at a subway station, a train is there that you can board immediately. You may get on and off the subway any number of times, and you may switch between different subway lines if you wish. All subway lines go in both directions.

    Input

    Input consists of the x,y coordinates of your home and your school, followed by specifications of several subway lines. Each subway line consists of the non-negative integer x,y coordinates of each stop on the line, in order. You may assume the subway runs in a straight line between adjacent stops, and the coordinates represent an integral number of metres. Each line has at least two stops. The end of each subway line is followed by the dummy coordinate pair -1,-1. In total there are at most 200 subway stops in the city.

    Output

    Output is the number of minutes it will take you to get to school, rounded to the nearest minute, taking the fastest route.

    Sample Input

    0 0 10000 1000
    0 200 5000 200 7000 200 -1 -1 
    2000 600 5000 600 10000 600 -1 -1

    Sample Output

    21

    已知家和学校的坐标,并给出若干条地铁线,以及步行速度和地铁速度,求从家到学校最少需要多少时间
    题目的难点在于输入和建图,由于输入的点最多200个,我们可以直接读入点的坐标,生成一个邻接矩阵,而同一地铁线上的各个站点,就可以在输入是直接存入邻接矩阵了,为了便于松弛,邻接矩阵中我们存入路径的用时,注意速度的单位是km/h,而路程的单位是m
    建好图之后,就是一个简单的最短路问题了

     1 #include<iostream>
     2 #include<cstring>
     3 #include<cmath>
     4 #include<algorithm>
     5 #include<map>
     6 #include<cstdio>
     7 #include<queue>
     8 #include<stack>
     9 
    10 using namespace std;
    11 
    12 const int INF = 0x3f3f3f3f;
    13 const int MAXN = 205;
    14 const double wsp = 10 * 1000 / 60;
    15 const double ssp = 40 * 1000 / 60;
    16 
    17 struct Node{
    18     double x, y;
    19 }node[MAXN];
    20 
    21 struct ff{
    22     int x, d;
    23     ff(){}
    24     ff( int a, double b ){ x = a; d = b; }
    25     bool operator <( const ff & a )const{
    26         return d > a.d;
    27     }
    28 };
    29 
    30 int cnt;
    31 double cost[MAXN][MAXN];
    32 double dis[MAXN];
    33 
    34 double gdis( int pre, int pos ){
    35     double dx = node[pre].x - node[pos].x;
    36     double dy = node[pre].y - node[pos].y;
    37     return sqrt( dx * dx + dy * dy );
    38 }
    39 
    40 void dij(){
    41     for( int i = 1; i < MAXN; i++ )
    42         dis[i] = INF;
    43     dis[1] = 0;
    44 
    45     priority_queue<ff> Q;
    46     Q.push( ff( 1, dis[1]) );
    47 
    48     while( !Q.empty() ){
    49         ff temp = Q.top(); Q.pop();
    50         int x = temp.x;
    51         if( temp.d > dis[x] ) continue;
    52         for( int i = 1; i < cnt; i++ ){
    53             if( dis[i] > dis[x] + cost[x][i] ){
    54                 dis[i] = dis[x] + cost[x][i];
    55                 Q.push( ff( i, dis[i] ) );
    56             }
    57         }
    58     }
    59 }
    60 
    61 int main(){
    62     ios::sync_with_stdio( false );
    63 
    64     for( int i = 0; i < MAXN; i++ )
    65         for( int j = 0; j < MAXN; j++ )
    66             cost[i][j] = INF;
    67     
    68     cin >> node[1].x >> node[1].y >> node[2].x >> node[2].y;
    69     cnt = 3;
    70 
    71     while( cin >> node[cnt].x >> node[cnt].y ){
    72         cnt++;
    73         while( cin >> node[cnt].x >> node[cnt].y, !( node[cnt].x == -1 && node[cnt].y == -1 ) ){
    74             cost[cnt][cnt - 1] = cost[cnt - 1][cnt] = gdis( cnt - 1, cnt ) / ssp;
    75             cnt++;
    76         }
    77     }
    78 
    79     for( int i = 1; i < cnt - 1; i++ ){
    80         cost[i][i] = 0;
    81         for( int j = i + 1; j < cnt; j++ ){
    82             cost[i][j] = cost[j][i] = min( cost[i][j], gdis( i, j ) / wsp );
    83         }
    84     }
    85 
    86     dij();
    87 
    88     cout << int( dis[2] + 0.5 );
    89 }
  • 相关阅读:
    ECMAScript 6教程 (二) 对象和函数
    ECMAScript 6教程 (一)
    MongDB 批量更新
    Discuz模版与插件 安装时提示“对不起,您安装的不是正版应用...”解决方法
    命名空间“System.Web.Mvc”中不存在类型或命名空间名称“Ajax”(是否缺少程序集引用?)
    解决浮层弹出如何加上datepicker,并且浮动在上面
    Jquery DataTables warning : Requested unknown from the data source for row 0
    jquery.dataTables插件使用例子详解
    MVC @Html.DropDownListFor 默认值
    初探 ref 和 out
  • 原文地址:https://www.cnblogs.com/hollowstory/p/5677084.html
Copyright © 2020-2023  润新知