SHARE
TWEET

Untitled

lalalalalalalaalalla Nov 8th, 2019 81 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <iomanip>
  5. #include <queue>
  6. #include <cmath>
  7. #include <algorithm>
  8. #include <tuple>
  9. #include <iomanip>
  10. #include <stdio.h>
  11. #include <map>
  12. #include <bitset>
  13. #include <set>
  14. #include <stack>
  15. #include <queue>
  16. #include <unordered_set>
  17. #include <cassert>
  18. #include <stdlib.h>
  19. #include <time.h>
  20. #include <random>
  21.  
  22.  
  23. //#pragma GCC optimize("Ofast,no-stack-protector")
  24. //#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
  25. //#pragma GCC optimize("unroll-loops")
  26. //#pragma GCC optimize("fast-math")
  27. //#pragma GCC optimize("section-anchors")
  28. //#pragma GCC optimize("profile-values,profile-reorder-functions,tracer")
  29. //#pragma GCC optimize("vpt")
  30. //#pragma GCC optimize("rename-registers")
  31. //#pragma GCC optimize("move-loop-invariants")
  32. //#pragma GCC optimize("unswitch-loops")
  33. //#pragma GCC optimize("function-sections")
  34. //#pragma GCC optimize("data-sections")
  35. //#pragma GCC optimize("branch-target-load-optimize")
  36. //#pragma GCC optimize("branch-target-load-optimize2")
  37. //#pragma GCC optimize("btr-bb-exclusive")
  38.  
  39.  
  40. #define int long long
  41. #define ll long long
  42. #define ull unsigned long long
  43. #define all(a) a.begin(), a.end()
  44. #define pii pair<int, int>
  45. #define pb push_back
  46. #define ld long double
  47.  
  48.  
  49. using namespace std;
  50.  
  51. const int INF = 1e17;
  52. //const int mod = 2600000069;
  53. //const int p = 179;
  54.  
  55. signed main() {
  56.     ios_base::sync_with_stdio(0);
  57.     cin.tie(0);
  58.     cout.tie(0);
  59.     int n;
  60.     cin >> n;
  61.     vector<vector<int>> g(n, vector<int>(n));
  62.     for (int i = 0; i < n; i++) {
  63.         for (int j = 0; j < n; j++) cin >> g[i][j];
  64.     }
  65.     vector<vector<int>> dp((1 << n), vector<int>(n, INF));
  66.     vector<vector<pii>> p((1 << n), vector<pii>(n, {-1, -1}));
  67.     vector<pii> cur = {{1, 0}}, next;
  68.     dp[1][0] = 0;
  69.     for (int i = 0; i < n - 1; i++) {
  70.         for (auto m : cur) {
  71.             for (int to = 0; to < n; to++) {
  72.                 if (g[m.second][to] == 0 || ((m.first >> to) & 1)) continue;
  73.                 dp[m.first | (1 << to)][to] = min(dp[m.first][m.second] + g[m.second][to], dp[m.first | (1 << to)][to]);
  74.                 next.pb({(m.first | (1 << to)), to});
  75.                 p[(m.first | (1 << to))][to] = {m.first, m.second};
  76.             }
  77.         }
  78.         cur = next;
  79.         next.clear();
  80.     }
  81. //    for (int i = 0; i < n; i++) {
  82. //        cout << dp[(1 << n) - 1][i] << "\n";
  83. //    }
  84.     int m = INF, curs = -1;
  85.     for (int i = 0; i < n; i++) {
  86.         if (dp[(1 << n) - 1][i] < m) {
  87.             curs = i;
  88.             m = dp[(1 << n) - 1][i];
  89.         }
  90.     }
  91.     int curf = (1 << n) - 1;
  92.     if (curs == -1) {
  93.         cout << -1;
  94.         return 0;
  95.     }
  96.     vector<int> path = {};
  97.     pii ss;
  98.     while (curf != -1) {
  99.         path.pb(curs);
  100.         ss = p[curf][curs];
  101.         curf = ss.first;
  102.         curs = ss.second;
  103.     }
  104.     reverse(all(path));
  105.     cout << m << "\n";
  106.     for (auto k : path) cout << k + 1 << " ";
  107. }
  108. /*
  109. 3
  110. 0 3 2
  111. 3 0 6
  112. 2 6 0
  113.  
  114. 5
  115. 0 6 4 0 0
  116. 6 0 7 0 7
  117. 4 7 0 0 0
  118. 0 0 0 0 2
  119. 0 7 0 2 0
  120. */
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top