poj 2502 Subway

题目链接:

  http://poj.org/problem?id=2502

题目大意:

  一个学生去上学,从家到学校,可以有若干个地铁路线,每个地铁路线有若干站,给出步行和地铁的速度,问:最短用多长时间从家到达学校?

解题思路:

  有地铁的两点建立地铁路线,没有的步行,求最短路,但是有一点坑的是地铁线有可能是曲线,也就是说从a站到b站再到c站的距离和大于a站直接到c站的距离

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <queue>
 5 #include <cmath>
 6 #include <vector>
 7 #include <iostream>
 8 #include <algorithm>
 9 using namespace std;
10 
11 #define maxn 310
12 #define INF 0x3f3f3f3f
13 #define walk (10000.0/60)
14 #define sub (40000.0/60)
15 double map[maxn][maxn], dist[maxn];
16 int k = 2;
17 
18 void init ();
19 void dijkstra ();
20 
21 int main ()
22 {
23     int i, j, s;
24     double x[maxn], y[maxn];
25 
26     init ();
27     while (scanf ("%lf %lf %lf %lf", &x[0], &y[0], &x[1], &y[1]) != EOF)
28     {
29         while (scanf ("%lf %lf", &x[k], &y[k]) != EOF)
30         {
31             s = k;
32             while (x[k]!=-1 || y[k] != -1)
33             {
34                 k ++;
35                 scanf ("%lf %lf", &x[k], &y[k]);
36             }
37 
38             for (i=s+1; i<k; i++)
39                     map[i-1][i] = map[i][i-1] = sqrt((x[i] - x[i-1])*(x[i] - x[i-1]) + (y[i] - y[i-1])*(y[i] - y[i-1])) / sub;
40 
41         }
42         for (i=0; i<k; i++)
43             for (j=i+1; j<k; j++)
44                 map[i][j] = map[j][i] = min (map[j][i], sqrt((x[i] - x[j])*(x[i] - x[j]) + (y[i] - y[j])*(y[i] - y[j])) / walk);
45         dijkstra ();
46         printf ("%.0f\n", dist[1]);
47     }
48     return 0;
49 }
50 
51 void init ()
52 {
53     for (int i=0; i<maxn; i++)
54         for (int j=0; j<maxn; j++)
55             if (i == j)
56                 map[i][j] = 0;
57             else
58                 map[i][j] = INF;
59 }
60 void dijkstra ()
61 {
62     bool vis[maxn];
63     int temp, index, i, j;
64     memset (vis, false, sizeof(vis));
65     vis[0] = 1;
66     for (i=0; i<k; i++)
67         dist[i] = map[0][i];
68     for (i=1; i<k; i++)
69     {
70         temp = INF;
71         for (j=0; j<k; j++)
72             if (!vis[j] && temp > dist[j])
73             {
74                 temp = dist[j];
75                 index = j;
76             }
77 
78         vis[index] = true;
79 
80         for (j=0; j<k; j++)
81             if (!vis[j])
82                 dist[j] = min(dist[j], dist[index]+map[index][j]);
83     }
84 }

 

posted @ 2015-01-22 22:40  罗茜  阅读(130)  评论(0编辑  收藏  举报