Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.16 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. const int N = 100;
  6. vector<pair<int, int>> g[N];
  7. int dist[N][N];
  8. int pre[N][N];
  9.  
  10. int main() {
  11.     ios::sync_with_stdio(false);
  12.     while (true) {
  13.         int n, m;
  14.         cin >> n;
  15.         if (n == -1) {
  16.             break;
  17.         }
  18.         for (int a = 0; a < n; ++a) {
  19.             g[a].clear();
  20.             dist[a][a] = 0;
  21.             pre[a][a] = -1;
  22.             for (int b = a + 1; b < n; ++b) {
  23.                 dist[a][b] = -1;
  24.                 dist[b][a] = -1;
  25.             }
  26.         }
  27.         cin >> m;
  28.         for (int i = 0; i < m; ++i) {
  29.             int a, b, d;
  30.             cin >> a >> b >> d;
  31.             --a, --b;
  32.             g[a].emplace_back(b, d);
  33.             g[b].emplace_back(a, d);
  34.         }
  35.         int mlen = -1;
  36.         vector<int> path;
  37.         for (int curr = 0; curr < n; ++curr) {
  38.             for (int i = 0; i < g[curr].size(); ++i) {
  39.                 int a = g[curr][i].first, ad = g[curr][i].second;
  40.                 dist[curr][a] = ad;
  41.                 dist[a][curr] = ad;
  42.                 pre[curr][a] = curr;
  43.                 pre[a][curr] = a;
  44.                 if (a < curr) {
  45.                     for (int j = 0; j < i; ++j) {
  46.                         int b = g[curr][j].first, bd = g[curr][j].second;
  47.                         if (a != b && dist[a][b] != -1 && (mlen == -1 || dist[a][b] + ad + bd < mlen)) {
  48.                             mlen = dist[a][b] + ad + bd;
  49.                             path.clear();
  50.                             for (int node = b; node != -1; node = pre[a][node]) {
  51.                                 path.emplace_back(node);
  52.                             }
  53.                             path.emplace_back(curr);
  54.                         }
  55.                     }
  56.                 }
  57.             }
  58.             for (auto conn: g[curr]) {
  59.                 int a = conn.first;
  60.                 if (a < curr) {
  61.                     for (int b = 0; b < curr; ++b) {
  62.                         if (dist[a][b] != -1 && (dist[curr][b] == -1 || dist[curr][a] + dist[a][b] < dist[curr][b])) {
  63.                             dist[curr][b] = dist[curr][a] + dist[a][b];
  64.                             dist[b][curr] = dist[b][a] + dist[a][curr];
  65.                             pre[curr][b] = pre[a][b];
  66.                             pre[b][curr] = pre[a][curr];
  67.                         }
  68.                     }
  69.                 }
  70.             }
  71.             for (int a = 1; a < curr; ++a) {
  72.                 for (int b = 0; b < a; ++b) {
  73.                     if (dist[a][curr] != -1 && dist[curr][b] != -1 && (dist[a][b] == -1 || dist[a][curr] + dist[curr][b] < dist[a][b])) {
  74.                         dist[a][b] = dist[a][curr] + dist[curr][b];
  75.                         dist[b][a] = dist[b][curr] + dist[curr][a];
  76.                         pre[a][b] = pre[curr][b];
  77.                         pre[b][a] = pre[curr][a];
  78.                     }
  79.                 }
  80.             }
  81.         }
  82.         if (mlen == -1) {
  83.             cout << "No solution.";
  84.         } else {
  85.             for (int node: path) {
  86.                 cout << node + 1 << ' ';
  87.             }
  88.         }
  89.         cout << '\n';
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement