UVA821 PageHopping

保存一下出现过的点,题目保证是完全图,Floyd求出最短路以后两个for统计一下。

#include<bits/stdc++.h>
using namespace std;

const int maxn = 101;

int g[maxn][maxn];
bool vis[maxn];
vector<int> p;
const int INF = 0x3f3f3f3f;

int main()
{
    //freopen("in.txt","r",stdin);
    int u,v,kas = 0;
    memset(g,0x3f,sizeof(g));
    for(int i = 1; i < maxn; i++) g[i][i] = 0;
    while(scanf("%d%d",&u,&v),u){
        p.clear(); memset(vis,0,sizeof(vis));
        g[u][v] = 1; vis[u] = vis[v] = true;
        while(scanf("%d%d",&u,&v),u){
            g[u][v] = 1;
            vis[u] = vis[v] = true;
        }
        for(int i = 1; i < maxn; i++){
            if(vis[i]) p.push_back(i);
        }
        int sz = p.size();

        for(int k = 0; k < sz; k++){
            int m = p[k];
            for(int i = 0; i < sz; i++){
                int u = p[i];
                if(g[u][m]<INF){
                     for(int j = 0; j < sz; j++){
                        int v = p[j];
                        g[u][v] = min(g[u][v],g[u][m]+g[m][v]);
                    }
                }
            }
        }

        int sum = 0;
        for(int i = 0; i < sz; i++){
            int u = p[i];
            for(int j = 0; j < sz; j++) if(i!=j){
                int v = p[j];
                sum += g[u][v];
                g[u][v] = INF;
            }
        }
        printf("Case %d: average length between pages = %.3lf clicks\n",++kas,(double)sum/(sz*(sz-1)));
    }
    return 0;
}
时间: 2024-11-08 21:58:47

UVA821 PageHopping的相关文章

UVa821 Page Hopping (Floyd)

链接:http://vjudge.net/problem/UVA-821 分析:利用Floyd求解,将所有有效边距离求和除以边数求平均值(注意:不包含自己到自己). 1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 using namespace std; 5 6 const int maxn = 100 + 5; 7 const int INF = 100000001; 8 9 int

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 r