-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpageHopping_UVA812OJ.cpp
90 lines (73 loc) · 1.79 KB
/
pageHopping_UVA812OJ.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <vector>
using namespace std;
void mark(int u, int & n, vector<int> & trans)
{
if (trans[u] == -1)
{
++n;
trans[u] = n;
}
}
int main()
{
ios_base::sync_with_stdio(false);
std::cin.tie(NULL);
int C = 0;
cout << fixed << setprecision(3);
while (true)
{
++C;
vector<int> trans(101, -1);
vector<vector<int>> dp(101, vector<int>(101, 100));
int u = 0, v = 0, n = 0;
cin >> u >> v;
if (u + v == 0) break;
mark(u, n, trans);
mark(v, n, trans);
dp[trans[u]][trans[v]] = 1;
while (u + v != 0)
{
cin >> u >> v;
if (u + v == 0) break;
mark(u, n, trans); mark(v, n, trans);
dp[trans[u]][trans[v]] = 1;
}
for (int i = 1; i <= n; ++i)
{
dp[i][i] = 0;
}
for (int k = 1; k <= n; ++k)
{
for (int u = 1; u <= n; ++u)
{
for (int v = 1; v <= n; ++v)
{
dp[u][v] = min(dp[u][v], dp[u][k] + dp[k][v]);
}
}
}
int total = 0;
for (int u = 1; u <= n; ++u)
{
for (int v = 1; v <= n; ++v)
{
total += dp[u][v];
}
}
double temp = n * (n - 1);
double ans = total / temp;
cout << "Case " << C << ": average length between pages = " << ans << " clicks\n";
}
cout.flush();
return 0;
}