Advertisement
sakib_shahriyar

FloydWarshall

Feb 27th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*FLOYDWARSHALL*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  2.  
  3. #include <bits/stdc++.h>
  4.  
  5. using namespace std;
  6.  
  7. const int MAX_NODE = 100;
  8. const int INF = 1e9;
  9.  
  10. int node , edge , cost[ MAX_NODE ][ MAX_NODE ];
  11.  
  12. void ini()
  13. {
  14.      for( int i = 1 ; i <= node ; i++ )
  15.     {
  16.         for( int j = 1 ; j <= node ; j++ )
  17.         {
  18.             cost[i][j] = INF;
  19.         }
  20.         cost[i][i] = 0;
  21.     }
  22. }
  23.  
  24. void constructGraph()
  25. {
  26.  
  27.     cin >> node >> edge;
  28.     ini();
  29.     int x , y, c;
  30.     for( int i = 0 ; i < edge ; i++ )
  31.     {
  32.        cin >> x >> y >> c;
  33.        cost[x][y] = cost[y][x] = min(cost[x][y], c );
  34.     }
  35. }
  36.  
  37. void floydwarshall(int source)
  38. {
  39.  
  40.      for( int k = 1 ; k <= node ; k++ )
  41.      {
  42.          for( int i = 1 ; i <= node ; i++ )
  43.          {
  44.              for( int j = 1 ; j <= node ; j++ )
  45.              {
  46.                  cost[i][j] = min( cost[i][j], cost[i][k] + cost[k][j] );
  47.              }
  48.          }
  49.      }
  50.  
  51.      for( int i = 1 ; i <= node ; i++ )
  52.      {
  53.          printf("source :: %d to :: %d dis :: %d\n",source, i , cost[source][i] );
  54.      }
  55.  
  56. }
  57.  
  58. int main()
  59. {
  60.     constructGraph();
  61.     floydwarshall(1);
  62.  
  63.     return 0;
  64. }
  65. /*
  66. 9 9
  67. 1 2 5
  68. 2 3 10
  69. 2 4 8
  70. 4 5 7
  71. 4 6 10
  72. 4 8 11
  73. 8 7 20
  74. 6 7 1
  75. 7 9 5
  76. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement