紫书 习题 11-1 UVa 821 (Floyd)
水题, Floyd一遍就完了。
#include<cstdio>
#include<algorithm>
#define REP(i, a, b) for(int i = (a); i < (b); i++)
using namespace std;
const int MAXN = 101;
int d[MAXN][MAXN], n;
int main()
{
int u, v, kase = 0;
while(~scanf("%d%d", &u, &v) && u && v)
{
REP(i, 1, MAXN)
REP(j, 1, MAXN)
d[i][j] = (i == j ? 0 : 1e9);
while(1)
{
d[u][v] = 1;
if(!(~scanf("%d%d", &u, &v) && u && v)) break;
}
REP(k, 1, MAXN)
REP(i, 1, MAXN)
REP(j, 1, MAXN)
d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
int sum = 0, cnt = 0;
REP(i, 1, MAXN)
REP(j, 1, MAXN)
if(d[i][j] != 1e9 && i != j)
{
cnt++;
sum += d[i][j];
}
printf("Case %d: average length between pages = %.3f clicks\n", ++kase, (double)sum / cnt);
}
return 0;
}