Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- const int N = 20;
- int n;
- int a[N][N];
- int cache[N][1 << N];
- int FULL;
- bool get_bit(int x, int k) {
- return (x >> k) & 1;
- }
- int dp(int i, int mask) {
- if(mask == FULL) return a[i][1];
- if(cache[i][mask] != -1) return cache[i][mask];
- int ans = 1e8;
- for(int j = 1; j <= n; ++j) {
- if(get_bit(mask, j)) continue;
- ans = min(ans, dp(j, mask | (1 << j)) + a[i][j]);
- }
- return cache[i][mask] = ans;
- }
- int main() {
- //freopen("in.txt", "r", stdin);
- //freopen("BCTSP.inp", "r", stdin);
- //freopen("BCTSP.out", "w", stdout);
- ios_base::sync_with_stdio(false);
- cin.tie(NULL); cout.tie(NULL);
- cin >> n;
- for(int i = 1; i <= n; ++i)
- for(int j = 1; j <= n; ++j)
- cin >> a[i][j];
- FULL = ((1 << n) - 1) << 1;
- memset(cache, -1, sizeof(cache));
- cout << dp(1, 1 << 1);
- return 0;
- }
Add Comment
Please, Sign In to add comment