fork download
  1. #include <bits/stdc++.h>
  2.  
  3. #define ll long long
  4. #define el cout << '\n'
  5. #define BIT(n) (1ll << (n))
  6. #define bit(mask, i) (((mask) >> (i)) & 1)
  7.  
  8. using namespace std;
  9.  
  10. const int maxn = 12;
  11. const ll INF = 1e4;
  12.  
  13. int t;
  14. ll cost[maxn + 10][maxn + 10], dp[maxn + 10][BIT(maxn)];
  15.  
  16. void minimize(ll &a, ll b)
  17. {
  18. a = min(a, b);
  19. }
  20.  
  21. int main()
  22. {
  23. ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  24. if (fopen("TRUYENFILE.INP", "r"))
  25. {
  26. freopen("TRUYENFILE.INP", "r", stdin);
  27. freopen("TRUYENFILE.OUT", "w", stdout);
  28. }
  29.  
  30. cin >> t;
  31. while (t--)
  32. {
  33. int n;
  34. cin >> n;
  35. for (int i = 0; i < n; i++)
  36. for (int j = 0; j < n; j++)
  37. cin >> cost[i][j];
  38. for (int i = 0; i < n; i++)
  39. for (int mask = 0; mask < BIT(n); mask++)
  40. dp[i][mask] = INF;
  41. for (int i = 0; i < n; i++)
  42. dp[i][0] = dp[i][BIT(i)] = 0;
  43. for (int mask = 0; mask < BIT(n); mask++)
  44. for (int i = 0; i < n; i++)
  45. if (bit(mask, i))
  46. for (int submask = mask; submask; submask = (submask - 1) & mask)
  47. for (int j = 0; j < n; j++)
  48. if (bit(submask, j))
  49. minimize(dp[i][mask], cost[i][j] + max(dp[j][submask], dp[i][mask ^ submask]));
  50. // for (int i = 0; i < n; i++)
  51. // {
  52. // for (int mask = 0; mask < BIT(n); mask++)
  53. // cout << dp[i][mask] << ' ';
  54. // el;
  55. // }
  56. cout << dp[0][BIT(n) - 1], el;
  57. }
  58. }
  59.  
Success #stdin #stdout 0.01s 5288KB
stdin
Standard input is empty
stdout
Standard output is empty