Thursday, 9 June 2016

UVa 821 - Page Hooping

#include<bits/stdc++.h>
#define mx 1000
#define inf 1e18
#define nil -999999999
using namespace std;
typedef long long ll;
ll graph[mx][mx];
ll path[mx][mx],N;
void fl_wr()
{
    ll i,j,k,l,m,n;
    for(k=1;k<=N;k++)
    {
        for(i=1;i<=N;i++)
        {
            for(j=1;j<=N;j++)
            {
                graph[i][j]=min(graph[i][j],graph[i][k]+graph[k][j]);
            }
        }
    }
}
int main()
{
    ll i,j,k=0,l,m,n;
    while(1)
    {
        k++;
        for(i=0;i<mx;i++)
        {
            for(j=0;j<mx;j++)
            {
                graph[i][j]=inf;
            }
        }
        for(i=1;i<mx;i++)
        {
            graph[i][i]=0;
        }
        ll a,b;
        N=0;
        cin>>a>>b;
        N=max(max(a,b),N);
        if(a==0&&b==0)
            break;
        else
        {
            graph[a][b]=1;
            while(1)
            {
                ll a,b;
                cin>>a>>b;
                N=max(max(a,b),N);
                if(a==0&&b==0)
                    break;
                else
                {
                    graph[a][b]=1;
                }
            }
            fl_wr();
            ll sum=0,d=0;
            for(i=1;i<=N;i++)
            {
                for(j=1;j<=N;j++)
                {
                    if(graph[i][j]!=inf&&graph[i][j]!=0)
                    {
                        d++;
                        sum+=graph[i][j];
                    }
                }
            }
            double x,y;
            x=sum;
            y=d;
            printf("Case %lld: average length between pages = ",k);
            printf("%.3lf clicks\n",x/y);
        }
    }
    return 0;
}

No comments:

Post a Comment