• Subway POJ


    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

    题意:在xy轴第一区域上,一个人要从家去学校,途中他可以步行,也可以坐地铁,步行的速度是10km/h,地铁的速度是40km/h,坐标轴的单位距离是米,问他从家到学校最短要用
    多少分钟时间。最多有200个地铁站点。输入第一行为四个数,是家和学校的坐标,然后往下每行为一条地铁线的坐标,每条地铁线的站点以-1,-1为输入结束。

    思路:将每个站点比喻成一个点,点之间用时间连接,不能连接的点用无穷大表示,家和学校作为第一第二个点,其他地铁站点从三开始标记,最后用Dijkstra算法算出1到2之间的最短
    时间按即可。注意结果为四舍五入的整数。

    代码:
      1 #include <cstdio>
      2 #include <fstream>
      3 #include <algorithm>
      4 #include <cmath>
      5 #include <deque>
      6 #include <vector>
      7 #include <queue>
      8 #include <string>
      9 #include <cstring>
     10 #include <map>
     11 #include <stack>
     12 #include <set>
     13 #include <sstream>
     14 #include <iostream>
     15 #define mod 998244353
     16 #define eps 1e-6
     17 #define ll long long
     18 #define INF 0x3f3f3f3f
     19 using namespace std;
     20 
     21 //结构体存放点的坐标
     22 struct node
     23 {
     24     int x,y;
     25 };
     26 //存放点的信息
     27 node no[3000];
     28 //点之间的信息
     29 double ma[310][310];
     30 //ans代表站点的标记
     31 int ans;
     32 //各点到起点之间的距离
     33 double dis[210];
     34 //是否为最短点标记
     35 int vis[210];
     36 void dijkstra()
     37 {
     38     //初始dis数组
     39     for(int i=1;i<=ans;i++)
     40     {
     41         dis[i]=ma[1][i];
     42     }
     43     //初始化
     44     memset(vis,0,sizeof(vis));
     45     vis[1]=1;
     46     for(int i=1;i<ans;i++)
     47     {
     48         double mi=INF;
     49         int k;
     50         //寻找最短路
     51         for(int j=1;j<=ans;j++)
     52         {
     53             if(dis[j]<mi&&!vis[j])
     54             {
     55                 mi=dis[j];
     56                 k=j;
     57             }
     58         }
     59         //标记
     60         vis[k]=1;
     61         for(int j=1;j<=ans;j++)
     62         {
     63             //判别是否为最短路
     64             if(!vis[j]&&dis[j]>dis[k]+ma[k][j])
     65             {
     66                 dis[j]=dis[k]+ma[k][j];
     67             }
     68         }
     69     }
     70 }
     71 //获取两点之间的距离
     72 double len(node m,node n)
     73 {
     74     return sqrt(double((m.x-n.x)*(m.x-n.x)+(m.y-n.y)*(m.y-n.y)));
     75 }
     76 int main()
     77 {
     78     //初始化ma函数两点之间距离最大
     79     for(int i=0;i<210;i++)
     80     {
     81         for(int j=0;j<210;j++)
     82         {
     83             if(i==j)
     84             {
     85                 ma[i][j]=0;
     86             }
     87             else
     88             {
     89                 ma[i][j]=ma[j][i]=INF;
     90             }
     91 
     92         }
     93     }
     94     //输入第一个点和第二个点
     95     scanf("%d %d %d %d",&no[1].x,&no[1].y,&no[2].x,&no[2].y);
     96     int a,b,bj;
     97     //从第三个点开始存放地铁的信息;
     98     //bj为当前地铁的初始站点的信息
     99     bj=ans=3;
    100     while(cin>>a>>b)
    101     {
    102         //如果为-1,则代表地铁站点输入玩了,接着存放下一个地铁站点
    103         if(a==-1&&b==-1)
    104         {
    105             bj=ans;
    106             continue;
    107         }
    108         //存放信息
    109         no[ans].x=a;
    110         no[ans].y=b;
    111         //如果bj不等于ans表示当前ans站点与第bj站点是一个地铁线的;
    112         if(bj!=ans)
    113         {
    114             //存放地铁线之间的距离
    115             //以地铁速度存放
    116             ma[ans][ans-1]=ma[ans-1][ans]=len(no[ans],no[ans-1])*3/2000;
    117         }
    118         ans++;
    119     }
    120     //遍历存放素有站点之间的关系
    121     //以步行速度存放
    122     for(int i=1;i<=ans;i++)
    123     {
    124         for(int j=i+1;j<=ans;j++)
    125         {
    126             ma[i][j]=ma[j][i]=min(ma[i][j],len(no[i],no[j])*3/500);
    127         }
    128     }
    129     //求最短路
    130     dijkstra();
    131     //对结果四舍五入
    132     int s=dis[2]+0.5;
    133     printf("%d
    ",s);
    134 }
  • 相关阅读:
    ssh-keygen的使用方法(无密码访问)
    ubuntu solute two different terminals cmd
    ubuntu 查看系统是32位还是64位
    pyplot 绘图与可视化
    python 正则表达式的处理
    python&pandas 与mysql 连接
    Ubuntu 11.10 H3C iNode 客户端安装
    Vijos1055(极大子矩阵)
    Vijos1055(极大子矩阵)
    luoguP2701 [USACO5.3]巨大的牛棚Big Barn(极大子矩阵)
  • 原文地址:https://www.cnblogs.com/mzchuan/p/11527295.html
Copyright © 2020-2023  润新知