Advertisement
Guest User

Untitled

a guest
Oct 16th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.45 KB | None | 0 0
  1. // Example program
  2. #include <iostream>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. void array() {
  8.     const int inf = 100;
  9.     // liczba rzedow = ilosc krawedzi
  10.     // start - end - value
  11.     int arcs[8][3] = {
  12.             {6, 4, 5},
  13.             {6, 5, 4},
  14.             {5, 3, -2},
  15.             {5, 2, 1},
  16.             {4, 3, 3},
  17.             {2, 1, 1},
  18.             {3, 2, -3},
  19.             {3, 1, 3}
  20.     };
  21. // wielkosc tablicy = ilosc wierzcholkow
  22.     int nodes[6] = {inf, inf, inf, inf, inf, 0};
  23.     int pi[6] = {-1, -1, -1, -1, -1, -1};
  24.     for (int i = 0; i < 5; i++) {
  25.         for (auto & arc : arcs) {
  26.             int start = arc[0] - 1;
  27.             int end = arc[1] - 1;
  28.             int value = arc[2];
  29.             if (nodes[end] > value + nodes[start]) {
  30.                 nodes[end] = value + nodes[start];
  31.                 pi[end] = start + 1;
  32.             }
  33.         }
  34.     }
  35.  
  36.     cout << "array algorithm" << "\n";
  37.  
  38.     for (int node : nodes) {
  39.         cout << node << " ";
  40.     }
  41.  
  42.     cout << "\npi\n";
  43.     for (int i : pi) {
  44.         cout << i << " ";
  45.     }
  46.  
  47.     for (auto & arc : arcs) {
  48.         int start = arc[0] - 1;
  49.         int end = arc[1] - 1;
  50.         int value = arc[2];
  51.         if (nodes[end] > value + nodes[start]) {
  52.             cout << "error";
  53.         }
  54.     }
  55. }
  56.  
  57. void list() {
  58.     const int inf = 100;
  59.     // start - end - value
  60.     int arcs[24] = {6, 4, 5, 6, 5, 4, 5, 3, -2, 5, 2, 1, 4, 3, 3, 2, 1, 1, 3, 2, -3, 3, 1, 3};
  61.     // wielkosc tablicy = ilosc wierzcholkow
  62.     int nodes[6] = {inf, inf, inf, inf, inf, 0};
  63.     int pi[6] = {-1, -1, -1, -1, -1, -1};
  64.     for (int i = 0; i < 5; i++) {
  65.         for (int j = 0; j < 24; j += 3) {
  66.             int start = arcs[j] - 1;
  67.             int end = arcs[j + 1] - 1;
  68.             int value = arcs[j + 2];
  69.             if (nodes[end] > value + nodes[start]) {
  70.                 nodes[end] = value + nodes[start];
  71.                 pi[end] = start + 1;
  72.             }
  73.         }
  74.     }
  75.  
  76.     cout << "list algorithm" << "\n";
  77.  
  78.     for (int node : nodes) {
  79.         cout << node << " ";
  80.     }
  81.  
  82.     cout << "\npi\n";
  83.     for (int i : pi) {
  84.         cout << i << " ";
  85.     }
  86.  
  87.     for (int j = 0; j < 23; j += 3) {
  88.         int start = arcs[j] - 1;
  89.         int end = arcs[j + 1] - 1;
  90.         int value = arcs[j + 2];
  91.         if (nodes[end] > value + nodes[start]) {
  92.             cout << "error";
  93.         }
  94.     }
  95. }
  96.  
  97. int main()
  98. {
  99.     array();
  100.     cout << "\n";
  101.     list();
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement