Advertisement
cyalue

kursach

Nov 17th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.03 KB | None | 0 0
  1. #include<iostream>
  2. #include<iomanip>
  3. #define NODE 20
  4. #define INF 999
  5. using namespace std;
  6.  
  7. double costMat[NODE][NODE];
  8.  
  9.  
  10. //Cost matrix of the graph
  11.  
  12.  
  13.  
  14. void floydWarshal() {
  15.     double cost[NODE][NODE];    //defind to store shortest distance from any node to any node
  16.     int next[NODE][NODE];
  17.     double next1[NODE][NODE];
  18.     double b[NODE][NODE];
  19.     for (int i = 0; i < NODE; i++) {
  20.         for (int j = 0; j < NODE; j++) {
  21.             cin >> costMat[i][j];
  22.                 if (costMat[i][j] == 0 && (i != j)) {
  23.                     costMat[i][j] = INF;
  24.                 }
  25.         }
  26.     }
  27.     cout << " ne w" << endl;
  28.     for (int i = 0; i < NODE; i++) {
  29.         for (int j = 0; j < NODE; j++) {
  30.             cin >> next1[i][j];
  31.         }
  32.     }
  33.        
  34.     for (int i = 0; i < NODE; i++) {
  35.         for (int j = 0; j < NODE; j++) {
  36.             b[i][j] = 0;
  37.         }
  38.     }
  39.  
  40.  
  41.     for (int i = 0; i < NODE; i++)
  42.         for (int j = 0; j < NODE; j++) {
  43.             cost[i][j] = costMat[i][j];
  44.         }
  45.  
  46.     for (int i = 0; i < NODE; i++)
  47.         for (int j = 0; j < NODE; j++) {
  48.             if(costMat[i][j] != INF)
  49.              next[i][j] = j;
  50.             else{
  51.                 next[i][j] = -1;
  52.             }
  53.         }
  54.  
  55.     for (int k = 0; k < NODE; k++) {
  56.         for (int i = 0; i < NODE; i++)
  57.             for (int j = 0; j < NODE; j++)
  58.                 if (cost[i][k] + cost[k][j] < cost[i][j]) {
  59.                     cost[i][j] = cost[i][k] + cost[k][j];
  60.                     next[i][j] = next[i][k];
  61.                 }
  62.                    
  63.     }
  64.  
  65.    
  66.     for (int i = 0; i < NODE; i++) {
  67.         for (int j = 0; j < NODE; j++) {
  68.             int d = i;
  69.             do {
  70.                 b[d][next[d][j]] += next1[i][j];
  71.                 d = next[d][j];
  72.             } while (d != j);
  73.         }
  74.     }
  75.  
  76.    
  77.  
  78.     cout << "The matrix:" << endl;
  79.     for (int i = 0; i < NODE; i++) {
  80.             for (int j = 0; j < NODE; j++) {
  81.                 cout.precision(3);
  82.                 cout << cost[i][j] << " ";
  83.             }
  84.         cout << endl;
  85.     }
  86.  
  87.  
  88.     cout << "The matrix:" << endl;
  89.     for (int i = 0; i < NODE; i++) {
  90.         for (int j = 0; j < NODE; j++) {
  91.             cout.precision(3);
  92.             cout << setw(5) << b[i][j] << " ";
  93.         }
  94.         cout << endl;
  95.     }
  96.  
  97.  
  98.     cout << " \n \n \n";
  99.     for (int i = 0; i < NODE; i++) {
  100.             for (int j = 0; j < NODE; j++) {
  101.                 cout << setw(5) << next[i][j] + 1;
  102.             }
  103.         cout << endl;
  104.     }
  105.  
  106.  
  107. }
  108.  
  109. int main() {
  110.     floydWarshal();
  111. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement