POJ 2502 - Subway(单源最短路)
来源:互联网 发布:淘宝一键复制有坏处吗 编辑:程序博客网 时间:2024/05/22 03:33
Subway
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 9121 Accepted: 2959
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
题意:
给出起点和终点的坐标,还有若干个地铁线。
每个地铁线至少有2个站点,可以步行也可以搭乘地铁。
给出了步行的速度和地铁的速度,问从起点到终点的最短时间。
解题思路:
此题的难点是构造图。
开始对于每条地铁,都记录下相邻的点的时间。
然后对于任意两点都用步行去更新,最后用dijkstra扫一遍即可。
POJ的G++对double类型不友好,要用C++。
AC代码:
#include<stdio.h>#include<math.h>#include<string.h>#include<algorithm>using namespace std;const int maxn = 3e2+5;const double INF = 0x3f3f3f3;const double v1=10000.0/60;const double v2=40000.0/60;struct node {double x,y;}point[maxn];double mp[maxn][maxn];double dis[maxn];bool vis[maxn];double distan(node a,node b) {return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}void dijkstra(int n){ for(int i = 1;i <= n;i++) dis[i] = INF,vis[i] = 0; dis[1] = 0; for(int i = 1;i <= n;i++) { int index = -1;double minn = INF; for(int j = 1;j <= n;j++) if(!vis[j]&&dis[j] < minn) minn = dis[j],index = j; vis[index] = 1; for(int j = 1;j <= n;j++) if(!vis[j] && dis[j] > dis[index]+mp[index][j]) dis[j] = dis[index] + mp[index][j]; } printf("%.0lf\n",dis[2]);}int main(){ for(int i = 1;i < maxn;i++) for(int j = 1;j < maxn;j++) mp[i][j] = i==j?0:INF; scanf("%lf%lf%lf%lf",&point[1].x,&point[1].y,&point[2].x,&point[2].y); int n = 2; int cnt = 3; double a,b; while(~scanf("%lf%lf",&a,&b)) { if(a==-1 && b==-1) { cnt = n+1; continue; } n++; point[n].x = a,point[n].y = b; if(n != cnt) mp[n][n-1] = mp[n-1][n] = min(mp[n][n-1],distan(point[n],point[n-1])/v2); } for(int i = 1;i <= n;i++) for(int j = 1;j <= n;j++) mp[i][j] = mp[j][i] = min(mp[i][j],distan(point[i],point[j])/v1); dijkstra(n); return 0;}
- POJ 2502 - Subway(单源最短路)
- POJ 2502 Subway(Floyd单源最短路)
- poj 2502 Subway(dijkstra 最短路)
- poj 2502 Subway (最短路 Dijksta)
- 【POJ 2502】Subway(最短路dij)
- POJ 2502 Subway(最短路)
- poj 2502 subway (最短路)
- [POJ 2502]Subway[最短路]
- poj 2502最短路subway
- POJ 2502 Subway 最短路 Floyd
- POJ-2502 Subway(最短路 Dijkstra)
- POJ 2502 Subway(迪杰斯特拉)
- POJ 2502 Subway (Djikstra)
- Subway(最短路)
- POJ 2502 Subway ACM解题报告 (dijkstra求最短路)
- POJ 2502 Subway(将各种数据转化成图+最短路+迪杰斯特拉算法)
- POJ 2502 Subway 单源点最短路模板
- POJ 2502 Subway (Dijkstra 最短路+建图)
- LeetCode 387. First Unique Character in a String
- 【矩阵相关】[Codeforces - 736D]Permutations
- APICLOUD平台初体验(一个任务系统的开发)
- linux下IO相关知识
- 报数(类似猴子选大王)
- POJ 2502 - Subway(单源最短路)
- 在不损失数据的情况下如何扩展C盘
- 剑指offer——面试题28:字符串的排列
- matlab—图像显示
- 使用libsvm实现文本分类
- java 访问权限控制
- 入职一周
- springMVC笔记系列(13)——模型数据处理篇 之 ModelAndVIew对象
- 所有OLE接口