SHARE
TWEET

Untitled

lalalalalalalaalalla Nov 9th, 2019 91 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.     assert(n < 15);
  62.     vector<vector<int>> g(n, vector<int>(n));
  63.     for (int i = 0; i < n; i++) {
  64.         for (int j = 0; j < n; j++) cin >> g[i][j];
  65.     }
  66.     vector<vector<int>> dp((1 << n), vector<int>(n, INF));
  67.     vector<vector<pii>> p((1 << n), vector<pii>(n, {-1, -1}));
  68.     set<pii> cur = {{1, 0}}, next;
  69.     dp[1][0] = 0;
  70.     for (int i = 1; i < n; i++) {
  71.         for (auto m : cur) {
  72.             for (int to = 0; to < n; to++) {
  73.                 if (g[m.second][to] == 0 || ((m.first >> to) & 1)) continue;
  74.                 dp[m.first | (1 << to)][to] = min(dp[m.first][m.second] + g[m.second][to], dp[m.first | (1 << to)][to]);
  75.                 next.insert({(m.first | (1 << to)), to});
  76.                 p[(m.first | (1 << to))][to] = {m.first, m.second};
  77.             }
  78.         }
  79.         cur = next;
  80.         next.clear();
  81.     }
  82. //    for (int i = 0; i < n; i++) {
  83. //        cout << dp[(1 << n) - 1][i] << "\n";
  84. //    }
  85.     int m = INF, curs = -1;
  86.     for (int i = 0; i < n; i++) {
  87.         if (dp[(1 << n) - 1][i] < m) {
  88.             curs = i;
  89.             m = dp[(1 << n) - 1][i];
  90.         }
  91.     }
  92.     int curf = (1 << n) - 1;
  93.     if (m == INF) {
  94.         cout << -1;
  95.         return 0;
  96.     }
  97.     vector<int> path = {};
  98.     pii ss;
  99.     while (curf != -1) {
  100.         path.pb(curs);
  101.         ss = p[curf][curs];
  102.         curf = ss.first;
  103.         curs = ss.second;
  104.     }
  105.     reverse(all(path));
  106.     assert(path.size() == n && path[0] == 0);
  107.     cout << m << "\n";
  108.     for (auto k : path) cout << k + 1 << " ";
  109. }
  110. /*
  111. 3
  112. 0 3 2
  113. 3 0 6
  114. 2 6 0
  115.  
  116. 5
  117. 0 6 4 0 0
  118. 6 0 7 0 7
  119. 4 7 0 0 0
  120. 0 0 0 0 2
  121. 0 7 0 2 0
  122. */
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