BotByte

Floyd Warshall.cpp

Mar 5th, 2017
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.59 KB | None | 0 0
  1. /* Floyd Warshall */
  2. /* Author : M. A. Rafsan Mazumder */
  3.  
  4. #include <bits/stdc++.h>
  5.  
  6. using namespace std;
  7.  
  8. #define MAX 105
  9. int dist[MAX][MAX];
  10. void floyd_warshall()
  11. {
  12.     for(int k=1; k<MAX; k++){
  13.         for(int i=1; i<MAX; i++){
  14.             for(int j=1; j<MAX; j++){
  15.                 if(dist[i][j] > dist[i][k] + dist[k][j]){
  16.                     dist[i][j] = dist[i][k] + dist[k][j];
  17.                     cout << i << " " << j << " " << dist[i][j] << endl;
  18.                 }
  19.             }
  20.         }
  21.     }
  22.  
  23.     //Transitive Closure
  24.     for(int k=1; k<MAX; k++){
  25.         for(int i=1; i<MAX; i++){
  26.             for(int j=1; j<MAX; j++){
  27.                 dist[i][j] == dist[i][j] || (dist[i][k] && dist[k][j]);
  28.             }
  29.         }
  30.     }
  31. }
  32.  
  33. int main()
  34. {
  35.     freopen("in.txt", "r", stdin);
  36.     int u, v;
  37.     int caseno = 0;
  38.     while(scanf("%d %d", &u, &v)){
  39.         if(u == 0 && v == 0) break;
  40.         for(int i=1; i<MAX; i++){
  41.             for(int j=1; j<MAX; j++){
  42.                 if(i == j) dist[i][j] = 0;
  43.                 else dist[i][j] = 1e9;
  44.             }
  45.         }
  46.         int cnt = 0;
  47.         while(scanf("%d %d", &u, &v)){
  48.             if(u == 0 && v == 0) break;
  49.             dist[u][v] = 1;
  50.             cnt++;
  51.         }
  52.         floyd_warshall();
  53.         int tot = 0;
  54.         for(int i=1; i<MAX; i++){
  55.             for(int j=1; j<MAX; j++){
  56.                 //if(dist[i][j] != 1e9 && dist[i][j] != 0) cout << dist[i][j] << " ";
  57.             }
  58.         }
  59.         printf("Case %d: average length between pages = %0.3lf\n",++caseno, (double) tot/(cnt * 1.0));
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment