UVA821 floyd最短路+暴力
题意:给n条边,求每两个点之间的平均距离;
思路:数据是100条边,用floyd得到每两点之间的最短距离,然后遍历相加除以边的数目;
1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <algorithm> 6 #define N 110 7 #define INF 0x7ffffff 8 #define repu(i,a,b) for(int i=a;i<b;i++) 9 using namespace std; 10 int p[N][N],vis[N][N]; 11 int floyd(int n,int m) 12 { 13 repu(k,n,m) 14 { 15 repu(i,n,m) 16 repu(j,n,m) 17 if(p[i][k] < INF && p[k][j] < INF) 18 { 19 p[i][j] = min(p[i][j],p[i][k] + p[k][j]); 20 } 21 } 22 } 23 int main() 24 { 25 int u,v,kase=1; 26 while(1) 27 { 28 repu(i,0,N) 29 { 30 repu(j,0,N) 31 p[i][j] = INF,vis[i][j] = 0; 32 p[i][i] = 0; 33 } 34 memset(vis,0,sizeof(vis)); 35 scanf("%d%d",&u,&v); 36 if(!u&&!v) 37 break; 38 p[u][v] = 1; 39 vis[u][v] = 1; 40 int minn = INF,maxn = 0; 41 while(1) 42 { 43 scanf("%d%d",&u,&v); 44 if(!u&&!v) 45 break; 46 p[u][v] = 1; 47 if(!vis[u][v]) 48 vis[u][v] = 1; 49 maxn = max(max(v,u),maxn); 50 minn = min(min(v,u),minn); 51 } 52 int sum = 0,t = 0; 53 floyd(minn,maxn+1); 54 repu(i,minn,maxn+1) 55 { 56 repu(j,minn,maxn+1) 57 if(p[i][j]!=INF && i!=j ) 58 t++,sum += p[i][j]; 59 } 60 double q = (double)sum / (double)t; 61 // cout<<t<<endl<<sum<<endl; 62 printf("Case %d: average length between pages = %.3lf clicks\n",kase++,q); 63 } 64 return 0; 65 }
人生就像心电图,想要一帆风顺,除非game-over