最短路(Floyd_Warshall) POJ 2253 Frogger
1 /*
2 最短路:Floyd算法模板题
3 */
4 #include <cstdio>
5 #include <iostream>
6 #include <algorithm>
7 #include <cmath>
8 #include <cstring>
9 #include <string>
10 #include <vector>
11 using namespace std;
12
13 const int MAXN = 200 + 10;
14 const int INF = 0x3f3f3f3f;
15 double d[MAXN][MAXN];
16 int used[MAXN];
17 struct NODE
18 {
19 int x, y;
20 }node[MAXN];
21
22 double dis(int j, int i)
23 {
24 return sqrt ((node[j].x - node[i].x) * (node[j].x - node[i].x) + (node[j].y - node[i].y) * (node[j].y - node[i].y));
25 }
26
27 void Floyd_Warshall(int n)
28 {
29 for (int k=1; k<=n; ++k)
30 {
31 for (int i=1; i<=n-1; ++i)
32 {
33 for (int j=i+1; j<=n; ++j)
34 {
35 if (d[i][k] < d[i][j] && d[k][j] < d[i][j])
36 {
37 if (d[i][k] < d[k][j])
38 d[i][j] = d[j][i] = d[k][j];
39 else
40 d[i][j] = d[j][i] = d[i][k];
41 }
42 }
43 }
44 }
45
46 printf ("Frog Distance = %.3f\n", d[1][2]);
47 }
48
49 int main(void) //POJ 2253 Frogger
50 {
51 //freopen ("D.in", "r", stdin);
52
53 int n;
54 int cnt = 0;
55 int first = 1;
56 while (~scanf ("%d", &n) && n)
57 {
58 for (int i=1; i<=n; ++i)
59 {
60 scanf ("%d%d", &node[i].x, &node[i].y);
61 }
62 for (int i=1; i<=n-1; ++i)
63 {
64 for (int j=i+1; j<=n; ++j)
65 {
66 d[i][j] = d[j][i] = dis (i, j);
67 }
68 }
69
70 printf ("Scenario #%d\n", ++cnt);
71 Floyd_Warshall (n);
72 puts ("");
73 }
74
75 return 0;
76 }
77
78
79
80
81 /*
82 Scenario #1
83 Frog Distance = 5.000
84
85 Scenario #2
86 Frog Distance = 1.414
87 */
编译人生,运行世界!