#include<bits/stdc++.h>
using namespace std;
#define int long long
#define ii pair <int, int>
#define app push_back
#define all(a) a.begin(), a.end()
#define bp __builtin_popcountll
#define ll long long
#define mp make_pair
#define f first
#define s second
#define Time (double)clock()/CLOCKS_PER_SEC
const int N = 21;
int a[N][N];
int dp[1 << N];
signed main() {
#ifdef HOME
freopen("input.txt", "r", stdin);
#else
#define endl '\n'
ios_base::sync_with_stdio(0); cin.tie(0);
#endif
int n, k;
cin >> n >> k;
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
cin >> a[i][j];
}
}
/*
for (int k = 0; k < n; ++k) {
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
}
}
}
*/
int all = (1 << n) - 1;
const int INF = 1e18 + 7;
for (int i = 0; i < (1 << N); ++i)
dp[i] = INF;
dp[all] = 0;
int ans = INF;
for (int mask = all; mask >= 0; --mask) {
if (bp(mask) <= k)
ans = min(ans, dp[mask]);
for (int i = 0; i < n; ++i) {
if ((mask >> i) & 1) {
for (int j = 0; j < n; ++j) {
if ((mask >> j) & 1) {
if (i != j) {
dp[mask ^ (1 << i)] = min(dp[mask ^ (1 << i)], dp[mask] + a[i][j]);
}
}
}
}
}
}
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
16768 KB |
Output is correct |
2 |
Correct |
11 ms |
16640 KB |
Output is correct |
3 |
Correct |
12 ms |
16720 KB |
Output is correct |
4 |
Correct |
12 ms |
16768 KB |
Output is correct |
5 |
Correct |
19 ms |
16640 KB |
Output is correct |
6 |
Correct |
27 ms |
16768 KB |
Output is correct |
7 |
Correct |
45 ms |
16768 KB |
Output is correct |
8 |
Correct |
86 ms |
16768 KB |
Output is correct |
9 |
Correct |
729 ms |
16768 KB |
Output is correct |
10 |
Correct |
726 ms |
16888 KB |
Output is correct |