POJ 2502 Subway 单源点最短路模板
2014-07-30 16:58
393 查看
Subway
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
Sample Output
这题输入麻烦,WA了好多发的原因竟然又是建图建成了有向图,调了一个多小时
,下次一定得吸取教训。
Subway
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 5917 | Accepted: 1915 |
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
这题输入麻烦,WA了好多发的原因竟然又是建图建成了有向图,调了一个多小时
,下次一定得吸取教训。
#include<iostream> #include<cstdio> #include<cmath> #include<algorithm> #include<cstring> #include<string> #include<vector> #include<queue> #include<ctype.h> # pragma comment (linker,"/STACK:16777216") using namespace std; const int MAXN = 220; const int INF = 2100000000; double sx, sy, ex, ey; int n; double mat[MAXN][MAXN]; double lowdis[MAXN]; bool vis[MAXN]; class Point { public: double x, y; }; Point line[MAXN]; Point point[MAXN]; int cnt; void initial() { for(int i = 0; i < MAXN; i++) { for(int j = 0; j < MAXN; j++) mat[i][j] = -1; } memset(vis, 0, sizeof(vis)); } void caldis(int src, int end, Point a, Point b) { double x1 = a.x, y1 = a.y, x2 = b.x, y2 = b.y; mat[src][end] = mat[end][src]= sqrt((x1-x2)*(x1-x2) + (y1-y2)*(y1-y2))/(40.0/3.6); } double caldis(Point a, Point b) { return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y)) /(10.0/3.6); } void calleftdis() { for(int i = 1; i <= n; i++) { for(int j = 1; j <= n; j++) { if(mat[i][j] < 0) { mat[i][j] = mat[j][i]= caldis(point[i], point[j]); } } } } double spfa() { queue<double> q; for(int i = 1; i <= n; i++) { q.push(i); vis[i] = 1; } for(int i = 1; i <= n; i++) lowdis[i] = mat[1][i]; while(!q.empty()) { int cur = q.front(); q.pop(); vis[cur] = 0; for(int i = 1; i <= n; i++) { if(lowdis[cur] + mat[cur][i] < lowdis[i]) { lowdis[i] = lowdis[cur] + mat[cur][i]; if(!vis[i]) q.push(i); } } } return lowdis ; } int main() { //freopen("C:/Users/zts/Desktop/in.txt", "r", stdin); n = 1; while(cin >> sx >> sy >> ex >> ey) { double x, y; cnt = 0; initial(); while(cin >> x >> y) { if(x == -1 && y == -1) { cnt = 0; continue;} line[cnt].x = x; line[cnt].y = y; n++; if(cnt >= 1) caldis(n-1, n, line[cnt-1], line[cnt]); point .x = x; point .y = y; cnt++; } n++; point[1].x = sx; point[1].y = sy; point .x = ex; point .y = ey; calleftdis(); int temp = (int)(spfa()/60.0+0.5); printf("%d\n", temp); } return 0; }
相关文章推荐
- 最短路练习12/poj/2502 Subway dijkstar,spfa,floyd都可以解决
- poj 2502 Subway 最短路
- POJ2502 Subway(最短路Floyed)
- POJ2502---Subway (最短路:spfa or floyed)
- POJ2502 subway(dijkstra以最短时间代替最短路)
- POJ 2502 Subway (最短路)
- POJ 2502 Subway 【最短路之建图为关键】
- POJ 2502 Subway (Dijkstra 最短路+建图)
- poj 2502最短路subway
- POJ-2502 Subway( 最短路 )
- POJ 2502 Subway 最短路 Floyd
- [POJ 2502]Subway[最短路]
- POJ - 2502 SUBWAY(FLOYD算法求解最短路)
- poj 2502 subway (最短路)
- POJ 2502 Subway(Floyd单源最短路)
- POJ 2502 - Subway(单源最短路)
- POJ-2502 Subway(最短路)
- poj 2502 Subway(dijkstra 最短路)
- 【POJ 2502】Subway(最短路dij)
- poj 2502 Subway (最短路 Dijksta)