fork download
  1. #include <cstdio>
  2. #include <algorithm>
  3. #include <cstring>
  4. #include <iostream>
  5.  
  6. using namespace std;
  7.  
  8. #define INF 1e9
  9. #define MAX_SIZE 100
  10.  
  11. int graph[MAX_SIZE + 1][MAX_SIZE + 1], n, mapping[MAX_SIZE + 1];
  12. int main() {
  13. int u, v, flag=0, testCase = 1;
  14. while(scanf("%d%d", &u, &v), u||v){
  15. for(int i = 1; i <= MAX_SIZE; i++) {
  16. for(int j = 1; j <= MAX_SIZE; j++) {
  17. graph[i][j] = INF;
  18. }
  19. graph[i][i] = 0;
  20. }
  21. int n = 0;
  22. memset (mapping, 0, sizeof(mapping));
  23. do {
  24. if (!mapping[u])
  25. mapping[u] = ++n;
  26. if (!mapping[v])
  27. mapping[v] = ++n;
  28. graph[mapping[u]][mapping[v]] = 1;
  29. } while(scanf("%d%d", &u, &v), u||v);
  30.  
  31. for(int k = 1; k <= n; k++)
  32. for(int i = 1; i <= n; i++)
  33. for(int j = 1; j <= n; j++)
  34. graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j]);
  35.  
  36. double s = 0.0;
  37. for(int i = 1; i <= n; i++)
  38. for(int j = 1; j <= n; j++)
  39. s += graph[i][j];
  40. //cout<<s<<endl;
  41. s /= (double)n*(n-1);
  42. //cout<<s<<endl;
  43. printf("Case %d: average length between pages = %.3lf clicks\n",testCase++,s);
  44. }
  45. return 0;
  46. }
Success #stdin #stdout 0s 3340KB
stdin
1 100 100 2 2 99 99 3 3 98 98 4 4 97 97 5 5 96 96 6 6 95 95 7 7 94 94 8 8 93 93 9 9 92 92 10 10 91 91 11 11 90 90 12 12 89 89 13 13 88 88 14 14 87 87 15 15 86 86 16 16 85 85 17 17 84 84 18 18 83 83 19 19 82 82 20 20 81 81 21 21 80 80 22 22 79 79 23 23 78 78 24 24 77 77 25 25 76 76 26 26 75 75 27 27 74 74 28 28 73 73 29 29 72 72 30 30 71 71 31 31 70 70 32 32 69 69 33 33 68 68 34 34 67 67 35 35 66 66 36 36 65 65 37 37 64 64 38 38 63 63 39 39 62 62 40 40 61 61 41 41 60 60 42 42 59 59 43 43 58 58 44 44 57 57 45 45 56 56 46 46 55 55 47 47 54 54 48 48 53 53 49 49 52 52 50 50 51 51 1 0 0
0 0
stdout
Case 1: average length between pages = 50.000 clicks