Page Hopping

It was recently reported that, on the average, only 19 clicks are necessary to move from any page on
the World Wide Web to any other page. That is, if the pages on the web are viewed as nodes in a
graph, then the average path length between arbitrary pairs of nodes in the graph is 19.
Given a graph in which all nodes can be
reached from any starting point, your job is to
find the average shortest path length between arbitrary
pairs of nodes. For example, consider the
following graph. Note that links are shown as directed
edges, since a link from page a to page b
does not imply a link from page b to page a.
The length of the shortest path from node 1
to nodes 2, 3, and 4 is 1,1, and 2 respectively. From node 2 to nodes 1, 3 and 4, the shortest paths
have lengths of 3, 2, and 1. From node 3 to nodes 1, 2, and 4, the shortest paths have lengths of 1, 2,
and 3. Finally, from node 4 to nodes 1, 2, and 3 the shortest paths have lengths of 2, 3, and 1. The
sum of these path lengths is 1 + 1 + 2 + 3 + 2 + 1 + 1 + 2 + 3 + 2 + 3 + 1 = 22. Since there are
12 possible pairs of nodes to consider, we obtain an average path length of 22/12, or 1.833 (accurate
to three fractional digits).
Input
The input data will contain multiple test cases. Each test case will consist of an arbitrary number of
pairs of integers, a and b, each representing a link from a page numbered a to a page numbered b. Page
numbers will always be in the range 1 to 100. The input for each test case will be terminated with a
pair of zeroes, which are not to be treated as page numbers. An additional pair of zeroes will follow
the last test case, effectively representing a test case with no links, which is not to be processed. The
graph will not include self-referential links (that is, there will be no direct link from a node to itself),
and at least one path will exist from each node in the graph to every other node in the graph.
Output
For each test case, determine the average shortest path length between every pair of nodes, accurate to
three fractional digits. Display this length and the test case identifier (theyre numbered sequentially
starting with 1) in a form similar to that shown in the sample output below.
Sample Input
1 2 2 4 1 3 3 1 4 3 0 0
1 2 1 4 4 2 2 7 7 1 0 0
0 0
Sample Output
Case 1: average length between pages = 1.833 clicks
Case 2: average length between pages = 1.750 clicks
网页之间会有联通,求平均任意两个网页到达需要的次数,直接把两个网页能到达的找出来,然后累和,再除以总的网页,根据每个网页这个特性,可以使用弗洛伊德,三重暴力……然而我作死用了迪杰斯特拉恶心版

#include<cstdio>
#include<cstring>
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
using namespace std;
typedef long long ll;
const int inf = 0x3f3f3f3f;
int a[105][105], d[105][105], leap[105];
int main()
{
    int i, j, m, n, ans, t, x, y, num, p = 1;
    while (scanf("%d%d", &x, &y) != EOF)
    {
        if (!x&&!y)break;
        for (i = 1; i <= 100;i++)
        for (j = 1; j <= 100; j++)
        {
            a[i][j] = d[i][j] = inf;
        }
        memset(leap, 0, sizeof(leap));
        leap[x] = leap[y] = 1;
        num = 2;
        a[x][y] = 1;
        while(scanf("%d%d", &x, &y) != EOF)
        {
            if (!x&&!y)break;
            if (!leap[x])num++;
            leap[x] = 1;
            if (!leap[y])num++;
            leap[y] = 1;
            a[x][y] = 1;
        }
        for (i = 1; i <= 100;i++)
        for (j = 1; j <= 100; j++)
            d[i][j] = a[i][j];
        for (i = 1; i <= 100; i++)
        {
            memset(leap, 0, sizeof(leap));
            leap[i] = 1;
            int k;
            for (k = 1; k <= 100; k++)
            {
                int temp, x;
                temp = x = inf;
                for (j = 1; j <= 100; j++)
                {
                    if (!leap[j] && d[i][j] <= temp)
                    {
                        temp = d[i][j];
                        x = j;
                    }
                }
                if (temp == inf)break;
                leap[x] = 1;
                for (j = 1; j <= 100; j++)
                {
                    d[i][j] = min(d[i][j], d[i][x] + a[x][j]);
                }
            }
        }
        ans = 0;
        int ans1;
        double ans2;
        for (i = 1; i <= 100;i++)
        for (j = 1; j <= 100;j++)
        if (d[i][j] != inf&&i != j)
            ans = ans + d[i][j];
        ans1 = num*(num - 1);
        ans2 = ans*1.0 / ans1;
        cout << "Case " << p << ": average length between pages = ";
        p++;
        printf("%.3lf", ans2);
        cout << " clicks" << endl;
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值