UVAOJ 821 Page Hopping(最短路)

来源:互联网 发布:大数据分析师笔试内容 编辑:程序博客网 时间:2024/06/05 02:46


https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=762



给出一个无向图,要求输出任意两个节点的平均最短路径。

裸Folyd算法,最后遍历图得到平均最短路径,需要用map映射节点。数据太弱了




#include<iostream>#include<cstdio>#include<cstring>#include<string.h>#include<algorithm>#include<cctype>#include<vector>#include<queue>#include<stack>#include<map>#include<set>using namespace  std;#define inf 999999int graph[200][200];int n;map<int,int>m;int temp;void Floyd(){    for(int i=1;i<temp;i++)    {        for(int j=1;j<temp;j++)        {            for(int k=1;k<temp;k++)                graph[j][k]=min(graph[j][i]+graph[i][k],graph[j][k]);        }    }}int main(){    int x,y,cot=0;    while(scanf("%d %d",&x,&y)!=EOF)    {        cot++;        m.clear();       for(int i=0;i<120;i++)       {           for(int j=0;j<120;j++)            graph[i][j]=(i==j)?0:inf;       }        if(x==0&&y==0)break;        temp=1;        if(!m[x])m[x]=temp++;        if(!m[y])m[y]=temp++;        graph[m[x]][m[y]]=1;        while(scanf("%d %d",&x,&y)!=EOF)        {            if(x==0&&y==0)break;            if(!m[x])m[x]=temp++;            if(!m[y])m[y]=temp++;            graph[m[x]][m[y]]=1;        }        Floyd();        int sum=0;        for(int i=1;i<temp;i++)        {            for(int j=1;j<temp;j++)                sum+=graph[i][j];        }        double ans=sum*1.0/(1.0*(temp-1)*(temp-2));        printf("Case %d: average length between pages = %.3lf clicks\n",cot,ans);    }    return 0;}


0 0