Test instructions
Gives a graph of the average value of all paths (shortest path between two points).
Analysis:
Use Floyd to find the shortest distance between two points, then the average is good.
Code:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace Std;
int dist[101][101];
int maxn=100000001;
int main ()
{
int A,b,n,cas=1;
while (~SCANF ("%d%d", &a,&b) &&a+b)
{
int i,j,k;
for (i=0;i<101;i++)
for (j=0;j<101;j++)
DIST[I][J]=MAXN;
for (i=0;i<101;i++)
dist[i][i]=0;
n=0;
Dist[a][b]=1;
if (a>n)
N=a;
if (b>n)
N=b;
while (~SCANF ("%d%d", &a,&b) &&a+b)
{
Dist[a][b]=1;
if (a>n)
N=a;
if (b>n)
N=b;
}
for (k=1;k<=n;k++)
{
for (i=1;i<=n;i++)
{
for (j=1;j<=n;j++)
{
if (Dist[i][j]>dist[i][k]+dist[k][j])
DIST[I][J]=DIST[I][K]+DIST[K][J];
}
}
}
int count=0,sum=0;
for (i=1;i<=n;i++)
{
for (j=1;j<=n;j++)
{
if (Dist[i][j]<maxn&&dist[i][j])
{
SUM+=DIST[I][J];
count++;
}
}
}
cout<<sum<<endl;
printf ("Case%d:", cas++);
printf ("average length between pages =%.3lf clicks\n", 1.0*sum/count);
}
}
UVa 821 Page Hopping