码迷,mamicode.com
首页 > 其他好文 > 详细

UVa821 Page Hopping (Floyd)

时间:2016-09-25 18:40:45      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:

链接:http://vjudge.net/problem/UVA-821

分析:利用Floyd求解,将所有有效边距离求和除以边数求平均值(注意:不包含自己到自己)。

 

 

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cstring>
 4 using namespace std;
 5 
 6 const int maxn = 100 + 5;
 7 const int INF = 100000001;
 8 
 9 int d[maxn][maxn];
10 
11 int main() {
12     int u, v, kase = 0;
13     while (scanf("%d%d", &u, &v) == 2 && (u || v)) {
14         for (int i = 0; i < maxn; i++)
15             for (int j = 0; j < maxn; j++) d[i][j] = INF;
16         for (int i = 0; i < maxn; i++) d[i][i] = 0;
17         int n = 0; n = max(n, max(u, v));
18         d[u][v] = 1;
19         while (scanf("%d%d", &u, &v) && (u || v)) { n = max(n, max(u, v)); d[u][v] = 1; }
20         for (int k = 1; k <= n; k++)
21             for (int i = 1; i <= n; i++)
22                 for (int j = 1; j <= n; j++)
23                     d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
24         int sum = 0, cnt = 0;
25         for (int i = 1; i <= n; i++)
26             for (int j = 1; j <= n; j++)
27                 if (d[i][j] < INF && d[i][j])
28                     sum += d[i][j], cnt++;
29         printf("Case %d: average length between pages = %.3lf clicks\n", ++kase, 1.0 * sum / cnt);
30     }
31     return 0;
32 }

 

UVa821 Page Hopping (Floyd)

标签:

原文地址:http://www.cnblogs.com/XieWeida/p/5906339.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!