---
# System prepended metadata

title: UVa .821 Page Hopping

---

# UVa .821 Page Hopping
[**題目連結**](https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=762)
## **Floyd Warshell**
```cpp=
#include<iostream>
#include<vector>
#include<algorithm>
#include<iomanip>
using namespace std;
int main()
{
    int Case = 1, a, b;
    while(true)
    {
        vector<vector<int>> len(101, vector<int> (101, 1e9));
        vector<int> node;
        vector<bool> exit(101, false);
        double sum = 0;
        for(int i = 1 ; i <= 100 ; i++)
            len[i][i] = 0;
        while(cin >> a >> b && a != 0 && b != 0)
        {
            len[a][b] = 1;
            if(!exit[a]) 
                node.push_back(a);
            if(!exit[b])
                node.push_back(b);
            exit[a] = exit[b] = true;
        }
        if(node.empty())
            break;
        sort(node.begin(), node.end());
        int n = node.back();
        for(int k = 1 ; k <= n ; k++) //Floyd
            for(int i = 1 ; i <= n ; i++)
                for(int j = 1 ; j <= n ; j++)
                    if(exit[i] && exit[j] && exit[k]) len[i][j] = min(len[i][j], len[i][k] + len[k][j]);

        for(int i = 1 ; i <= n ; i++)
        {
            for(int j = 1 ; j <= n ; j++)
            {
                if(exit[i] && exit[j] && i != j)
                    sum += len[i][j];
            }
        }
        cout << "Case " << Case << ": average length between pages = ";
        cout << fixed << setprecision(3) << sum / (node.size() * (node.size() - 1)) << " clicks\n";
        Case++;
    }
    return 0;
}
```
## **BFS**
```cpp=
#include<iostream>
#include<queue>
#include<vector>
#include<algorithm>
#include<iomanip>
using namespace std;
void bfs(int i, vector<vector<int>> &Map, vector<int> &path)
{
    queue<int> q;
    q.push(i);
    path[i] = 0;
    while(!q.empty())
    {
        int now = q.front();
        q.pop();
        for(int i : Map[now])
        {
            if(path[i] == -1)
            {
                path[i] = path[now] + 1;
                q.push(i);
            }
        }
    }
}
int main()
{
    int Case = 1;
    while(true)
    {
        vector<vector<int>> Map(101, vector<int>{});
        vector<int> node;
        vector<bool> exit(101, false);
        int a, b;
        double sum = 0;
        while(cin >> a >> b && a != 0 && b != 0)
        {
            if(!exit[a])
                node.push_back(a);
            if(!exit[b])
                node.push_back(b);
            exit[a] = exit[b] = true;
            Map[a].push_back(b);
        }
        if(node.empty())
            break;
        sort(node.begin(), node.end());
        for(int i : node)
        {
            vector<int> path(node.back() + 1, -1);
            bfs(i, Map, path);
            for(int i = 1 ; i <= path.size() ; i++)
                if(exit[i]) sum += path[i];
        }
        cout << "Case " << Case << ": average length between pages = ";
        cout << fixed << setprecision(3) << sum / (node.size() * ( node.size() - 1 )) << " clicks\n";
        Case++;
    }
}
```