#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> edge;
int w[20][20];
int dp[1<<20];
void solve() {
int n, k;
cin >> n >> k;
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
cin >> w[i][j];
}
}
int ret = 1 << 30;
for(int mask = 0; mask < (1<<n); mask++) {
dp[mask] = 1 << 30;
}
dp[(1<<n)-1] = 0;
for(int mask = (1<<n)-1; mask >= 0; mask--) {
int bits = __builtin_popcount(mask);
if(bits == k) {
ret = min(ret, dp[mask]);
}
if(bits <= k) continue;
for(int i = 0; i < n; i++) {
if(!(mask&(1<<i))) continue;
for(int j = 0; j < n; j++) {
if(i!=j && (mask&(1<<j))) {
dp[mask^(1<<i)] = min(dp[mask^(1<<i)], dp[mask] + w[i][j]);
}
}
}
}
cout << ret << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
448 KB |
Output is correct |
4 |
Correct |
2 ms |
620 KB |
Output is correct |
5 |
Correct |
9 ms |
640 KB |
Output is correct |
6 |
Correct |
18 ms |
788 KB |
Output is correct |
7 |
Correct |
33 ms |
836 KB |
Output is correct |
8 |
Correct |
73 ms |
1236 KB |
Output is correct |
9 |
Correct |
816 ms |
4820 KB |
Output is correct |
10 |
Correct |
72 ms |
4820 KB |
Output is correct |