Advertisement
Guest User

Untitled

a guest
Oct 22nd, 2016
56
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.31 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <string>
  4. #include <vector>
  5. #include <set>
  6.  
  7. using namespace std;
  8.  
  9. const int infinity = 200;
  10. int **tab, *d, v, w, vertexes, temp = 0;
  11. void Display();
  12. void CreateTable()
  13. {
  14.     tab = new int *[vertexes];
  15.     d = new int[vertexes];
  16. }
  17.  
  18. void ReadData()
  19. {
  20.     for (int i = 0; i < vertexes; i++)
  21.     {
  22.         tab[i] = new int[vertexes];
  23.         for (int j = 0; j < vertexes; j++)
  24.         {
  25.             cin >> v;
  26.             if (v != 0) { tab[i][j] = v; }
  27.             else { tab[i][j] = infinity; }
  28.         }
  29.     }
  30. }
  31.  
  32. void BellmanFord()
  33. {
  34.     for (int i = 0; i < vertexes; i++) { d[i] = infinity; }
  35.     d[0] = 0;
  36.  
  37.     for (int k = 0; k < vertexes -1  ; k++)
  38.     {
  39.         for (int i = 0 ; i < vertexes; i++)
  40.         {
  41.             for (int j = 0; j < vertexes; j++)
  42.             {
  43.                 if (d[i] + tab[i][j] < d[j])
  44.                     d[j] = d[i] + tab[i][j];
  45.             }
  46.         }
  47.         Display();
  48.         cout << endl;
  49.     }
  50. }
  51. void DeleteTable()
  52. {  
  53.     for (int i = 0; i < vertexes; i++)
  54.         delete[] tab[i];
  55.     delete[] tab;
  56.     delete[] d;
  57.     vertexes = 0;
  58. }
  59.  
  60. void Display()
  61. {
  62.     for (int i = 0; i < vertexes; i++)
  63.     {
  64.         if (d[i] < infinity) { cout << d[i]; }
  65.         else { cout << 0 ; }
  66.     }
  67. }
  68.  
  69. int main()
  70. {
  71.     int test = 0;
  72.     cin >> test;
  73.     for (int l = 0; l < test; l++)
  74.     {
  75.         cin >> vertexes;
  76.         CreateTable();
  77.         ReadData();
  78.         BellmanFord();
  79.         DeleteTable();
  80.     }
  81.     system("PAUSE");
  82.     return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement