uva821 Page Hopping( Floyd )

这个点的个数不是最大的编号,而是输入中真实的点的个数

比简单的floyd的题目

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 110;
const int INF = 100000000;
int g[N][N], a, b, id, sum, n[N], num;
int main()
{
    int icase = 1;
    while ( scanf("%d%d", &a, &b ) != EOF && !(!a && !b ) ) {
        id = 0, sum = 0, num = 0;
        memset( n, 0, sizeof(n) );
        for ( int i = 0; i < N; ++i ) for ( int j = i; j < N; ++j ) g[i][j] = g[j][i] = INF;
        id = max( id, max(a, b));
        g[a][b] = 1;
        if ( !n[a] ) num++, n[a] = 1;
        if ( !n[b] ) num++, n[b] = 1;
        while ( scanf("%d%d", &a, &b) && !(!a&&!b)) {
            g[a][b] = 1, id = max( id, max( a, b ) );
            if ( !n[a] ) n[a] = 1, num++;
            if ( !n[b] ) n[b] = 1, num++;
        }
        for ( int k = 1; k <= id; ++k ) 
            for ( int i = 1; i <= id; ++i ) 
                for ( int j = 1; j <= id; ++j ) if ( i != j && j != k && k != i && g[i][k] + g[k][j] < g[i][j] ) g[i][j] = g[i][k] + g[k][j];
        for ( int i = 1; i <= id; ++i )
            for ( int j = 1; j <= id; ++j ) 
                if ( g[i][j] < INF ) sum += g[i][j];
        num *= ( num-1 );
        //printf("%d %d\n", sum, num);
        printf("Case %d: average length between pages = %.3lf clicks\n", icase++, sum/(double)num);
    }
}


你可能感兴趣的:(uva821 Page Hopping( Floyd ))