#include <bits/stdc++.h>
using namespace std;
template<class ISqr, class T>
ISqr& operator>>(ISqr& is, vector<T>& v) { for (auto& x : v) is >> x; return is; }
#define SPEED \
ios_base::sync_with_stdio(0); \
cin.tie(NULL); \
cout.tie(NULL);
#define pb push_back
#define ins insert
#define fi first
#define se second
#define endl "\n"
#define ALL(x) x.begin(), x.end()
#define sz(x) x.size()
#define intt long long
#define cntone(x) __builtin_popcount(x)
const intt maksN = 2e5 + 31;
const intt inf = 1e18;
const intt mod = 1e9 + 7;
intt dx[4] = {1, -1, 0, 0};
intt dy[4] = {0, 0, 1, -1};
void solve() {
intt n, m;
cin >> n >> m;
vector<vector<intt>>C(n + 1, vector<intt>(n + 1, 0ll));
vector<intt>dp((1 << n), inf);
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
cin >> C[i][j];
}
}
if(n == m) {
cout << 0 << endl;
return;
}
dp[(1<<n) - 1] = 0ll;
intt ans = inf;
for(intt mask = (1ll << n) - 1; mask >= 0; mask--) {
if(cntone(mask) <= m) {
ans = min(ans, dp[mask]);
continue;
}
vector<intt>idxs;
for(intt i = 0; i < n; i++) {
if(((1 << i) & mask)) {
idxs.pb(i);
}
}
for(intt i = 0; i < idxs.size(); i++) {
for(int j =0; j < idxs.size(); j++) {
intt num1 = idxs[i], num2 = idxs[j];
if(num1 == num2) continue;
dp[mask ^ (1 << num1)] = min(dp[mask ^ (1 << num1)], dp[mask] + C[num1][num2]);
}
}
}
cout << ans << endl;
}
signed main() {
SPEED;
intt tst = 1;
// cin >> tst;
while (tst--) {
solve();
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |