{"id":329321,"date":"2022-02-09T15:00:31","date_gmt":"2022-02-09T15:00:31","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=329321"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=329321","title":{"rendered":"<span>\u041f\u0443\u0442\u0438 \u043d\u0430 \u0433\u0440\u0430\u0444\u0435. \u0417\u0430\u0434\u0430\u0447\u0430 \u043a\u043e\u043c\u043c\u0438\u0432\u043e\u044f\u0436\u0451\u0440\u0430 \u043d\u0430 C++<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h2>\u041f\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0437\u0430\u0434\u0430\u0447\u0438<\/h2>\n<p>\u0418\u043c\u0435\u0442\u0441\u044f \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0439 \u0433\u0440\u0430\u0444. \u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043d\u0430\u0439\u0442\u0438 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u043f\u0443\u0442\u044c \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u0439\u0441\u044f \u0438 \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u044e\u0449\u0438\u0439\u0441\u044f \u0432 \u0434\u0430\u043d\u043d\u043e\u0439 \u0442\u043e\u0447\u043a\u0435, \u0442\u0430\u043a\u0436\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u044c \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0432\u0435\u0440\u0448\u0438\u043d \u0433\u0440\u0430\u0444\u0430.<\/p>\n<p>\u0414\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438, \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0430\u0439\u0434\u0451\u043c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043c\u0435\u0436\u0434\u0443 \u0432\u0441\u0435\u043c\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u043c\u0438 \u0434\u043b\u044f \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u0438\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430\u043c\u0438 \u0438 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0414\u0435\u0439\u043a\u0441\u0442\u0440\u044b. \u0417\u0430\u0442\u0435\u043c \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043a\u0430\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0443\u043c\u043c\u0443 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u0438\u0437\u043c\u0435\u043d\u044f\u044f \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u0438\u044f \u0432\u0435\u0440\u0448\u0438\u043d.<\/p>\n<p>\u042d\u0442\u0443 \u0437\u0430\u0434\u0430\u0447\u0443 \u043c\u043e\u0436\u043d\u043e \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u0442\u044c \u0438 \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u043e\u0438\u0441\u043a \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u0430 \u0434\u043b\u044f \u043f\u043e\u043b\u0435\u0442\u0430 \u043a\u0432\u0430\u0434\u0440\u043e\u043a\u043e\u043f\u0442\u0435\u0440\u0430-\u043a\u0443\u0440\u044c\u0435\u0440\u0430.<\/p>\n<h2>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0433\u0440\u0430\u0444\u0430<\/h2>\n<p>\u0413\u0440\u0430\u0444 \u0441 N \u0432\u0435\u0440\u0448\u0438\u043d\u0430\u043c\u0438 \u0431\u0443\u0434\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c\u0441\u044f \u0432 \u043c\u0430\u0442\u0440\u0438\u0446\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 NxN.<\/p>\n<p>\u041f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e  \u0437\u0430\u0434\u0430\u0434\u0438\u043c \u0433\u0440\u0430\u0444 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0439 \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 \u043d\u0438\u0436\u0435:<\/p>\n<figure class=\"\"><img decoding=\"async\" src=\"\/img\/image-loader.svg\" alt=\"\u0413\u0440\u0430\u0444 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e\" title=\"\u0413\u0440\u0430\u0444 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e\" height=\"339\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/092\/759\/03c\/09275903cc91fe1edf086e2cce639523.png\" data-width=\"501\"\/><figcaption>\u0413\u0440\u0430\u0444 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e<\/figcaption><\/figure>\n<p><strong>Graph.h:<\/strong><\/p>\n<pre><code class=\"cpp\">#ifndef GRAPH_H #define GRAPH_H  #include &lt;iostream> #include &lt;iomanip>  using namespace std;  class Graph { private:     int n;                          \/\/ \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0435\u0440\u0448\u0438\u043d \u0433\u0440\u0430\u0444\u0430     int** data;                     \/\/ \u0434\u0430\u043d\u043d\u044b\u0435 \u043e \u0440\u0435\u0431\u0440\u0430\u0445 \u0433\u0440\u0430\u0444\u0430: -1 - \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0440\u0435\u0431\u0440\u043e; >0 - \u0440\u0430\u0441\u0442\u0442\u043e\u044f\u043d\u0438\u0435 \u043c\u0435\u0436\u0434\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u0430\u043c\u0438 i \u0438 j      public:     Graph(int n);                   \/\/ \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0433\u0440\u0430\u0444\u0430 \u0441 n \u0432\u0435\u0440\u0448\u0438\u043d\u0430\u043c\u0438     Graph();                        \/\/ \u0433\u0440\u0430\u0444 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e     int** Get_data() const;         \/\/ \u0434\u0430\u043d\u043d\u044b\u0435 \u043e \u0433\u0440\u0430\u0444\u0435     int Get_n() const;              \/\/ \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0435\u0440\u0448\u0438\u043d \u0433\u0440\u0430\u0444\u0430  };  ostream&amp; operator&lt;&lt;(ostream&amp; os, const Graph&amp; g);  #endif <\/code><\/pre>\n<p><strong>Graph.cpp<\/strong>:<\/p>\n<pre><code class=\"cpp\">#include \"Graph.h\"  Graph::Graph() {     this->n = 5;     data = new int*[5];     for (int i = 0; i &lt; 5; i++) {         data[i] = new int[5];     }      \/\/ \u0433\u0440\u0430\u0444 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e     data[0][0] = 0;    data[0][1] = 2;    data[0][2] = -1;    data[0][3] = 3;    data[0][4] = -1;     data[1][0] =2;     data[1][1] = 0;    data[1][2] = 4;     data[1][3] = 4;    data[1][4] = -1;     data[2][0] = -1;   data[2][1] = 4;    data[2][2] = 0;     data[2][3] = 5;    data[2][4] = 6;     data[3][0] = 3;    data[3][1] = 4;    data[3][2] = 5;    data[3][3] = 0;    data[3][4] = -1;     data[4][0] = -1;   data[4][1] = -1;   data[4][2] = 6;     data[4][3] = -1;   data[4][4] = 0; }  \/\/ \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0433\u0440\u0430\u0444\u0430 \u0441 n \u0432\u0435\u0440\u0448\u0438\u043d\u0430\u043c\u0438 Graph::Graph(int n) {     this->n = n;     data = new int*[n];     for (int i = 0; i &lt; n; i++) {         data[i] = new int[n];     }     for (int i = 0; i &lt; n; i++) {         for (int j = i + 1; j &lt; n; j++) {             cout &lt;&lt; \"Input \" &lt;&lt; i &lt;&lt; '-' &lt;&lt; j &lt;&lt; \": \";             cin >> data[i][j];             data[j][i] = data[i][j];         }     } }  \/\/ \u0434\u0430\u043d\u043d\u044b\u0435 \u043e \u0433\u0440\u0430\u0444\u0435 int** Graph::Get_data() const { return data; }  \/\/ \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0435\u0440\u0448\u0438\u043d \u0433\u0440\u0430\u0444\u0430 int Graph::Get_n() const { return n; }  \/\/ \u0432\u044b\u0432\u043e\u0434 \u0433\u0440\u0430\u0444\u0430 ostream&amp; operator&lt;&lt;(ostream&amp; os, const Graph&amp; g) {     int** d = g.Get_data();          for (int i = 0; i &lt; g.Get_n(); i++) {         for (int j = 0; j &lt; g.Get_n(); j++) {             os &lt;&lt; setw(4) &lt;&lt; d[i][j] &lt;&lt; ' ';         }         os &lt;&lt; endl;     }     return os; } <\/code><\/pre>\n<h2>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0443\u0442\u0438<\/h2>\n<p>\u041f\u0443\u0442\u044c \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432 \u0432\u0438\u0434\u0435 \u0432\u0435\u043a\u0442\u043e\u0440\u0430, \u0442\u0430\u043a\u0436\u0435 \u044f \u0445\u0440\u0430\u043d\u044e \u0434\u043b\u0438\u043d\u0443 \u0432\u0441\u0435\u0433\u043e \u043f\u0443\u0442\u0438. \u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0442\u0430\u043a\u0436\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u043a\u043e\u043d\u0435\u0446 \u043f\u0443\u0442\u0438 \u043e\u0434\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0438 \u0434\u0440\u0443\u0433\u043e\u0433\u043e \u043f\u0443\u0442\u0438 (\u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0434\u0430\u043b\u044c\u0448\u0435 \u0432 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0435 \u0414\u0435\u0439\u043a\u0441\u0442\u0440\u044b). <\/p>\n<p><strong>Path.h:<\/strong><\/p>\n<pre><code class=\"cpp\">#ifndef PATH_H #define PATH_H  #include &lt;iostream> #include &lt;set> #include &lt;vector>  using namespace std;  class Path { private:     vector&lt;int> path;                   \/\/ \u043f\u0443\u0442\u044c     int length;                         \/\/ \u0434\u043b\u0438\u043d\u0430 \u043f\u0443\u0442\u0438  public:     Path(int start);     Path&amp; operator= (const Path&amp; p);     vector&lt;int> Get_path() const;       \/\/ \u043f\u0443\u0442\u044c     int Get_length() const;             \/\/ \u0434\u043b\u0438\u043d\u0430 \u043f\u0443\u0442\u0438     void push_back(int v, int l);       \/\/ \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0432 \u043a\u043e\u043d\u0435\u0446     void push_back(Path p);             \/\/ \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u0443\u0442\u0438 \u0432 \u043a\u043e\u043d\u0435\u0446  };  ostream&amp; operator&lt;&lt;(ostream&amp; os, const Path&amp; p);  #endif <\/code><\/pre>\n<p><strong>Path.cpp:<\/strong><\/p>\n<pre><code class=\"cpp\">#include \"Path.h\"  Path::Path(int start) {     path.push_back(start);     length = 0; }  Path&amp; Path::operator=(const Path&amp; p) {     path = p.Get_path();     length = p.Get_length();     return *this; }  \/\/ \u043f\u0443\u0442\u044c vector&lt;int> Path::Get_path() const { return path; }  \/\/ \u0434\u043b\u0438\u043d\u0430 \u043f\u0443\u0442\u0438 int Path::Get_length() const { return length; }  \/\/ \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0432 \u043a\u043e\u043d\u0435\u0446 void Path::push_back(int v, int l) {     path.push_back(v);     length += l; }  \/\/ \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u0443\u0442\u0438 \u0432 \u043a\u043e\u043d\u0435\u0446 void Path::push_back(Path p) {     for (size_t i = 0; i &lt; p.Get_path().size(); ++i) {         int last = path[path.size() - 1];         if (last != p.Get_path()[i]) {             path.push_back(p.Get_path()[i]);         }     }     length += p.Get_length(); }  ostream&amp; operator&lt;&lt;(ostream&amp; os, const Path&amp; p) {     size_t n = p.Get_path().size();     for (size_t i = 0; i &lt; n; i++) {         os &lt;&lt; p.Get_path()[i] + 1;         if (i != n - 1) {             os &lt;&lt; \"->\";         } else {             os &lt;&lt; \" (\";         }     }     os &lt;&lt; p.Get_length() &lt;&lt; ')' &lt;&lt; endl;     return os; } <\/code><\/pre>\n<h2>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0414\u0435\u0439\u043a\u0441\u0442\u0440\u044b<\/h2>\n<p>\u041e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u043c \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0441\u0442\u044c\u044e, \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u043e\u0437\u043d\u0430\u0447\u0430\u0442\u044c, \u0447\u0442\u043e \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u0435\u0449\u0451 \u043d\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u0430, \u0430 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u0443\u044e 0. <s>\u041e\u0447\u0435\u0432\u0438\u0434\u043d\u043e<\/s>, \u0447\u0442\u043e \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0434\u043e \u0441\u0430\u043c\u043e\u0439 \u0441\u0435\u0431\u044f \u0440\u0430\u0432\u043d\u043e 0. \u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u0439\u0434\u0435\u043c \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0438\u0437 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0442\u043e\u0447\u043a\u0438 \u0432 \u0441\u043e\u0441\u0435\u0434\u043d\u0438\u0435, \u0437\u0430\u0442\u0435\u043c \u043e\u0442\u043c\u0435\u0442\u0438\u043c \u0435\u0451. \u0414\u0430\u043b\u0435\u0435 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u043d\u0430\u0445\u043e\u0434\u0438\u043c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u043d\u0435 \u043e\u0442\u043c\u0435\u0447\u0435\u043d\u043d\u0443\u044e \u0442\u043e\u0447\u043a\u0443 \u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u043c \u0441\u0443\u043c\u043c\u0443 \u0441 \u0441\u043e\u0441\u0435\u0434\u043d\u0438\u043c\u0438, \u043d\u0435 \u043e\u0442\u043c\u0435\u0447\u0435\u043d\u043d\u044b\u043c\u0438 \u0438 \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u043c \u0435\u0435 \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0432\u0435\u0440\u0448\u0438\u043d\u044b, \u0435\u0441\u043b\u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043c\u0435\u043d\u044c\u0448\u0435, \u0442\u043e \u0437\u0430\u043c\u0435\u043d\u044f\u0435\u043c \u0435\u0433\u043e. <\/p>\n<h2>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0414\u0435\u0439\u043a\u0441\u0442\u0440\u044b \u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043a\u043e\u043c\u043c\u0438\u0432\u043e\u044f\u0436\u0451\u0440\u0430<\/h2>\n<p>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0414\u0435\u0439\u043a\u0441\u0442\u0440\u044b \u043c\u044b \u0443\u0436\u0435 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u043b\u0438, \u043d\u043e \u043f\u0440\u0438 \u0435\u0433\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 \u044f \u0442\u0430\u043a\u0436\u0435 \u0441\u0440\u0430\u0437\u0443 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u044e \u043a\u0440\u0430\u0442\u0447\u0430\u0439\u0448\u0438\u0439 \u043f\u0443\u0442\u044c \u043e\u0442 \u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043a \u0434\u0440\u0443\u0433\u0438\u043c. \u0415\u0441\u043b\u0438 \u0432\u0435\u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043e\u043a\u0430\u0437\u0430\u043b\u0441\u044f \u0431\u043e\u043b\u044c\u0448\u0435, \u0447\u0435\u043c \u043d\u043e\u0432\u044b\u0439, \u0442\u043e \u043a\u0440\u043e\u043c\u0435 \u043d\u043e\u0432\u043e\u0433\u043e \u0432\u0435\u0441\u0430, \u043c\u044b \u0442\u0430\u043a\u0436\u0435 \u0437\u0430\u043c\u0435\u043d\u044f\u0435\u043c \u043f\u0443\u0442\u044c: \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0432\u0435\u043d \u043f\u0443\u0442\u0438 \u043a \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043f\u043b\u044e\u0441 \u0441\u0430\u043c\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0430.<\/p>\n<p>\u0412\u0441\u0435 \u043f\u0443\u0442\u0438 \u044f \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u044e \u0432 \u0441\u043b\u043e\u0432\u0430\u0440\u044c, \u043a\u043b\u044e\u0447\u043e\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430, \u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u0443\u0442\u0435\u0439 (i-\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0432\u0443\u0435\u0442 \u043a\u0440\u043e\u0442\u0447\u0430\u0439\u0448\u0435\u043c\u0443 \u043f\u0443\u0442\u0438 \u0434\u043e (i+1)-\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b).<\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0414\u0435\u0439\u043a\u0441\u0442\u0440\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e next_permutation \u044f \u043f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u044e \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u0438\u044f \u0432\u0435\u0440\u0448\u0438\u043d \u0438 \u043d\u0430\u0445\u043e\u0436\u0443 \u043f\u0443\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b.<\/p>\n<p><strong>Solution.h:<\/strong><\/p>\n<pre><code class=\"cpp\">#ifndef SOLUTION_H #define SOLUTION_H  #include \"Graph.h\" #include \"Path.h\" #include &lt;iostream> #include &lt;limits> #include &lt;vector> #include &lt;map> #include &lt;algorithm>  using namespace std;  class Solution { private:     int n;                                      \/\/ \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0435\u0440\u0448\u0438\u043d     int** graph;                                \/\/ \u0433\u0440\u0430\u0444 \u0432 \u0432\u0438\u0434\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 n \u043d\u0430 n  public:     Solution(const Graph&amp; g);     Path* Dijkstra(int v, Path* data);          \/\/ \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0414\u0435\u0439\u043a\u0441\u0442\u0440\u044b     Path calc(vector&lt;int> v, int start);        \/\/ \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u0443\u0442\u0438  };  #endif <\/code><\/pre>\n<p><strong>Solution.cpp:<\/strong><\/p>\n<pre><code class=\"cpp\">#include \"Solution.h\"  Solution::Solution(const Graph&amp; g) {     graph = g.Get_data();     n = g.Get_n(); }  \/\/ \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0414\u0435\u0439\u043a\u0441\u0442\u0440\u044b Path* Solution::Dijkstra(int v, Path* data) {     data = (Path*)calloc(n, sizeof(Path));      \/\/\u043f\u0443\u0442\u0438 \u043e\u0442 \u0432\u0435\u0440\u0448\u0438\u043d\u044b v \u0434\u043e \u0434\u0440\u0443\u0433\u0438\u0445 \u0432\u0435\u0440\u0448\u0438\u043d \u0433\u0440\u0430\u0444\u0430     int distaces[n];                            \/\/\u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043e\u0442 \u0432\u0435\u0440\u0448\u0438\u043d\u044b v \u0434\u043e \u0434\u0440\u0443\u0433\u0438\u0445     int out[n];                                 \/\/\u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b     for (int i = 0; i &lt; n; ++i) {         if (i == v) {             distaces[i] = 0;             data[i] = Path(v);         } else {             distaces[i] = numeric_limits&lt;int>::max();         }         out[i] = 0;     }      int min = numeric_limits&lt;int>::max(), index = -1;     do {         min = numeric_limits&lt;int>::max();         for (int i = 0; i &lt; n; ++i) {             if ((out[i] == 0) &amp;&amp; (distaces[i] &lt; min)) {                 min = distaces[i];                 index = i;             }         }         if (min != numeric_limits&lt;int>::max()) {             for (int i = 0; i &lt; n; ++i) {                 if (graph[index][i] > 0) {                     int temp = min + graph[index][i];                     if (temp &lt; distaces[i]) {                         distaces[i] = temp;                         data[i] = data[index];                         data[i].push_back(i, graph[index][i]);                     }                 }             }             out[index] = 1;         }     } while (min &lt; numeric_limits&lt;int>::max());      return data; }  \/\/ \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u0443\u0442\u0438 Path Solution::calc(vector&lt;int> v, int start) {     map&lt;int, Path*> distaces;     Path* temp = (Path*)calloc(n, sizeof(Path));     temp = Dijkstra(start, temp);     distaces[start] = temp;     for (size_t i = 0; i &lt; v.size(); ++i) {         temp = Dijkstra(v[i], temp);         distaces[v[i]] = temp;     }      Path p = Path(start);     sort(v.begin(), v.end());     for (size_t i = 0; i &lt; v.size(); ++i) {         int last = p.Get_path()[p.Get_path().size() - 1];         p.push_back(distaces[last][v[i]]);     }     int last = p.Get_path()[p.Get_path().size() - 1];     p.push_back(distaces[last][start]);     next_permutation(v.begin(), v.end());      do {         Path temp = Path(start);         for (size_t i = 0; i &lt; v.size(); ++i) {             int last = temp.Get_path()[temp.Get_path().size() - 1];             temp.push_back(distaces[last][v[i]]);             if (temp.Get_length() > p.Get_length())                 break;         }         int last = temp.Get_path()[temp.Get_path().size() - 1];         temp.push_back(distaces[last][start]);          if (temp.Get_length() &lt; p.Get_length()) {             p = temp;         }     } while (next_permutation(v.begin(), v.end()));      return p; } <\/code><\/pre>\n<h2>\u0418\u0442\u043e\u0433\u0438<\/h2>\n<p>\u0414\u0430\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0434\u0430\u043b\u0435\u043a\u043e \u043e\u0442 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e\u0433\u043e, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u043d\u0430\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u044f \u043f\u043e\u0440\u044f\u0434\u043a\u0430 \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d. \u0422\u0430\u043a\u0436\u0435 \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u0443\u044e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0414\u0435\u0439\u043a\u0441\u0442\u0440\u044b, \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0432 \u0435\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0424\u0438\u0431\u043e\u043d\u0430\u0447\u0447\u0438\u0435\u0432\u043e\u0439 \u043a\u0443\u0447\u0438.<\/p>\n<p>\u041f\u0440\u043e\u0435\u043a\u0442 \u0437\u0430\u0434\u0430\u0447\u0438 \u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u043a\u0430\u0447\u0430\u0442\u044c \u043d\u0430 <a href=\"https:\/\/github.com\/MMMuzahim\/travelling-salesman-problem\" rel=\"noopener noreferrer nofollow\">GitHub<\/a>.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/650643\/\"> https:\/\/habr.com\/ru\/post\/650643\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<h2>\u041f\u043e\u0441\u0442\u0430\u043d\u043e\u0432\u043a\u0430 \u0437\u0430\u0434\u0430\u0447\u0438<\/h2>\n<p>\u0418\u043c\u0435\u0442\u0441\u044f \u0441\u0432\u044f\u0437\u0430\u043d\u043d\u044b\u0439 \u0433\u0440\u0430\u0444. \u041d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043d\u0430\u0439\u0442\u0438 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u043f\u0443\u0442\u044c \u043d\u0430\u0447\u0438\u043d\u0430\u044e\u0449\u0438\u0439\u0441\u044f \u0438 \u0437\u0430\u043a\u0430\u043d\u0447\u0438\u0432\u0430\u044e\u0449\u0438\u0439\u0441\u044f \u0432 \u0434\u0430\u043d\u043d\u043e\u0439 \u0442\u043e\u0447\u043a\u0435, \u0442\u0430\u043a\u0436\u0435 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u0441\u0435\u0442\u0438\u0442\u044c \u043f\u043e\u0434\u043c\u043d\u043e\u0436\u0435\u0441\u0442\u0432\u043e \u0432\u0435\u0440\u0448\u0438\u043d \u0433\u0440\u0430\u0444\u0430.<\/p>\n<p>\u0414\u043b\u044f \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0434\u0430\u043d\u043d\u043e\u0439 \u0437\u0430\u0434\u0430\u0447\u0438, \u0441\u043d\u0430\u0447\u0430\u043b\u0430 \u043d\u0430\u0439\u0434\u0451\u043c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u044f \u043c\u0435\u0436\u0434\u0443 \u0432\u0441\u0435\u043c\u0438 \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u043c\u0438 \u0434\u043b\u044f \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u0438\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430\u043c\u0438 \u0438 \u043e\u0441\u0442\u0430\u043b\u044c\u043d\u044b\u043c\u0438 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0414\u0435\u0439\u043a\u0441\u0442\u0440\u044b. \u0417\u0430\u0442\u0435\u043c \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043a\u0430\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u0441\u0443\u043c\u043c\u0443 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0439 \u0438\u0437\u043c\u0435\u043d\u044f\u044f \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u0438\u044f \u0432\u0435\u0440\u0448\u0438\u043d.<\/p>\n<p>\u042d\u0442\u0443 \u0437\u0430\u0434\u0430\u0447\u0443 \u043c\u043e\u0436\u043d\u043e \u0432\u0441\u0442\u0440\u0435\u0442\u0438\u0442\u044c \u0438 \u043d\u0430 \u043f\u0440\u0430\u043a\u0442\u0438\u043a\u0435. \u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u043e\u0438\u0441\u043a \u043c\u0430\u0440\u0448\u0440\u0443\u0442\u0430 \u0434\u043b\u044f \u043f\u043e\u043b\u0435\u0442\u0430 \u043a\u0432\u0430\u0434\u0440\u043e\u043a\u043e\u043f\u0442\u0435\u0440\u0430-\u043a\u0443\u0440\u044c\u0435\u0440\u0430.<\/p>\n<h2>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0433\u0440\u0430\u0444\u0430<\/h2>\n<p>\u0413\u0440\u0430\u0444 \u0441 N \u0432\u0435\u0440\u0448\u0438\u043d\u0430\u043c\u0438 \u0431\u0443\u0434\u0435\u0442 \u0445\u0440\u0430\u043d\u0438\u0442\u044c\u0441\u044f \u0432 \u043c\u0430\u0442\u0440\u0438\u0446\u0435 \u0440\u0430\u0437\u043c\u0435\u0440\u0430 NxN.<\/p>\n<p>\u041f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e  \u0437\u0430\u0434\u0430\u0434\u0438\u043c \u0433\u0440\u0430\u0444 \u043f\u043e\u043a\u0430\u0437\u0430\u043d\u043d\u044b\u0439 \u043d\u0430 \u0440\u0438\u0441\u0443\u043d\u043a\u0435 \u043d\u0438\u0436\u0435:<\/p>\n<figure class=\"\"><figcaption>\u0413\u0440\u0430\u0444 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e<\/figcaption><\/figure>\n<p><strong>Graph.h:<\/strong><\/p>\n<pre><code class=\"cpp\">#ifndef GRAPH_H #define GRAPH_H  #include &lt;iostream> #include &lt;iomanip>  using namespace std;  class Graph { private:     int n;                          \/\/ \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0435\u0440\u0448\u0438\u043d \u0433\u0440\u0430\u0444\u0430     int** data;                     \/\/ \u0434\u0430\u043d\u043d\u044b\u0435 \u043e \u0440\u0435\u0431\u0440\u0430\u0445 \u0433\u0440\u0430\u0444\u0430: -1 - \u043e\u0442\u0441\u0443\u0442\u0441\u0442\u0432\u0443\u0435\u0442 \u0440\u0435\u0431\u0440\u043e; >0 - \u0440\u0430\u0441\u0442\u0442\u043e\u044f\u043d\u0438\u0435 \u043c\u0435\u0436\u0434\u0443 \u0432\u0435\u0440\u0448\u0438\u043d\u0430\u043c\u0438 i \u0438 j      public:     Graph(int n);                   \/\/ \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0433\u0440\u0430\u0444\u0430 \u0441 n \u0432\u0435\u0440\u0448\u0438\u043d\u0430\u043c\u0438     Graph();                        \/\/ \u0433\u0440\u0430\u0444 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e     int** Get_data() const;         \/\/ \u0434\u0430\u043d\u043d\u044b\u0435 \u043e \u0433\u0440\u0430\u0444\u0435     int Get_n() const;              \/\/ \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0435\u0440\u0448\u0438\u043d \u0433\u0440\u0430\u0444\u0430  };  ostream&amp; operator&lt;&lt;(ostream&amp; os, const Graph&amp; g);  #endif <\/code><\/pre>\n<p><strong>Graph.cpp<\/strong>:<\/p>\n<pre><code class=\"cpp\">#include \"Graph.h\"  Graph::Graph() {     this->n = 5;     data = new int*[5];     for (int i = 0; i &lt; 5; i++) {         data[i] = new int[5];     }      \/\/ \u0433\u0440\u0430\u0444 \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e     data[0][0] = 0;    data[0][1] = 2;    data[0][2] = -1;    data[0][3] = 3;    data[0][4] = -1;     data[1][0] =2;     data[1][1] = 0;    data[1][2] = 4;     data[1][3] = 4;    data[1][4] = -1;     data[2][0] = -1;   data[2][1] = 4;    data[2][2] = 0;     data[2][3] = 5;    data[2][4] = 6;     data[3][0] = 3;    data[3][1] = 4;    data[3][2] = 5;    data[3][3] = 0;    data[3][4] = -1;     data[4][0] = -1;   data[4][1] = -1;   data[4][2] = 6;     data[4][3] = -1;   data[4][4] = 0; }  \/\/ \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u0435 \u0433\u0440\u0430\u0444\u0430 \u0441 n \u0432\u0435\u0440\u0448\u0438\u043d\u0430\u043c\u0438 Graph::Graph(int n) {     this->n = n;     data = new int*[n];     for (int i = 0; i &lt; n; i++) {         data[i] = new int[n];     }     for (int i = 0; i &lt; n; i++) {         for (int j = i + 1; j &lt; n; j++) {             cout &lt;&lt; \"Input \" &lt;&lt; i &lt;&lt; '-' &lt;&lt; j &lt;&lt; \": \";             cin >> data[i][j];             data[j][i] = data[i][j];         }     } }  \/\/ \u0434\u0430\u043d\u043d\u044b\u0435 \u043e \u0433\u0440\u0430\u0444\u0435 int** Graph::Get_data() const { return data; }  \/\/ \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0435\u0440\u0448\u0438\u043d \u0433\u0440\u0430\u0444\u0430 int Graph::Get_n() const { return n; }  \/\/ \u0432\u044b\u0432\u043e\u0434 \u0433\u0440\u0430\u0444\u0430 ostream&amp; operator&lt;&lt;(ostream&amp; os, const Graph&amp; g) {     int** d = g.Get_data();          for (int i = 0; i &lt; g.Get_n(); i++) {         for (int j = 0; j &lt; g.Get_n(); j++) {             os &lt;&lt; setw(4) &lt;&lt; d[i][j] &lt;&lt; ' ';         }         os &lt;&lt; endl;     }     return os; } <\/code><\/pre>\n<h2>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u043f\u0443\u0442\u0438<\/h2>\n<p>\u041f\u0443\u0442\u044c \u0445\u0440\u0430\u043d\u0438\u0442\u0441\u044f \u0432 \u0432\u0438\u0434\u0435 \u0432\u0435\u043a\u0442\u043e\u0440\u0430, \u0442\u0430\u043a\u0436\u0435 \u044f \u0445\u0440\u0430\u043d\u044e \u0434\u043b\u0438\u043d\u0443 \u0432\u0441\u0435\u0433\u043e \u043f\u0443\u0442\u0438. \u0420\u0435\u0430\u043b\u0438\u0437\u0443\u0435\u043c \u0442\u0430\u043a\u0436\u0435 \u0444\u0443\u043d\u043a\u0446\u0438\u0438 \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0432 \u043a\u043e\u043d\u0435\u0446 \u043f\u0443\u0442\u0438 \u043e\u0434\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0438 \u0434\u0440\u0443\u0433\u043e\u0433\u043e \u043f\u0443\u0442\u0438 (\u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0434\u0430\u043b\u044c\u0448\u0435 \u0432 \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0435 \u0414\u0435\u0439\u043a\u0441\u0442\u0440\u044b). <\/p>\n<p><strong>Path.h:<\/strong><\/p>\n<pre><code class=\"cpp\">#ifndef PATH_H #define PATH_H  #include &lt;iostream> #include &lt;set> #include &lt;vector>  using namespace std;  class Path { private:     vector&lt;int> path;                   \/\/ \u043f\u0443\u0442\u044c     int length;                         \/\/ \u0434\u043b\u0438\u043d\u0430 \u043f\u0443\u0442\u0438  public:     Path(int start);     Path&amp; operator= (const Path&amp; p);     vector&lt;int> Get_path() const;       \/\/ \u043f\u0443\u0442\u044c     int Get_length() const;             \/\/ \u0434\u043b\u0438\u043d\u0430 \u043f\u0443\u0442\u0438     void push_back(int v, int l);       \/\/ \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0432 \u043a\u043e\u043d\u0435\u0446     void push_back(Path p);             \/\/ \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u0443\u0442\u0438 \u0432 \u043a\u043e\u043d\u0435\u0446  };  ostream&amp; operator&lt;&lt;(ostream&amp; os, const Path&amp; p);  #endif <\/code><\/pre>\n<p><strong>Path.cpp:<\/strong><\/p>\n<pre><code class=\"cpp\">#include \"Path.h\"  Path::Path(int start) {     path.push_back(start);     length = 0; }  Path&amp; Path::operator=(const Path&amp; p) {     path = p.Get_path();     length = p.Get_length();     return *this; }  \/\/ \u043f\u0443\u0442\u044c vector&lt;int> Path::Get_path() const { return path; }  \/\/ \u0434\u043b\u0438\u043d\u0430 \u043f\u0443\u0442\u0438 int Path::Get_length() const { return length; }  \/\/ \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0432 \u043a\u043e\u043d\u0435\u0446 void Path::push_back(int v, int l) {     path.push_back(v);     length += l; }  \/\/ \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u0435 \u043f\u0443\u0442\u0438 \u0432 \u043a\u043e\u043d\u0435\u0446 void Path::push_back(Path p) {     for (size_t i = 0; i &lt; p.Get_path().size(); ++i) {         int last = path[path.size() - 1];         if (last != p.Get_path()[i]) {             path.push_back(p.Get_path()[i]);         }     }     length += p.Get_length(); }  ostream&amp; operator&lt;&lt;(ostream&amp; os, const Path&amp; p) {     size_t n = p.Get_path().size();     for (size_t i = 0; i &lt; n; i++) {         os &lt;&lt; p.Get_path()[i] + 1;         if (i != n - 1) {             os &lt;&lt; \"->\";         } else {             os &lt;&lt; \" (\";         }     }     os &lt;&lt; p.Get_length() &lt;&lt; ')' &lt;&lt; endl;     return os; } <\/code><\/pre>\n<h2>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0414\u0435\u0439\u043a\u0441\u0442\u0440\u044b<\/h2>\n<p>\u041e\u0431\u043e\u0437\u043d\u0430\u0447\u0438\u043c \u0432\u0441\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u0431\u0435\u0441\u043a\u043e\u043d\u0435\u0447\u043d\u043e\u0441\u0442\u044c\u044e, \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u043e\u0437\u043d\u0430\u0447\u0430\u0442\u044c, \u0447\u0442\u043e \u0432\u0435\u0440\u0448\u0438\u043d\u0430 \u0435\u0449\u0451 \u043d\u0435 \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u0430, \u0430 \u043d\u0430\u0447\u0430\u043b\u044c\u043d\u0443\u044e 0. <s>\u041e\u0447\u0435\u0432\u0438\u0434\u043d\u043e<\/s>, \u0447\u0442\u043e \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0434\u043e \u0441\u0430\u043c\u043e\u0439 \u0441\u0435\u0431\u044f \u0440\u0430\u0432\u043d\u043e 0. \u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u0439\u0434\u0435\u043c \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u0438\u0437 \u0438\u0441\u0445\u043e\u0434\u043d\u043e\u0439 \u0442\u043e\u0447\u043a\u0438 \u0432 \u0441\u043e\u0441\u0435\u0434\u043d\u0438\u0435, \u0437\u0430\u0442\u0435\u043c \u043e\u0442\u043c\u0435\u0442\u0438\u043c \u0435\u0451. \u0414\u0430\u043b\u0435\u0435 \u043d\u0430 \u043a\u0430\u0436\u0434\u043e\u043c \u0448\u0430\u0433\u0435 \u043d\u0430\u0445\u043e\u0434\u0438\u043c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u0443\u044e \u043d\u0435 \u043e\u0442\u043c\u0435\u0447\u0435\u043d\u043d\u0443\u044e \u0442\u043e\u0447\u043a\u0443 \u0438 \u043d\u0430\u0445\u043e\u0434\u0438\u043c \u0441\u0443\u043c\u043c\u0443 \u0441 \u0441\u043e\u0441\u0435\u0434\u043d\u0438\u043c\u0438, \u043d\u0435 \u043e\u0442\u043c\u0435\u0447\u0435\u043d\u043d\u044b\u043c\u0438 \u0438 \u0441\u0440\u0430\u0432\u043d\u0438\u0432\u0430\u0435\u043c \u0435\u0435 \u0441\u043e \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u0432\u0435\u0440\u0448\u0438\u043d\u044b, \u0435\u0441\u043b\u0438 \u043f\u043e\u043b\u0443\u0447\u0435\u043d\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043c\u0435\u043d\u044c\u0448\u0435, \u0442\u043e \u0437\u0430\u043c\u0435\u043d\u044f\u0435\u043c \u0435\u0433\u043e. <\/p>\n<h2>\u0420\u0435\u0430\u043b\u0438\u0437\u0430\u0446\u0438\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0414\u0435\u0439\u043a\u0441\u0442\u0440\u044b \u0438 \u0440\u0435\u0448\u0435\u043d\u0438\u044f \u0437\u0430\u0434\u0430\u0447\u0438 \u043a\u043e\u043c\u043c\u0438\u0432\u043e\u044f\u0436\u0451\u0440\u0430<\/h2>\n<p>\u0410\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0414\u0435\u0439\u043a\u0441\u0442\u0440\u044b \u043c\u044b \u0443\u0436\u0435 \u0440\u0430\u0437\u043e\u0431\u0440\u0430\u043b\u0438, \u043d\u043e \u043f\u0440\u0438 \u0435\u0433\u043e \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u0438 \u044f \u0442\u0430\u043a\u0436\u0435 \u0441\u0440\u0430\u0437\u0443 \u0441\u043e\u0445\u0440\u0430\u043d\u044f\u044e \u043a\u0440\u0430\u0442\u0447\u0430\u0439\u0448\u0438\u0439 \u043f\u0443\u0442\u044c \u043e\u0442 \u0434\u0430\u043d\u043d\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043a \u0434\u0440\u0443\u0433\u0438\u043c. \u0415\u0441\u043b\u0438 \u0432\u0435\u0441 \u0432\u0435\u0440\u0448\u0438\u043d\u044b \u043e\u043a\u0430\u0437\u0430\u043b\u0441\u044f \u0431\u043e\u043b\u044c\u0448\u0435, \u0447\u0435\u043c \u043d\u043e\u0432\u044b\u0439, \u0442\u043e \u043a\u0440\u043e\u043c\u0435 \u043d\u043e\u0432\u043e\u0433\u043e \u0432\u0435\u0441\u0430, \u043c\u044b \u0442\u0430\u043a\u0436\u0435 \u0437\u0430\u043c\u0435\u043d\u044f\u0435\u043c \u043f\u0443\u0442\u044c: \u043e\u043d \u0431\u0443\u0434\u0435\u0442 \u0440\u0430\u0432\u0435\u043d \u043f\u0443\u0442\u0438 \u043a \u043f\u0440\u0435\u0434\u044b\u0434\u0443\u0449\u0435\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u0435 \u043f\u043b\u044e\u0441 \u0441\u0430\u043c\u0430 \u0432\u0435\u0440\u0448\u0438\u043d\u0430.<\/p>\n<p>\u0412\u0441\u0435 \u043f\u0443\u0442\u0438 \u044f \u0437\u0430\u043f\u0438\u0441\u044b\u0432\u0430\u044e \u0432 \u0441\u043b\u043e\u0432\u0430\u0440\u044c, \u043a\u043b\u044e\u0447\u043e\u043c \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u0432\u0435\u0440\u0448\u0438\u043d\u0430, \u0430 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u0435\u043c \u043c\u0430\u0441\u0441\u0438\u0432 \u043f\u0443\u0442\u0435\u0439 (i-\u044b\u0439 \u044d\u043b\u0435\u043c\u0435\u043d\u0442 \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0432\u0443\u0435\u0442 \u043a\u0440\u043e\u0442\u0447\u0430\u0439\u0448\u0435\u043c\u0443 \u043f\u0443\u0442\u0438 \u0434\u043e (i+1)-\u043e\u0439 \u0432\u0435\u0440\u0448\u0438\u043d\u044b).<\/p>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c\u0430 \u0414\u0435\u0439\u043a\u0441\u0442\u0440\u044b \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e next_permutation \u044f \u043f\u0435\u0440\u0435\u0431\u0438\u0440\u0430\u044e \u043f\u043e\u0440\u044f\u0434\u043e\u043a \u043f\u043e\u0441\u0435\u0449\u0435\u043d\u0438\u044f \u0432\u0435\u0440\u0448\u0438\u043d \u0438 \u043d\u0430\u0445\u043e\u0436\u0443 \u043f\u0443\u0442\u044c \u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0439 \u0434\u043b\u0438\u043d\u044b.<\/p>\n<p><strong>Solution.h:<\/strong><\/p>\n<pre><code class=\"cpp\">#ifndef SOLUTION_H #define SOLUTION_H  #include \"Graph.h\" #include \"Path.h\" #include &lt;iostream> #include &lt;limits> #include &lt;vector> #include &lt;map> #include &lt;algorithm>  using namespace std;  class Solution { private:     int n;                                      \/\/ \u043a\u043e\u043b\u0438\u0447\u0435\u0441\u0442\u0432\u043e \u0432\u0435\u0440\u0448\u0438\u043d     int** graph;                                \/\/ \u0433\u0440\u0430\u0444 \u0432 \u0432\u0438\u0434\u0435 \u0434\u0432\u043e\u0438\u0447\u043d\u043e\u0433\u043e \u043c\u0430\u0441\u0441\u0438\u0432\u0430 n \u043d\u0430 n  public:     Solution(const Graph&amp; g);     Path* Dijkstra(int v, Path* data);          \/\/ \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0414\u0435\u0439\u043a\u0441\u0442\u0440\u044b     Path calc(vector&lt;int> v, int start);        \/\/ \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u0443\u0442\u0438  };  #endif <\/code><\/pre>\n<p><strong>Solution.cpp:<\/strong><\/p>\n<pre><code class=\"cpp\">#include \"Solution.h\"  Solution::Solution(const Graph&amp; g) {     graph = g.Get_data();     n = g.Get_n(); }  \/\/ \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0414\u0435\u0439\u043a\u0441\u0442\u0440\u044b Path* Solution::Dijkstra(int v, Path* data) {     data = (Path*)calloc(n, sizeof(Path));      \/\/\u043f\u0443\u0442\u0438 \u043e\u0442 \u0432\u0435\u0440\u0448\u0438\u043d\u044b v \u0434\u043e \u0434\u0440\u0443\u0433\u0438\u0445 \u0432\u0435\u0440\u0448\u0438\u043d \u0433\u0440\u0430\u0444\u0430     int distaces[n];                            \/\/\u043c\u0438\u043d\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0435 \u0440\u0430\u0441\u0441\u0442\u043e\u044f\u043d\u0438\u0435 \u043e\u0442 \u0432\u0435\u0440\u0448\u0438\u043d\u044b v \u0434\u043e \u0434\u0440\u0443\u0433\u0438\u0445     int out[n];                                 \/\/\u043f\u043e\u0441\u0435\u0449\u0435\u043d\u043d\u044b\u0435 \u0432\u0435\u0440\u0448\u0438\u043d\u044b     for (int i = 0; i &lt; n; ++i) {         if (i == v) {             distaces[i] = 0;             data[i] = Path(v);         } else {             distaces[i] = numeric_limits&lt;int>::max();         }         out[i] = 0;     }      int min = numeric_limits&lt;int>::max(), index = -1;     do {         min = numeric_limits&lt;int>::max();         for (int i = 0; i &lt; n; ++i) {             if ((out[i] == 0) &amp;&amp; (distaces[i] &lt; min)) {                 min = distaces[i];                 index = i;             }         }         if (min != numeric_limits&lt;int>::max()) {             for (int i = 0; i &lt; n; ++i) {                 if (graph[index][i] > 0) {                     int temp = min + graph[index][i];                     if (temp &lt; distaces[i]) {                         distaces[i] = temp;                         data[i] = data[index];                         data[i].push_back(i, graph[index][i]);                     }                 }             }             out[index] = 1;         }     } while (min &lt; numeric_limits&lt;int>::max());      return data; }  \/\/ \u0432\u044b\u0447\u0438\u0441\u043b\u0435\u043d\u0438\u0435 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u043e\u0433\u043e \u043f\u0443\u0442\u0438 Path Solution::calc(vector&lt;int> v, int start) {     map&lt;int, Path*> distaces;     Path* temp = (Path*)calloc(n, sizeof(Path));     temp = Dijkstra(start, temp);     distaces[start] = temp;     for (size_t i = 0; i &lt; v.size(); ++i) {         temp = Dijkstra(v[i], temp);         distaces[v[i]] = temp;     }      Path p = Path(start);     sort(v.begin(), v.end());     for (size_t i = 0; i &lt; v.size(); ++i) {         int last = p.Get_path()[p.Get_path().size() - 1];         p.push_back(distaces[last][v[i]]);     }     int last = p.Get_path()[p.Get_path().size() - 1];     p.push_back(distaces[last][start]);     next_permutation(v.begin(), v.end());      do {         Path temp = Path(start);         for (size_t i = 0; i &lt; v.size(); ++i) {             int last = temp.Get_path()[temp.Get_path().size() - 1];             temp.push_back(distaces[last][v[i]]);             if (temp.Get_length() > p.Get_length())                 break;         }         int last = temp.Get_path()[temp.Get_path().size() - 1];         temp.push_back(distaces[last][start]);          if (temp.Get_length() &lt; p.Get_length()) {             p = temp;         }     } while (next_permutation(v.begin(), v.end()));      return p; } <\/code><\/pre>\n<h2>\u0418\u0442\u043e\u0433\u0438<\/h2>\n<p>\u0414\u0430\u043d\u043d\u043e\u0435 \u0440\u0435\u0448\u0435\u043d\u0438\u0435 \u0434\u0430\u043b\u0435\u043a\u043e \u043e\u0442 \u0438\u0434\u0435\u0430\u043b\u044c\u043d\u043e\u0433\u043e, \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u0440\u0438\u0434\u0443\u043c\u0430\u0442\u044c \u0431\u043e\u043b\u0435\u0435 \u043e\u043f\u0442\u0438\u043c\u0430\u043b\u044c\u043d\u044b\u0439 \u043c\u0435\u0442\u043e\u0434 \u043d\u0430\u0445\u043e\u0436\u0434\u0435\u043d\u0438\u044f \u043f\u043e\u0440\u044f\u0434\u043a\u0430 \u043e\u0431\u0445\u043e\u0434\u0430 \u0432\u0435\u0440\u0448\u0438\u043d. \u0422\u0430\u043a\u0436\u0435 \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u044f \u043e\u043f\u0442\u0438\u043c\u0438\u0437\u0438\u0440\u0443\u044e \u0430\u043b\u0433\u043e\u0440\u0438\u0442\u043c \u0414\u0435\u0439\u043a\u0441\u0442\u0440\u044b, \u0443\u043c\u0435\u043d\u044c\u0448\u0438\u0432 \u0435\u0433\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e \u0424\u0438\u0431\u043e\u043d\u0430\u0447\u0447\u0438\u0435\u0432\u043e\u0439 \u043a\u0443\u0447\u0438.<\/p>\n<p>\u041f\u0440\u043e\u0435\u043a\u0442 \u0437\u0430\u0434\u0430\u0447\u0438 \u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u043a\u0430\u0447\u0430\u0442\u044c \u043d\u0430 <a href=\"https:\/\/github.com\/MMMuzahim\/travelling-salesman-problem\" rel=\"noopener noreferrer nofollow\">GitHub<\/a>.<\/p>\n<\/div>\n<\/div>\n<\/div>\n<div class=\"v-portal\" style=\"display:none;\"><\/div>\n<\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/650643\/\"> https:\/\/habr.com\/ru\/post\/650643\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-329321","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/329321","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=329321"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/329321\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=329321"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=329321"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=329321"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}