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;
}

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

  1. POJ 1062 昂贵的聘礼 (最短路 迪杰斯特拉 )

    题目链接 Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请 ...

  2. Poj 3268 Silver cow party 迪杰斯特拉+反向矩阵

    Silver cow party 迪杰斯特拉+反向 题意 有n个农场,编号1到n,每个农场都有一头牛.他们想要举行一个party,其他牛到要一个定好的农场中去.每个农场之间有路相连,但是这个路是单向的 ...

  3. C#迪杰斯特拉算法

    C#迪杰斯特拉算法 网上有许多版本的,自己还是写一个理解点 Dijkstra.cs public class Dijkstra { private List<Node> _nodes; p ...

  4. C++迪杰斯特拉算法求最短路径

    一:算法历史 迪杰斯特拉算法是由荷兰计算机科学家狄克斯特拉于1959 年提出的,因此又叫狄克斯特拉算法.是从一个顶点到其余各顶点的最短路径算法,解决的是有向图中最短路径问题.迪杰斯特拉算法主要特点是以 ...

  5. 【算法杂谈】LJX的迪杰斯特拉算法报告

    迪杰斯特拉(di jie qi)算法 这里有一张图: 假设要求从1号节点到5号节点的最短路.那么根据迪杰斯特拉算法的思想,我们先看: 节点1,从节点1出发的一共有3条路,分别是1-6.1-3.1-2. ...

  6. C# 迪杰斯特拉算法 Dijkstra

    什么也不想说,现在直接上封装的方法: using System; using System.Collections.Concurrent; using System.Collections.Gener ...

  7. 迪杰斯特拉(dijkstra)算法的简要理解和c语言实现(源码)

    迪杰斯特拉(dijkstra)算法:求最短路径的算法,数据结构课程中学习的内容. 1 . 理解 算法思想::设G=(V,E)是一个带权有向图,把图中顶点集合V分成两组,第一组为已求出最短路径的顶点集合 ...

  8. 图-最短路径-Dijktra(迪杰斯特拉)算法

    1. 迪杰斯特拉算法是由荷兰计算机科学家狄克斯特拉算法于1959 年提出的,因此又叫狄克斯特拉算法.是从一个顶点到其余各顶点的最短路径算法,解决的是有向图中最短路径问题.迪杰斯特拉算法主要特点是以起始 ...

  9. 迪杰斯特拉算法——PAT 1003

    本文主要是将我对于我对于迪杰斯特拉算法的理解写出来,同时通过例题来希望能够加深对于算法的理解,其中有错误的地方希望大家指正. 迪杰斯特拉算法 我将这个算法理解成一个局部到整体的算法,这个方法确实越研究 ...

随机推荐

  1. ajax提交请求为啥url要用这个函数encodeURI

    参考如下: 如果你是通过form提交的,那就不需要用这个了.但是如果是你使用url的方式例如:ajax提交到后台的,就需要对url进行encodeURI编码,否则,会导致后台出现各种乱码,不加enco ...

  2. Ecstore内置表单验证?

       

  3. 像table一样布局div的CSS属性详解

    .equal {                     display:table;                     border-collapse:separate;margin: aut ...

  4. linux下查看所有用户及所有用户组

    groups 查看当前登录用户的组内成员groups gliethttp 查看gliethttp用户所在的组,以及组内成员whoami 查看当前登录用户名 /etc/group文件包含所有组/etc/ ...

  5. java日期处理总结(二)

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAzUAAAG1CAIAAABPoU1KAAAgAElEQVR4nOy9e1xU1d747znP9/V9nu

  6. php中curl、fsockopen的应用

    最近要用到通过post上传文件,网上盛传的有curl的post提交和fsockopen,其中curl最简单,于是从最简单的说起. 这是简单的将一个变量post到另外一个页面 $url = ''; $d ...

  7. keepalived + nginx

    本文主要介绍keepalived的安装,Nginx自行解决,也可以使用httpd.随便任何服务都可以... keepalived 官网http://www.keepalived.org/index.h ...

  8. MySQL新建用户,授权,删除用户,修改密码总结

    首先要声明一下:一般情况下,修改MySQL密码,授权,是需要有mysql里的root权限的. 注:本操作是在WIN命令提示符下,phpMyAdmin同样适用. 用户:rdingcn 用户数据库:rdi ...

  9. 【行为型】Interpreter模式

    解释器模式意图为给定的语言定义其文法表示,同时定义该文法表示的一套解释器来解释语言中的句子.该模式说的简单通俗点,其主要用途是用来解释用的.至于解释什么,则要看具体的上下文环境.我们可以为一个表达式专 ...

  10. 创建一个MVC解决方案,添加一个控制器后,运行程序报错:”/"未找到服务器

    1.创建一个MVC项目,如图