Subway
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 6689   Accepted: 2176

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

Waterloo local 2001.09.22



题目链接:

id=2502">http://poj.org/problem?id=2502



题目大意:第一行给出起点和终点坐标,然后每一行是一个地铁线,用坐标表示,以-1 -1表示该条线路输入完成。注意单位是米!

每条线路都是直线双向。地铁时速40km/h,人步行速度10km/h,地铁仅仅能在相邻两站间行使,不能直接从第i站到第i+2站。若该人一到地铁站就有地铁坐,问其从起点到终点的最少须要几分钟



题目分析:此题的输入建图比較麻烦。每条地铁线我们要单独处理。笛卡尔距离 / 地铁速(40km/h)作为边权,处理完每条线,再处理其它点之间的边权,笛卡儿距离 / 人速(10km/h),然后就是裸的最短路问题,用Dijkstra求解,注意3个问题。第1:单位的换算,第2:结果要求四舍五入,第3:无穷大设置为double型!

#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
int const MAX = 405;
int const INF = 100000000.0; struct Node
{
double u, v;
}nd[MAX]; double dis[MAX], e[MAX][MAX];
bool vis[MAX];
int cnt; double get_dis(double x1, double y1, double x2, double y2)
{
return sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
} void Dijkstra(int v0)
{
for(int i = 0; i < cnt; i++)
dis[i] = e[v0][i];
dis[v0] = 0;
vis[v0] = true;
for(int i = 0; i < cnt - 1; i++)
{
double mi = INF;
int u = v0;
for(int j = 0; j < cnt; j++)
{
if(!vis[j] && mi > dis[j])
{
u = j;
mi = dis[j];
}
}
vis[u] = true;
for(int k = 0; k < cnt; k++)
if(!vis[k] && dis[k] > dis[u] + e[u][k])
dis[k] = dis[u] + e[u][k];
}
} int main()
{
memset(vis, false, sizeof(vis));
memset(e, 0, sizeof(e));
scanf("%lf %lf %lf %lf", &nd[0].u, &nd[0].v, &nd[1].u, &nd[1].v);
double u, v;
int tmp = 2;
cnt = 2;
while(scanf("%lf %lf", &u, &v) != EOF)
{
if(u == -1.0 && v == -1.0)
{
for(int i = tmp; i < cnt - 1; i++)
{
double get = get_dis(nd[i].u, nd[i].v, nd[i + 1].u, nd[i + 1].v) / 40000.0;
e[i][i + 1] = e[i + 1][i] = get;
}
tmp = cnt;
continue;
}
nd[cnt].u = u;
nd[cnt++].v = v;
}
for(int i = 0; i < cnt; i++)
for(int j = i + 1; j < cnt; j++)
if(e[i][j] == 0)
e[i][j] = e[j][i] = get_dis(nd[i].u, nd[i].v, nd[j].u, nd[j].v) / 10000.0;
Dijkstra(0);
printf("%d\n", (int)(dis[1] * 60.0 + 0.5));
}

版权声明:本文博客原创文章,博客,未经同意,不得转载。

POJ 2502 Subway (Dijkstra 最短+建设规划)的更多相关文章

  1. POJ 2502 - Subway Dijkstra堆优化试水

    做这道题的动机就是想练习一下堆的应用,顺便补一下好久没看的图论算法. Dijkstra算法概述 //从0出发的单源最短路 dis[][] = {INF} ReadMap(dis); for i = 0 ...

  2. POJ 2502 Subway / NBUT 1440 Subway / SCU 2186 Subway(图论,最短距离)

    POJ 2502 Subway / NBUT 1440 Subway / SCU 2186 Subway(图论,最短距离) Description You have just moved from a ...

  3. Dijkstra+计算几何 POJ 2502 Subway

    题目传送门 题意:列车上行驶40, 其余走路速度10.问从家到学校的最短时间 分析:关键是建图:相邻站点的速度是40,否则都可以走路10的速度.读入数据也很变态. #include <cstdi ...

  4. (简单) POJ 2502 Subway,Dijkstra。

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

  5. poj 2502 Subway【Dijkstra】

    <题目链接> 题目大意: 某学生从家到学校之间有N(<200)条地铁,这个学生可以在任意站点上下车,无论何时都能赶上地铁,可以从一条地铁的任意一站到另一条地跌的任意一站,学生步行速度 ...

  6. POJ 2502 Subway(迪杰斯特拉)

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

  7. POJ 2502 Subway

    Subway Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4928   Accepted: 1602 Descriptio ...

  8. POJ 2502 Subway (最短路)

    Subway 题目链接: http://acm.hust.edu.cn/vjudge/contest/122685#problem/L Description You have just moved ...

  9. POJ 2502 Subway ( 最短路 && 最短路建图 )

    题意 : 给出二维平面上的两个点代表起点以及终点,接下来给出若干条地铁线路,除了在地铁线路上行进的速度为 40km/h 其余的点到点间都只能用过步行且其速度为 10km/h ,现问你从起点到终点的最短 ...

随机推荐

  1. 关于VCL的编写 (一) 如何编写自己的VCL控件

    如何编写自己的VCL控件 用过Delphi的朋友们,大概对Delphi的最喜欢Delphi的不是他的强类型的pascal语法,而是强大的VCL控件,本人就是一位VCL控件的爱好者. VCL控件的开源, ...

  2. cocos2dx 遮罩层 android 手机上 失败

    1.CCClippingNode使用(在模拟器上ok,在手机上不行),实现多个剪切区域 local layer=CCLayerColor:create(ccc4(0,0,0,110))     --/ ...

  3. 对于COM对象使用ComPtr代替传统指针

    对于COM对象来说使用传统指针比较麻烦,还要记得Release()防止内存泄漏,一不小心就会出现各种各样的问题.针对这种问题微软提供了对于COM对象的智能指针ComPtr,这里是官方文档https:/ ...

  4. ZOJ Problem Set - 2563 Long Dominoes 【如压力dp】

    称号:ZOJ Problem Set - 2563 Long Dominoes 题意:给出1*3的小矩形.求覆盖m*n的矩阵的最多的不同的方法数? 分析:有一道题目是1 * 2的.比較火.链接:这里 ...

  5. iOS_18_开关控制器_NavigationController_push道路_数据传输

    最后效果图: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcHJlX2VtaW5lbnQ=/font/5a6L5L2T/fontsize/400/fill ...

  6. 【剑指offer学习】求和为定值的两个数(拓展)

    接着上面一篇文章: http://blog.csdn.net/u013476464/article/details/40651451 接下来我们拓展一下题目,如果数组是乱序的,并且规定数组中的元素所有 ...

  7. Matlab学习------------带有右键菜单的GUI学习实例

    实例步骤: 须要设置UIContextMenu,否则点击右键不显示. 右键点击第一个菜单之后:(在菜单中加入对应的回调函数) function r1_Callback(hObject, eventda ...

  8. 软测试是一个烂摊子?NO——【软测试】

    软测试是一个烂摊子权?我开始也是这么认为的.这充分证明,.我并没有考虑,整个合并没有类似的项目. 前几天跟慕夏交流了怎样做总结,听完她讲的,我開始学着为细节的知识点找联系. 只是今天跟老师一交流,才发 ...

  9. bat(批处理文件)初步 第一篇 基本符号

    近期我使用的一款软件中须要大量的环境变量设置,而我又不想讲这些变量都加入到系统的环境变量中,一方面是由于有一些同名的库文件的版本号却不一样,都 写在系统环境中会相互干扰:还有一方面则是大部分的路径仅仅 ...

  10. CSS——(2)与标准流盒模型

    部分博客<CSS--(1)基础>中简介了CSS的概念和几种用法,如今主要是介绍其的核心内容. 盒子模型 为了理解盒子模型,我们能够先从生活中的盒子入手.盒子是用来放置物品的,内部除了有物品 ...