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\n", ++kase, ans / count);
47     }
48 }

 

转载于:https://www.cnblogs.com/zyb993963526/p/6502971.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值