zoukankan      html  css  js  c++  java
  • subway(dijkstra以最短时间代替最短路)

    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
     
    思路:
    总而言之,人的速度是10km/h,地铁是40km/h,化出来就500/3 m/min,2000/3 m/min
    把起点作为0号,终点作为最后一个点。
    这题的难点主要是在如何建图上面。
    1、我们先对同一条线路对地铁之间建图。
    2、对不同线路之间对地铁建图
    3、我们对起点到地铁站建图
    4、我们对终点到地铁建图
    最后稍微处理下四舍五入就好了
     1 #include <stdio.h>
     2 #include <algorithm>
     3 #include <iostream>
     4 #include <stdlib.h>
     5 #include <math.h>
     6 #include <stack>
     7 
     8 #define INF 0x3f3f3f3f
     9 using namespace std;
    10 const int MAXN = 320;
    11 
    12 double graph[MAXN][MAXN];
    13 double dist[MAXN];
    14 bool vis[MAXN];
    15 double subx[MAXN],suby[MAXN];
    16 int n;
    17 
    18 stack<int> path;
    19 
    20 void dijistra(int x){
    21     int pos = x;
    22     for(int i = 0; i <= n; i ++){
    23         dist[i] = graph[pos][i];
    24     }
    25     dist[pos]=0;
    26     vis[pos] = true;
    27     for(int i = 1; i <= n; i ++){
    28         double mins = INF;
    29         for(int j = 1; j <= n; j ++){
    30             if(!vis[j] && dist[j] < mins){
    31                 pos = j;
    32                 mins = dist[j];
    33             }
    34         }
    35         vis[pos] = true;
    36         for(int j = 1; j <= n; j ++){
    37             if(!vis[j] && dist[j] > dist[pos] + graph[pos][j]){
    38                 dist[j] = dist[pos] + graph[pos][j];
    39             }
    40         }
    41     }
    42 }
    43 
    44 int main()
    45 {
    46     //freopen("../in.txt","r",stdin);
    47     double sx,sy,ex,ey;
    48     scanf("%lf%lf%lf%lf",&sx,&sy,&ex,&ey);
    49     int i=1;
    50     double x,y;
    51     memset(vis, false, sizeof(vis));
    52     for (int j=0;j<MAXN;j++)
    53         for (int k=0;k<MAXN;k++)
    54             graph[j][k] = INF;
    55     while (~scanf("%lf%lf",&x,&y))
    56     {
    57         subx[i]=x;
    58         suby[i]=y;
    59         i++;
    60         while (~scanf("%lf%lf",&x,&y))
    61         {
    62             if (x == -1 && y == -1)
    63                 break;
    64             double len = sqrt((x-subx[i-1])*(x-subx[i-1])+(y-suby[i-1])*(y-suby[i-1]));
    65             graph[i][i-1] = graph[i-1][i] = min(graph[i][i-1],len*3.0/2000.0);
    66             subx[i] = x;
    67             suby[i] = y;
    68             i++;
    69         }
    70     }
    71     int num = i;
    72     for (i=1;i<num;i++)
    73     {
    74         for (int j=1;j<num;j++)
    75         {
    76             double len = sqrt( (subx[i] - subx[j]) * (subx[i] - subx[j]) + (suby[i] - suby[j]) * (suby[i] - suby[j]) );
    77             graph[i][j] = min(graph[i][j],len*3.0/500.0);
    78         }
    79     }
    80     for (i=1;i<num;i++)
    81         graph[0][i] = graph[i][0] = min((sqrt( (sx - subx[i]) * (sx - subx[i]) + (sy - suby[i]) * (sy - suby[i]) ) * 3.0 / 500.0),graph[i][0]);
    82     for (i=1;i<num;i++)
    83         graph[num][i] = graph[i][num] = min(graph[num][i],(sqrt((ex-subx[i])*(ex-subx[i])+(ey-suby[i])*(ey-suby[i]))* 3.0 / 500.0));
    84     n=num;
    85     dijistra(0);
    86     printf("%d
    ",(int)(dist[num]+0.5));
    87     return 0;
    88 }
  • 相关阅读:
    泛型编程 --迭代器
    cpp输入输出加速
    算法训练 加法运算(指针的一个测试)
    蓝桥杯-基础练习-字母图形
    蓝桥杯-基础练习-特殊回文数
    win10下稍微美观下Git
    mysql8.0以上版本注册驱动并建立数据库的连接公共代码
    idea使用的一些问题解决记录
    单链表逆转(递归指针实现)
    increment/decrement/dereference操作符
  • 原文地址:https://www.cnblogs.com/-Ackerman/p/11296964.html
Copyright © 2011-2022 走看看