Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- typedef long long int ll;
- #define OO 9999999999999999LL
- ll n, k, dp[1 << 21], a[22][22], res;
- ll solve(ll mask,ll on)
- {
- if (on == k)return 0;
- ll ans = OO, &r = dp[mask],sh;
- if (r != -1)return r;
- for (int i = 0; (1 << i) <= mask; i++)
- {
- if (((1 << i)&mask)==0)
- continue;
- for (int j = 0; (1 << j) <= mask; j++)
- {
- if (((1 << j)&mask)==0)
- continue;
- if (i == j)
- continue;
- //cout << "jdfkdjf";
- sh = solve((mask - (1 << i)), on - 1)+a[i][j];
- //if (sh == 0)cout << i << " " << j << endl;
- ans = min(ans, sh);
- }
- }
- return r=ans;
- }
- int main()
- {
- ios::sync_with_stdio(false);
- cin.tie(0);
- cout.tie(0);
- memset(dp, -1, sizeof dp);
- cin >> n >> k;
- for (int i = 0; i < n;i++)
- for (int j = 0; j < n; j++)
- cin >> a[i][j];
- ll e = (1 << n);
- e--;
- res = solve(e,n);
- cout << res << "\n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment