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. 安卓手机内外SD卡互换

    相信有許多人....有內置sd太小...外置sd(sdcard2或extsd)卻只能放資料.... 一些遊戲或者是影音播放軟體....根本不會去讀外置sd(sdcard2或extsd)..... 記憶 ...

  2. C++重载操作符

    重载的函数操作符,对对象使用起来就像对象是一个函数一样 class A{public:A(int n);int operator()(int n);  //需要一个参数,返回int类型void out ...

  3. php本页面调试报错配置

    ini_set('display_errors', 'On'); ini_set('memory_limit', '64M'); //报错,详细 error_reporting(E_ALL); //不 ...

  4. PHP扩展开发(5) - PHP常量的定义和读取

    1. 定义   //定义PHP常量REGISTER_STRINGL_CONSTANT("SIMPLE_VERSION", PHP_SIMPLE_VERSION, sizeof(PH ...

  5. C#中out的一种用法

    1.当希望方法返回多个值时,声明out 方法很有用. 这样使方法可以有选择地返回值. using System; using System.Collections.Generic; using Sys ...

  6. OpenSSL初瞻及本系列的博文的缘由

    OpenSSL初瞻及本系列的博文的缘由1.为什么要写关于“OpenSSL源码分析与学习笔记”系列博文?非常重要的两个原因是Heartbleed和学校课程.我虽然是一个非常崇尚自学的人但是并不代表我不擅 ...

  7. [SQL注入3]from_sqli_to_shell_II

    [SQL注入1]这关学习盲注 ,这篇还有些东西没理透,后面搞明白了再修改. http://www.pentesterlab.com/exercises/from_sqli_to_shell_II/ 准 ...

  8. 在线安装maven插件问题:Cannot complete the install because one or more required items could not be found.

    用Eclipse在线安装的方式:Help-->Install  New Software 地址输入:http://m2eclipse.sonatype.org/sites/m2e/,列表中打勾勾 ...

  9. php 读取 word

    ---恢复内容开始--- 首先安装com扩展: php.ini php.ini 确保有此语句 [PHP_COM_DOTNET] extension=php_com_dotnet.dll   php.i ...

  10. BZOJ3412: [Usaco2009 Dec]Music Notes乐谱

    3412: [Usaco2009 Dec]Music Notes乐谱 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 35  Solved: 30[Sub ...