https://vjudge.net/problem/UVA-821
题意:
给出一个有向图,任意两点都可相互到达,求任意两点的最短距离的平均值。
思路:
求两点的最短距离,用Floyd算法很方便,最后加起来算个平均值即可。
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std; const int INF = ; int x, y;
int d[][]; void Floyd()
{
for (int k = ; k <= ;k++)
for (int i = ; i <= ;i++)
for (int j = ; j <= ; j++)
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
} int main()
{
//freopen("D:\\txt.txt", "r", stdin);
int kase = ;
while (cin >> x >> y && (x!= || y!=))
{
for (int i = ; i <= ;i++)
for (int j = ; j <= ;j++)
if (i == j) d[i][j] = ;
else d[i][j] = INF; do
{
d[x][y] = ;
} while (cin >> x >> y && (x!= || y!=));
Floyd();
double ans = ;
int count = ;
for (int i = ; i <= ;i++)
for (int j = ; j <= ; j++)
{
if (i!=j && d[i][j] < INF)
{
count++;
ans += d[i][j];
}
}
printf("Case %d: average length between pages = %.3f clicks\n", ++kase, ans / count);
}
}