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 }
时间: 2024-12-28 05:09:15

poj 2502 Subway的相关文章

POJ 2502 SUBWAY(最短路)

POJ 2502 SUBWAY 题目链接:http://poj.org/problem?id=2502 题目大意:求从a点到b点所需要的最短时间. 题目思路:用最短路来求,把各个点之间的时间看作所需要的路程.然后用 dij求最短路就可以了,感觉输入有点坑,还有在每条地铁线上,只有相同地铁线上的 点可以互相到达. #include<stdio.h> #include<algorithm> #include<math.h> using namespace std; cons

POJ 2502 Subway(最短路径)

原题地址:http://poj.org/problem?id=2502 Subway Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7347   Accepted: 2387 Description You have just moved from a quiet Waterloo neighbourhood to a big, noisy city. Instead of getting to ride your bi

POJ 2502 Subway(迪杰斯特拉)

Subway Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6692   Accepted: 2177 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

POJ 2502 Subway (Dijkstra 最短路+建图)

Subway Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6689   Accepted: 2176 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

(简单) POJ 2502 Subway,Dijkstra。

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

Dijkstra+计算几何 POJ 2502 Subway

题目传送门 题意:列车上行驶40, 其余走路速度10.问从家到学校的最短时间 分析:关键是建图:相邻站点的速度是40,否则都可以走路10的速度.读入数据也很变态. #include <cstdio> #include <cmath> #include <algorithm> #include <cstring> #include <queue> using namespace std; const int N = 2e2 + 5; const i

[有向树的最小表示] poj 1635 Subway tree systems

题目链接: http://poj.org/problem?id=1635 Subway tree systems Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6541   Accepted: 2747 Description Some major cities have subway systems in the form of a tree, i.e. between any pair of stations, th

Subway poj 2502

http://poj.org/problem?id=2502 题意:在一个城市里,分布着若干条地铁线路,每条地铁线路有若干个站点,所有地铁的速度均为40km/h.现在你知道了出发地和终点的坐标,以及这些地铁 线路每个站点的坐标,你的步行速度为10km/h,且你到了地铁的任意一个站之后就刚好有地铁出发.问你从出发点到终点最少需要多少时间. #include<stdio.h> #include<vector> #include<queue> #include<algo

L - Subway - POJ 2502

题意:在一个城市里,分布着若干条地铁线路,每条地铁线路有若干个站点,所有地铁的速度均为40km/h.现在你知道了出发地和终点的坐标,以及这些地铁 线路每个站点的坐标,你的步行速度为10km/h,且你到了地铁的任意一个站之后就刚好有地铁出发.问你从出发点到终点最少需要多少时间. ////////////////////////////////////////////////////////////////////// 有很多站点,给的数据只是一条条线路,所以需要先预处理数据,增加任意两点人走需要的