思路:
需要注意的地方:一条地铁线路并不一定和样例描述的那样是直的;同一条线路上的两个站点步行可能更快。
实现:
1 #include <iostream> 2 #include <cstdio> 3 #include <cmath> 4 using namespace std; 5 6 const int MAXN = 205, INF = 0x3f3f3f3f; 7 int sx, sy, ex, ey; 8 double dis[MAXN][MAXN]; 9 10 struct node 11 { 12 int x, y; 13 }; 14 node a[MAXN]; 15 16 int square(int x) 17 { 18 return x * x; 19 } 20 21 double cal(int i, int j) 22 { 23 return sqrt((double)square(a[i].x - a[j].x) + (double)square(a[i].y - a[j].y)); 24 } 25 26 int main() 27 { 28 int x, y; 29 cin >> sx >> sy >> ex >> ey; 30 a[0].x = sx; a[0].y = sy; 31 int tmp = 1, cnt = 1; 32 for (int i = 0; i < MAXN; i++) 33 { 34 for (int j = 0; j < MAXN; j++) 35 { 36 dis[i][j] = INF; 37 } 38 } 39 for (int i = 0; i < MAXN; i++) dis[i][i] = 0; 40 while (cin >> x >> y) 41 { 42 if (x == -1 && y == -1) 43 { 44 for (int i = tmp; i < cnt - 1; i++) 45 dis[i][i + 1] = dis[i + 1][i] = cal(i, i + 1) / 40.0; 46 tmp = cnt; 47 continue; 48 } 49 a[cnt++].x = x; a[cnt - 1].y = y; 50 } 51 a[cnt].x = ex; a[cnt].y = ey; 52 for (int i = 0; i <= cnt; i++) 53 { 54 for (int j = i + 1; j <= cnt; j++) 55 { 56 dis[i][j] = dis[j][i] = min(dis[i][j], cal(i, j) / 10.0); 57 } 58 } 59 for (int k = 0; k <= cnt; k++) 60 { 61 for (int i = 0; i <= cnt; i++) 62 { 63 for (int j = 0; j <= cnt; j++) 64 if (dis[i][k] + dis[k][j] < dis[i][j]) 65 dis[i][j] = dis[i][k] + dis[k][j]; 66 } 67 } 68 69 cout << int(dis[0][cnt] / 1000.0 * 60.0 + 0.5) << endl; 70 return 0; 71 }