Page Hopping UVA - 821
Floyd的模板题
1 #define INF 1e8 2 #include<cstdio> 3 #include<cstring> 4 #include<iostream> 5 #include<algorithm> 6 using namespace std; 7 8 const int maxn=10005; 9 10 int kase,cnt,sum; 11 int d[105][105]; 12 13 void Init(){ 14 cnt=0,sum=0; 15 for(int i=1;i<=101;i++) 16 for(int j=1;j<=101;j++) d[i][j]=INF; 17 } 18 19 void Floyd(){ 20 for(int k=1;k<=100;k++){ 21 for(int i=1;i<=100;i++){ 22 for(int j=1;j<=100;j++){ 23 if(d[i][k]==INF||d[k][j]==INF) continue; 24 d[i][j]=min(d[i][j],d[i][k]+d[k][j]); 25 } 26 } 27 } 28 } 29 30 void solve(){ 31 Floyd(); 32 for(int i=1;i<=100;i++){ 33 for(int j=1;j<=100;j++){ 34 if(i==j||d[i][j]==INF) continue; 35 sum=sum+d[i][j]; 36 cnt++; 37 } 38 } 39 printf("Case %d: average length between pages = %.3f clicks\n",++kase,1.0*sum/cnt); 40 } 41 int main() 42 { kase=0; 43 while(true){ 44 int x,y; 45 scanf("%d%d",&x,&y); 46 if(x==0&&y==0) break; 47 Init(); 48 d[x][y]=1; 49 while(true){ 50 int xx,yy; 51 scanf("%d%d",&xx,&yy); 52 if(xx==0&&yy==0) break; 53 d[xx][yy]=1; 54 } 55 solve(); 56 } 57 return 0; 58 }