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. Eclipse 导入项目乱码问题(中文乱码)

    1.编码不对 a.对某文件或某工程更改编码:   鼠标移到工程名或文件名,右键->Properties->Resource->Text file enCoding ->更改编码 ...

  2. SQL从入门到基础 - 02 SQLServer的使用

    一.SQLServer的管理 服务器名称:ICECOA-81DEA7A2.\SQLEXPRESS 1. 数据库->表->字段->主键 2. 编辑表 二.数据类型 1. bit:相当于 ...

  3. C# - 使用 OLEDB读取 excel(不用Excel对象).

    参考: How to read from an Excel file using OLEDB 为了使用方便,我做成了工具类(OledbCommon.cs),好以后使用. 注:连接字符串中,Provid ...

  4. CentOS 7 之几个新特性(转)

    上篇我们讲到默认没有ifconfig是centos7的新特性,所以我特意上网搜索了一下其新特性,找到一篇文章,现转过来. centos最小好化安装没有ifconfig命令 刚安装了centos7.0, ...

  5. 【插件】WordPress缓存最佳组合:DB Cache Reloaded Fix + Hyper Cache

    DB Cache Reloaded Fix是一个出色的WordPress数据库缓存插件,可以大大减少对数据库的请求次数. Hyper Cache 是非常小巧但很强大的WordPress缓存插件,设置简 ...

  6. Swagger 生成 ASP.NET Web API

    使用 Swagger 生成 ASP.NET Web API 在线帮助测试文档 原文:ASP.NET Web API Help Pages using Swagger作者:Shayne Boyer翻译: ...

  7. 复杂事件处理引擎—Esper入门(第二弹)

    说明: 以下内容,可以参考Esper官方网站<Qucik start & Tutorial >(顺序做了部分调整). PS:因为英语水平有限(大学期间刚过CET4的英语小盲童一枚) ...

  8. recovery编译学习笔记

    开始建立我们要编译机器的device目录 ./build/tools/device/mkvendor.sh htc vivo ~/boot.img 需要自己配置的部分: 位置:cm/deviec/品牌 ...

  9. ionic ng-src 在网页显示,但是导出apk在android手机中运行不显示图片

    解决方法参照: http://stackoverflow.com/questions/29896158/load-image-using-ng-src-in-android-ionic-aplicat ...

  10. Android 之 AlertDialog 用户登录

    1:activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/androi ...