【POJ 2502】Subway(最短路dij)
来源:互联网 发布:赤月传说2神翼数据 编辑:程序博客网 时间:2024/05/18 09:20
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
题目大意
题目大意就是说一个人走路的时速是10km/h,坐动车的话是40km/h,现在问你从(x,y)到达(posx,posy)需要花费的最小时间(单位为分钟)。每当输入-1 -1时表示一条线路的所有站点输入结束。
思路
这题主要难在输入的处理,需要计算两点间的距离,同时又要注意只有处在同一条线路上的相邻动车站之间才能到达,在建边的时候要特别注意这一点。对于多组输入可以用while来实现,单位可以在计算前先统一转换。这些处理好后就是一个dij的模版题了。因为对于这种卡输入的题我表示并不想做,所以就网上copy了一份代码,大家可以参考。
代码
#include<stdio.h>#include<math.h>#include<string.h>#define min(a,b) (a<b?a:b)#define INF 0x3f3f3f3f#define MAXV 205typedef struct{ int x,y;}Point;double map[MAXV][MAXV],d[MAXV];int v[MAXV],n;Point point[MAXV],a;int exist(Point t);float distance(Point a,Point b);void Dijkstra();int main(){ int subway[MAXV],k;//subway表示地铁上的第n个点 for(int i=1;i<=MAXV;i++) for(int j=1;j<=MAXV;j++) map[i][j]=(i==j?0:INF); scanf("%d%d%d%d",&point[1].x,&point[1].y,&point[2].x,&point[2].y); n=2; while(~scanf("%d%d",&a.x,&a.y)) { k=0; subway[k++]=exist(a); while(scanf("%d%d",&a.x,&a.y)&&a.x!=-1&&a.y!=-1) subway[k++]=exist(a); for(int i=1;i<k;i++) map[subway[i]][subway[i-1]]=map[subway[i-1]][subway[i]]=distance(point[subway[i]],point[subway[i-1]])*60.0/40000.0;//40 km/h= 40000/60 m/min } for(int i=1;i<=n;i++) for(int j=1;j<=i;j++) map[i][j]=map[j][i]=min(map[i][j],distance(point[i],point[j])*60.0/10000.0);//10 km/h= 10000/60 m/min Dijkstra(); return 0;}int exist(Point t)//返回值为第几个点 { int i; for(i=1;i<=n;i++) if(t.x==point[i].x&&t.y==point[i].y) break; if(i==n+1) point[++n]=t; return i;}float distance(Point a,Point b){ return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));}void Dijkstra(){ for(int i=1;i<=n;i++) d[i]=map[1][i]; memset(v,0,sizeof(v)); for(int i=1;i<=n;i++) { int x,m=INF; for(int y=1;y<=n;y++) if(!v[y]&&d[y]<m) m=d[x=y]; v[x]=1; for(int y=1;y<=n;y++) d[y]=min(d[y],d[x]+map[x][y]); } printf("%d\n",int(d[2]+0.5));}
- 【POJ 2502】Subway(最短路dij)
- poj 2502 Subway(dijkstra 最短路)
- poj 2502 Subway (最短路 Dijksta)
- POJ 2502 Subway(最短路)
- poj 2502 subway (最短路)
- [POJ 2502]Subway[最短路]
- poj 2502最短路subway
- POJ 2502 Subway 最短路 Floyd
- POJ-2502 Subway(最短路 Dijkstra)
- Subway(最短路)
- poj 3439(dij最短路)
- POJ 2502 - Subway(单源最短路)
- POJ 2502 Subway 单源点最短路模板
- POJ 2502 Subway (Dijkstra 最短路+建图)
- 【POJ 1797】Heavy Transportation(最短路dij)
- POJ 2502 Subway ACM解题报告 (dijkstra求最短路)
- POJ 2502 Subway(将各种数据转化成图+最短路+迪杰斯特拉算法)
- poj 3463 Sightseeing (dij 求最短路和次短路并计数)
- (OK)(OK) install-docker.txt
- [转]快速幂(C语言实现) 超详细
- HDU 1394 Minimum Inversion Number(用规律取代线段树)
- awk 命令在处理文本中的应用
- ListView的侧滑demo
- 【POJ 2502】Subway(最短路dij)
- PAT-B 1006. 换个格式输出整数
- The True Cost of Calls
- leetcode题集——sudoku-solver
- HDD is Outdated Technology
- 训练之DP-Doing homework
- 读取.csv数据/写入另外一个.csv (Python)
- java链表的数据结构和二叉树的实现
- (OK)(OK) Install Docker on Fedora 23