fork download
  1. /*
  2. * Author: Geeza
  3. */
  4.  
  5. #include <bits/stdc++.h>
  6.  
  7. #define ld long double
  8. #define ll long long
  9. #define pb push_back
  10. #define fin(a, n) for(int i = a; i < n; i++)
  11. #define fjn(a, n) for(int j = a; j < n; j++)
  12. #define all(a) a.begin(),a.end()
  13. #define allr(a) a.rbegin(),a.rend()
  14. #define FAST ios_base::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr)
  15.  
  16. using namespace std;
  17.  
  18. const double PI = acos(-1);
  19. const int N = 1e6+5;
  20. const ll oo = 0x3f3f3f3f3f3f3f3f;
  21. const int mod = 998244353, inf = 1e6;
  22.  
  23. string di[] = {"D","L", "U", "R", "UL", "UR", "DL", "DR"};
  24. int dx[] = {+1, +0, +0, -1, -1, -1, +1, +1};
  25. int dy[] = {+0, -1, +1, +0, -1, +1, -1, +1};
  26. char dc[] = {'D', 'L', 'R', 'U'};
  27.  
  28. ll n;
  29. vector<vector<ll>> grid;
  30. ll dp[1<<11][12]; // mask, lst
  31.  
  32. ll calc(ll mask, ll lst) {
  33. if (mask == (1<<n)-1) {
  34. return grid[lst][0];
  35. }
  36.  
  37. ll &ret = dp[mask][lst];
  38. if (~ret)return ret;
  39. ret = oo;
  40.  
  41. for (int i = 1; i < n; i++) {
  42. if ((mask>>i)&1ll)continue;
  43. ret = min(ret, calc(mask|(1ll<<i), i)+grid[lst][i]);
  44. }
  45. return ret;
  46. }
  47.  
  48. void solve() {
  49. cin >> n; ++n;
  50. grid = vector<vector<ll>>(n, vector<ll>(n, 0));
  51. for (int i = 0; i < n; i++) {
  52. for (int j = 0; j < n; j++) {
  53. if (i==j)continue;
  54. cin>>grid[i][j];
  55. }
  56. }
  57. memset(dp, -1, sizeof dp);
  58. cout << calc(1, 0) << "\n";
  59. }
  60.  
  61. int main() {
  62. FAST;
  63. #ifndef ONLINE_JUDGE
  64. freopen("input.txt","r",stdin);
  65. freopen("output.txt","w",stdout);
  66. #endif
  67. int tt = 1; cin >> tt;
  68. while(tt--){
  69. solve();
  70. }
  71. return 0;
  72. }
Success #stdin #stdout 0s 5316KB
stdin
Standard input is empty
stdout
0