Advertisement
Guest User

What???

a guest
Apr 25th, 2015
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.30 KB | None | 0 0
  1. #include <cstdlib>
  2. #include <iostream>
  3. #include <fstream>
  4. #include <typeinfo>
  5. #include <vector>
  6. #include <climits>
  7.  
  8. #define INF -1
  9. #define ASCII_START 97
  10. #define SIZE 5
  11.  
  12. using namespace std;
  13.  
  14. class Cell {
  15.     vector<string> summa, new_summa;
  16.  
  17. public:
  18.  
  19.     void mult(string &str, Cell &other) {
  20.         for (auto &str2 : other.summa) {
  21.             new_summa.push_back(str + str2);
  22.         }
  23.     }
  24.  
  25.     void commit(string &row) {
  26.         for (auto it = new_summa.begin();it != new_summa.end();it++) {
  27.             if ((*it).find(row) != string::npos) {
  28.                 new_summa.erase(it);
  29.                 it--;
  30.             }
  31.         }
  32.         summa = new_summa, new_summa.clear();
  33.     }
  34.  
  35.     bool empty() {
  36.         return summa.size() == 0;
  37.     }
  38.  
  39.     int get_most_short(int m[][SIZE], int size, string &path) {
  40.         int len = INT_MAX;
  41.         for (auto &str : summa) {
  42.             int tmp = 0;
  43.             char prev = path[0];
  44.             string buff(&prev, 1);
  45.             for (char &ch : str) {
  46.                 tmp += m[prev - ASCII_START][ch - ASCII_START];
  47.                 prev = ch;
  48.                 buff += ch;
  49.             }
  50.             tmp += m[prev - ASCII_START][path[0] - ASCII_START]; // cycled path
  51.             if (tmp < len) len = tmp, path.assign(buff);
  52.         }
  53.         return len;
  54.     }
  55.  
  56.     void operator += (const string str) {
  57.         summa.push_back(str);
  58.     }
  59. };
  60.  
  61. void mult(string[][SIZE], Cell[][SIZE], bool=false);
  62.  
  63. int main(int argc, char** argv) {
  64.  
  65.     int m[][SIZE] = {{INF, 10, 5, 8, 1},
  66.             {8, INF, 15, 2, 14},
  67.             {5, 14, INF, 1, INF},
  68.             {4, 16, 10, INF, 2},
  69.             {1, 4, 1, 1, INF}};
  70.  
  71.     string v[SIZE][SIZE];
  72.     Cell p[SIZE][SIZE];
  73.     for (int i = 0; i < SIZE; i++) {
  74.         for (int j = 0; j < SIZE; j++) {
  75.             char ch = m[i][j] > 0 ? ASCII_START + j : '0';
  76.             v[i][j].assign(string(&ch, 1));
  77.         }
  78.     }
  79.  
  80.     for (int i = 0; i < SIZE; i++) {
  81.         for (int j = 0; j < SIZE; j++) {
  82.             for (int k = 0; k < SIZE; k++) {
  83.                 if (not (v[i][k].empty() or v[i][k].compare("0") == 0) and m[k][j] != INF and i != j) {
  84.                     p[i][j] +=  v[i][k];
  85.                 }
  86.             }
  87.         }
  88.     }
  89.  
  90.     for (int i = 3;i <= SIZE;i++) {
  91.         if (i == SIZE) {            // Якщо остання матриця P
  92.             mult(v, p, true); // то рахуємо діагоналі
  93.         } else mult(v, p);
  94.         for (int j = 0;j < SIZE;j++) {
  95.             for (int k = 0;k < SIZE;k++) {
  96.                 char ch(ASCII_START + j);
  97.                 string str(&ch, 1);
  98.                 p[j][k].commit(str);
  99.             }
  100.         }
  101.     }
  102.  
  103.     string path("a");
  104.     int len = p[0][0].get_most_short(m, SIZE, path);
  105.     cout << endl << endl << "Shortest path: " << (path += "a") << " Length: " << len << endl << endl;
  106.  
  107.     return 0;
  108. }
  109.  
  110. void mult(string v[][SIZE], Cell p[][SIZE], bool calc_diag) {
  111.     for (int i = 0; i < SIZE; i++) {
  112.         for (int j = 0; j < SIZE; j++) {
  113.             for (int k = 0; k < SIZE; k++) {
  114.                 if (not (v[i][k].empty() or v[i][k].compare("0") == 0) and not p[k][j].empty() and (i !=  j or calc_diag)) {
  115.                     p[i][j].mult(v[i][k], p[k][j]);
  116.                 }
  117.             }
  118.         }
  119.     }
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement