UVa 821 网页跳跃(Floyd)

https://vjudge.net/problem/UVA-821

题意:
给出一个有向图,任意两点都可相互到达,求任意两点的最短距离的平均值。

思路:
求两点的最短距离,用Floyd算法很方便,最后加起来算个平均值即可。

 1 #include<iostream> 
 2 #include<algorithm>
 3 #include<cstring>
 4 using namespace std;
 5 
 6 const int INF = 1000;
 7 
 8 int x, y;
 9 int d[105][105];
10 
11 void Floyd()
12 {
13     for (int k = 1; k <= 100;k++)
14     for (int i = 1; i <= 100;i++)
15     for (int j = 1; j <= 100; j++)
16         d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
17 }
18 
19 int main()
20 {
21     //freopen("D:\txt.txt", "r", stdin);
22     int kase = 0;
23     while (cin >> x >> y && (x!=0 || y!=0))
24     {
25         for (int i = 1; i <= 100;i++)
26         for (int j = 1; j <= 100;j++)
27         if (i == j)  d[i][j] = 0;
28         else d[i][j] = INF;
29 
30         do
31         {
32             d[x][y] = 1;
33         } while (cin >> x >> y && (x!=0 || y!=0));
34         Floyd();
35         double ans = 0;
36         int count = 0;
37         for (int i = 1; i <= 100;i++)
38         for (int j = 1; j <= 100; j++)
39         {
40             if (i!=j && d[i][j] < INF)
41             {
42                 count++;
43                 ans += d[i][j];
44             }
45         }
46         printf("Case %d: average length between pages = %.3f clicks
", ++kase, ans / count);
47     }
48 }
原文地址:https://www.cnblogs.com/zyb993963526/p/6502971.html