poj 2502 Subway (最短路 Dijksta)

原创 2016年05月30日 10:20:36
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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

POJ 2502 Subway ACM解题报告 (dijkstra求最短路)

第一次手搓dijkstra,以为是精度问题结果居然是模板写错(ps.我发四以后再不也写错) 首先这题是有不超过202个点(算上家和学校),距离化成时间来计算会比较方便哦。然后就是要用double型还...

POJ 2502 Subway(Floyd单源最短路)

POJ 2502 Subway(BellmanFord单源最短路) http://poj.org/problem?id=2502 题意:给你你的家坐标和学校坐标,以及多条地铁线路的各个站点坐标.你以1...

POJ 2502 Subway (Dijkstra 最短路+建图)

POJ 2502 Subway (Dijkstra 最短路+建图)

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

这道题做的时间不短,花了将近4小时,自己的代码错误一直没找出来。看了题解也找了2小时才找出错误。 发现自己思考问题不全面,并且一旦入坑就很难跳出。(无法摆脱自己的定向思维)(这是个大问题) 题意:现在...

POJ 2502 Subway 单源点最短路模板

                                                                                               ...
  • tanmlh
  • tanmlh
  • 2014年07月30日 16:58
  • 214

【POJ 2502】Subway(最短路dij)

DescriptionYou have just moved from a quiet Waterloo neighbourhood to a big, noisy city. Instead of ...

poj 2502 subway (最短路)

人走路的速度是10km/h,地铁的速度是40km/h 题目给出一个起点,一个终点, 以及几条地铁线路运行的站点。   题目给的点的做坐标单位是m 把速度统一为m/min   答案输出...

【最短路】POJ2502 SUBWAY (spfa)

subway You have just moved from a quiet Waterloo neighbourhood to a big, noisy city. Instead of gett...

poj 2502 Subway(dijkstra 最短路)

大概题意是学生上学可以坐地铁可以步行,只要走到地铁站就立马能上车做地铁...先给出学生的家和学校的坐标...然后下面都是以-1 -1结尾的一串二维坐标,代表一条地铁线,不同地铁线不能建边,只能与相邻地...

poj 2502最短路subway

题目大体意思: 有一个人要去学校,一共有两种出行方式。  分别是:1.走路10km/h                2.坐地铁40km/h 已知 家、地铁站、学校的坐标。 ...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:poj 2502 Subway (最短路 Dijksta)
举报原因:
原因补充:

(最多只允许输入30个字)