关闭

poj 2502 Subway (最短路 Dijksta)

193人阅读 评论(0) 收藏 举报
分类:
Subway
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 8323   Accepted: 2690

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

Source

[Submit]   [Go Back]   [Status]   [Discuss]

Home Page   Go Back  To top

已知人走路的速度是10km/h,地铁的速度是40km/h,题目首先给出一个起点,一个终点,
以及几条地铁线路运行的站点,以-1,-1结束;注意题目给的点的做坐标单位是m,我们要把速度统一为m/min
 
答案输出从起点到终点的时间,到最近的分钟数。
 
10km/h= 10000/60 m/min
40km/h= 40000/60 m/min
 
所有的点直接以步行的速度建边。地铁线路两站相邻的以地铁速度建边

一道比较 变态的 最短路吧~  输入时需要注意;


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
const int N=330;
const double INF=1e30;

struct node
{
    double x,y;
}p[N];

int vis[N];
double dist[N],map[N][N]; ///全部设成double以免出错

double dis(node a,node b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));   ///求两站点之间的距离;
}

void Dijkstra(int n)
{
    int i,j;
    for(i=1;i<=n;i++)
    {
        dist[i]=INF;
        vis[i]=0;
    }
    dist[1]=0;
    for(i=1;i<=n;i++)
    {
        int index=-1;
        double Min=INF;
        for(j=1;j<=n;j++)
        {
           if(!vis[j] && dist[j]<Min)
           {
               Min=dist[j];
               index=j;
           }
        }
        if(index==-1) break;
        vis[index]=1;
        for(j=1;j<=n;j++)
        {
            if(!vis[j] && dist[j]>dist[index]+map[index][j])
                dist[j]=dist[index]+map[index][j];
        }
    }
    printf("%.0f\n",dist[2]);   ///起点到终点就是 dist[2],1到2的最短路;
}

int main()
{
    double v1=10000.0/60;  先把人和地铁的速度转化一下
    double v2=40000.0/60;
    while(scanf("%lf%lf%lf%lf",&p[1].x,&p[1].y,&p[2].x,&p[2].y)==4)
    {
        int n=2;
        int cot=3;  ///初始化cot=3..... 
        int x,y,i,j;
        for(i=1;i<N;i++)
            for(j=1;j<N;j++)
            {
               if(i==j) map[i][j]=0;
               else map[i][j]=INF;  ///初始化~
            }
        while(scanf("%d%d",&x,&y)==2)  ///结束时记得 按ctrl+Z;
        {
            if(x==-1 && y==-1)
            {
                cot=n+1;  ///控制 相邻的两个站台才可以 坐车
                continue;
            }
            n++;
            p[n].x=x;
            p[n].y=y;
            if(n!=cot)
                map[n][n-1]=map[n-1][n]=min(map[n][n-1],dis(p[n],p[n-1])/v2);
        }
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
                map[i][j]=min(map[i][j],dis(p[i],p[j])/v1);
        }
        Dijkstra(n);
    }
    return 0;
}

0
0
查看评论

最短路练习12/poj/2502 Subway dijkstar,spfa,floyd都可以解决

这道题做的时间不短,花了将近4小时,自己的代码错误一直没找出来。看了题解也找了2小时才找出错误。 发现自己思考问题不全面,并且一旦入坑就很难跳出。(无法摆脱自己的定向思维)(这是个大问题) 题意:现在学生要去学校,学生和学校的坐标第一行给出,接下来每一行都代表一条地铁的轨道,直到-1,-1表明这条地...
  • xiangAccepted
  • xiangAccepted
  • 2017-04-26 15:03
  • 355

poj2502 - Subway

想看更多的解题报告: http://blog.csdn.net/wangjian8006/article/details/7870410              &...
  • wangjian8006
  • wangjian8006
  • 2012-08-16 10:02
  • 2419

POJ 2502 Subway(最短路)

Subway Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6229   Accepted: 2026 Des...
  • yeguxin
  • yeguxin
  • 2014-10-07 19:39
  • 1589

[POJ 2502]Subway[最短路]

题目链接:[POJ 2502]Subway[最短路] 题意分析: 你从A到B上学,可以选择走路或者到地铁站搭乘地铁到达,走路速度10km/h,地铁速度40km/h,你可以任意站点下车上车,问:最快要多少分钟到达学校? 解题思路: 建图求最短路即可。 地铁每条线路相邻站点之间建一条双向边,速度...
  • CatGlory
  • CatGlory
  • 2015-10-23 00:55
  • 380

poj 2502最短路subway

题目大体意思: 有一个人要去学校,一共有两种出行方式。  分别是:1.走路10km/h                2.坐地铁40k...
  • qq_35124363
  • qq_35124363
  • 2016-05-26 11:39
  • 256

poj 2502 subway (最短路)

人走路的速度是10km/h,地铁的速度是40km/h 题目给出一个起点,一个终点, 以及几条地铁线路运行的站点。   题目给的点的做坐标单位是m 把速度统一为m/min   答案输出从起点到终点的时间,分钟数。   10km/h= 10000/60...
  • u013008291
  • u013008291
  • 2015-02-24 21:19
  • 328

Poj 2502 Subway(最短路)

题意:题意好理解,一个人步行的速度为每小时10公里,坐地铁的速度为40公里每小时。首先给出你两个点,分别是起点和终点。然后给出若干条铁路,每个铁路上至少有两个站点(也就是这条起点和终点),然后-1,-1为结束。问你从起点到终点最短需要多长时间。 题解:如何建图是一个问题。数据较小用临界矩阵来存图,...
  • PK__PK
  • PK__PK
  • 2018-01-31 15:21
  • 43

poj 2502 Subway(dijkstra 最短路)

大概题意是学生上学可以坐地铁可以步行,只要走到地铁站就立马能上车做地铁...先给出学生的家和学校的坐标...然后下面都是以-1 -1结尾的一串二维坐标,代表一条地铁线,不同地铁线不能建边,只能与相邻地铁站建边...地铁速度和步行速度给出,问到学校的最短时间是多少.. 以到达时间为边权建边...步行...
  • azx736420641
  • azx736420641
  • 2015-02-05 15:55
  • 314

POJ 2502 Subway 最短路 Floyd

Subway Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8733 Accepted: 2821 DescriptionYou have just moved from a quiet Waterloo...
  • Katapeltes
  • Katapeltes
  • 2016-08-18 17:16
  • 129

【POJ 2502】Subway(最短路dij)

DescriptionYou 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...
  • iceiceicpc
  • iceiceicpc
  • 2016-07-21 21:13
  • 258
    个人资料
    • 访问:91375次
    • 积分:3574
    • 等级:
    • 排名:第10953名
    • 原创:274篇
    • 转载:16篇
    • 译文:0篇
    • 评论:32条
    最新评论