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

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

最新文章

  1. codeforces 645 E. Intellectual Inquiry
  2. MySQL SELECT执行顺序
  3. Django中如何使用django-celery完成异步任务2(转)
  4. 蓝牙 GameKit
  5. zoj 3829 Known Notation
  6. 使用Java BigDecimal进行精确运算
  7. jboss学习 - vfs---转载
  8. Codeforces Round #419 (Div. 2)
  9. 关于ZendStudio 10.5的破解
  10. jq实现全选或者全不选
  11. MySQL之索引原理和慢查询优化
  12. Angular6 Observable.fromEvent error: “Invalid event target”
  13. OpenWRT平台搭建及简单应用[转帖]+华为HG255D编译实践(20190323更新)
  14. TCP粘包问题解析与解决
  15. 关于Discuz! X系列UC_Server 本地文件包含漏洞
  16. 那些好用的Chrome 插件
  17. python 将函数参数一键转化成字典的技巧,非**kwargs,公有方法和函数抵制kwargs。
  18. 硬件电路io口控制继电器电路
  19. shell 变量相关的命令
  20. IDEA PYCHARM USAGE NOTE

热门文章

  1. 玩转web之servlet(六)---session介绍及简单使用(登录验证中保存信息)
  2. Linux - SVN下载项目
  3. Git相关操作汇总
  4. 查看linux系统版本号命令
  5. partial 的好处
  6. iOS 7 新特性
  7. Ubuntu Linux中开启MySQL远程访问功能
  8. dom4j解析xml中指定元素下内容
  9. 照片教你eclipse通过使用gradle 打包Android
  10. android利用jdk制作签名