-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path821 - Page Hopping.cpp
61 lines (47 loc) · 987 Bytes
/
821 - Page Hopping.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
#include<bits/stdc++.h>
using namespace std;
const int INF = 1e8 + 10;
int cn = 0;
void solve(int n ,int m){
int nax = 105;
int cost[nax][nax];
for(int i = 0 ; i < nax ; i++)
for(int j= 0 ; j < nax ;j++){
if(i==j)
cost[i][j] = 0;
else
cost[i][j] = INF;
}
nax = 0;
while(n or m){
cost[n][m] = 1;
scanf("%d%d",&n,&m);
nax = max({nax,n,m});
}
nax ++;
for(int k = 0 ; k < nax ; k++)
for(int i = 0 ; i < nax ; i++)
for(int j = 0; j < nax ; j++){
if(cost[i][j] > cost[i][k] + cost[k][j]){
cost[i][j] = cost[i][k]+cost[k][j];
}
}
long long int sum = 0;
int cnt = 0;
for(int i = 0; i < nax ; i++){
for(int j =0; j < nax ;j++){
if(cost[i][j] != INF and i!=j){
sum += cost[i][j];
cnt ++;
}
}
}
double ans = (sum*1.0/cnt);
printf("Case %d: average length between pages = %.3lf clicks\n",++cn,ans);
}
int main(){
int n , m;
while(scanf("%d%d",&n,&m)==2 and (n or m))
solve(n,m);
return 0;
}