POJ 2502 Subway(迪杰斯特拉)

Subway

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6692   Accepted: 2177

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

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

题目大意:给出家和学校坐标表示固定的起点和终点,接下来每一行代表一条地铁线,输入该条地铁线的每个站点的坐标,以-1,-1(不是坐标)结束一行,保证地铁线沿直线,且至少有两站。给出乘地铁和步行的不同速度,求一个人从家到学校用到的最小时间。

解题思路:构建无向图,时间为权值,用迪杰斯特拉算法,求家到学校两个结点最短路径问题。同一条地铁线上两站间的时间先计算出来,输入结束,没有权值任意两以步行的速度计算时间。注意输入格式,以EOF结束。

补充:Dijkstra算法适用于权值都为正的图结构,dist[ i ]数组存储 i 到起点v0 的最短路长度,初始为邻接矩阵eg[v0][i]值无穷大.遍历n-1次找出n-1条最短路。s[i ]数组记录结点是否已确定最小dist[ i ],初始为0,确定后为1,找到的i值赋为u,以u为起点找下一个距离u最近的节点j,更新条件:dist [ j ] = min(dist [ u ]+eg [
v0 ] [ u ],dist [ j ]),保证每个点距离起点的距离最短。如果要记录路径,要用到path数组,如果通过u找到了j,path [ j ]=u记录即可。

代码如下:

#include <cstdio>
#include <cstring>
#include <cmath>
#define INF 100000000.0;
int const maxn=400;
int s[maxn];
double dist[maxn];
double eg[maxn][maxn];
int num;

struct  A
{
    double x,y;
}stop[maxn];
double lenth(double x1,double y1,double x2,double y2)
{
    double a=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);
    return sqrt(a);
}

void Dijkstra(int v0)
{
    int i,j;
    for(i=0;i<num;i++)
    {
        s[i]=0;
        dist[i]=eg[v0][i];
    }
    s[v0]=1;
    for(i=0;i<num-1;i++)
    {
        double min=INF;
        int u;
        for(j=0;j<num;j++)
        {
            if(!s[j] && dist[j]<min)
            {
                u=j;
                min=dist[j];
            }
        }
        s[u]=1;
        for(j=0;j<num;j++)
        {
            if(!s[j] && dist[u]+eg[u][j]<dist[j])
                dist[j]=dist[u]+eg[u][j];
        }
    }
}

int main()
{
    int i,j;
    double x1,x2,y1,y2;
    memset(eg,0,sizeof(eg));
    scanf("%lf%lf%lf%lf",&x1,&y1,&x2,&y2);
	stop[0].x=x1;
	stop[0].y=y1;
	stop[1].x=x2;
	stop[1].y=y2;
    num=2;
    int p=0;
    while(scanf("%lf%lf",&stop[num].x,&stop[num].y)!=EOF)
    {
        if(stop[num].x==-1 && stop[num].y==-1)
        {
            p=0;
            continue;
        }
        if(!p)
        {
            p=1;
            num++;
            continue;
        }
        eg[num-1][num]=eg[num][num-1]=lenth(stop[num-1].x,stop[num-1].y,stop[num].x,stop[num].y)/4000.0;
        num++;
    }
	for(i=0;i<num;i++)
    {
        for(j=i+1;j<num;j++)
            if(eg[i][j]==0 )
                eg[i][j]=eg[j][i]=lenth(stop[i].x,stop[i].y,stop[j].x,stop[j].y)/1000.0;
    }
	Dijkstra(0);
	printf("%d\n",int(0.5+6.0*dist[1]) );
	return 0;
}
时间: 2024-08-09 13:09:11

POJ 2502 Subway(迪杰斯特拉)的相关文章

用迪杰斯特拉算法实现地铁的站点搜索

上一篇文章,利用迪杰斯特拉(dijkstra)算法,实现了无向图的最短路径搜索功能.本篇将以南京地铁为例,用迪杰斯特拉算法实现两个站点之间的最短路径搜索. 借用百度百科,南京2015年4月份的地铁运行线路图如下: 创建一个数据构造类,初始化南京地铁的各条线路站点数据(截至2015年4月南京地铁运营公司数据,与上图对应) /** * */ package com.test.dijkstra; import java.util.ArrayList; import java.util.HashSet;

最短路径算法——迪杰斯特拉算法(Dijkstra)

图结构中应用的最多的就是最短路径的查找了,关于最短路径查找的算法主要有两种:迪杰斯特拉算法(Dijkstra)和Floyd算法. 其中迪杰斯特拉算法(Dijkstra)实现如下: 原理就是不断寻找当前的最优解: void main() { int V[Max][Max]={0,8,32,Infinity,Infinity, 12,0,16,15,Infinity, Infinity,29,0,Infinity,13, Infinity,21,Infinity,0,7, Infinity,Infi

迪杰斯特拉算法——PAT 1003

本文主要是将我对于我对于迪杰斯特拉算法的理解写出来,同时通过例题来希望能够加深对于算法的理解,其中有错误的地方希望大家指正. 迪杰斯特拉算法 我将这个算法理解成一个局部到整体的算法,这个方法确实越研究就会发现越经典. 首先可以将整个图的节点看成两个集合:一个是S,一个是U-S.如果是求v0到图中各点的最短距离的话,那么S就是已经确认到v0距离最短的点,U-S则是对于整体的点集合U,还没有加入S集合的点. 这里提出一个算法总体的思想,将所有的点按照一定的原则加入到S集就是解集.而这个解法就是重点了

数据结构之单源最短路径(迪杰斯特拉算法)-(九)

最开始接触最短路径是在数据结构中图的那个章节中.运用到实际中就是我在大三参加的一次美赛中,解决中国的水资源问题.所谓单源最短路径,就是一个起点到图中其他节点的最短路径,这是一个贪心算法. 迪杰斯特拉算法原理(百科): 按路径长度递增次序产生算法: 把顶点集合V分成两组: (1)S:已求出的顶点的集合(初始时只含有源点V0) (2)V-S=T:尚未确定的顶点集合 将T中顶点按递增的次序加入到S中,保证: (1)从源点V0到S中其他各顶点的长度都不大于从V0到T中任何顶点的最短路径长度 (2)每个顶

最短路 迪杰斯特拉.cpp

<span style="color:#3333ff;">#include<stdio.h> #include<stdlib.h> #define INITITY 999//最大值 #define VERTEX 20//最多顶点个数 #define FALSE 0 #define TURE 1 #define size 30 #define OVERFLOW -1 typedef struct ArcCell{ int adj;//权值类型 }Arc

迪杰斯特拉算法介绍

迪杰斯特拉(Dijkstra)算法是典型最短路径算法,用于计算一个节点到其他节点的最短路径. 它的主要特点是以起始点为中心向外层层扩展(广度优先搜索思想),直到扩展到终点为止. 基本思想 通过Dijkstra计算图G中的最短路径时,需要指定起点s(即从顶点s开始计算). 此外,引进两个集合S和U.S的作用是记录已求出最短路径的顶点(以及相应的最短路径长度),而U则是记录还未求出最短路径的顶点(以及该顶点到起点s的距离). 初始时,S中只有起点s:U中是除s之外的顶点,并且U中顶点的路径是"起点s

hdu 1142(迪杰斯特拉+记忆化搜索)

A Walk Through the Forest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7330    Accepted Submission(s): 2687 Problem Description Jimmy experiences a lot of stress at work these days, especiall

hdu 3339 In Action(迪杰斯特拉+01背包)

In Action Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5102    Accepted Submission(s): 1696 Problem Description Since 1945, when the first nuclear bomb was exploded by the Manhattan Project t

迪杰斯特拉(Dijkstra)算法

1 # include <stdio.h> 2 3 # define MAX_VERTEXES 20//最大顶点数 4 # define INFINITY 65535;//代表∞ 5 6 typedef struct 7 {/* 无向图结构体 */ 8 int vexs[MAX_VERTEXES];//顶点下标 9 int arc[MAX_VERTEXES][MAX_VERTEXES];//矩阵 10 int numVertexes, numEdges;//顶点数和边数 11 12 }MGra