UVA821 floyd最短路+暴力

时间:2023-08-24 10:44:08

题意:给n条边,求每两个点之间的平均距离;

思路:数据是100条边,用floyd得到每两点之间的最短距离,然后遍历相加除以边的数目;

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#define N 110
#define INF 0x7ffffff
#define repu(i,a,b) for(int i=a;i<b;i++)
using namespace std;
int p[N][N],vis[N][N];
int floyd(int n,int m)
{
repu(k,n,m)
{
repu(i,n,m)
repu(j,n,m)
if(p[i][k] < INF && p[k][j] < INF)
{
p[i][j] = min(p[i][j],p[i][k] + p[k][j]);
}
}
}
int main()
{
int u,v,kase=;
while()
{
repu(i,,N)
{
repu(j,,N)
p[i][j] = INF,vis[i][j] = ;
p[i][i] = ;
}
memset(vis,,sizeof(vis));
scanf("%d%d",&u,&v);
if(!u&&!v)
break;
p[u][v] = ;
vis[u][v] = ;
int minn = INF,maxn = ;
while()
{
scanf("%d%d",&u,&v);
if(!u&&!v)
break;
p[u][v] = ;
if(!vis[u][v])
vis[u][v] = ;
maxn = max(max(v,u),maxn);
minn = min(min(v,u),minn);
}
int sum = ,t = ;
floyd(minn,maxn+);
repu(i,minn,maxn+)
{
repu(j,minn,maxn+)
if(p[i][j]!=INF && i!=j )
t++,sum += p[i][j];
}
double q = (double)sum / (double)t;
// cout<<t<<endl<<sum<<endl;
printf("Case %d: average length between pages = %.3lf clicks\n",kase++,q);
}
return ;
}