UVA 821(p379)----Page Hopping

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxx=2147483647/3;
int g[155][155],ans,num;
void init()
{
    for(int i=0;i<=100;i++)
        for(int j=0;j<=100;j++)
          g[i][j]=maxx;
}
double solve()
{
    num=0;ans=0;
    for(int k=1;k<=100;k++)
        for(int i=1;i<=100;i++)
          for(int j=1;j<=100;j++)
            g[i][j]=min(g[i][j],g[i][k]+g[k][j]);
    for(int i=1;i<=100;i++)
        for(int j=1;j<=100;j++)
           if(g[i][j]!=maxx&&(i!=j))
           {
               ans+=g[i][j];
               num++;
           }
    return (double)ans/(double)num;
}
int main()
{
    int cas=0,a,b,x,y;
    while((scanf("%d%d",&a,&b)!=EOF)&&a&&b)
    {
        init();g[a][b]=1;cas++;
        while(scanf("%d%d",&x,&y)&&x&&y) g[x][y]=1;
        printf("Case %d: average length between pages = %.3lf clicks\n",cas,solve());
    }
    return 0;
}
题目地址: https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=762

你可能感兴趣的:(UVA 821(p379)----Page Hopping)