链接: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 }
时间: 2024-10-07 02:20:31