#include <bits/stdc++.h>
using namespace std;
const int md = 2e7 + 10;
const int oo = 1e9 + 10;
int f[md];
int n, k, c[30][30];
int getbit(int x, int i) {
return ((x >> i) & 1);
}
//1010011
//0010000
int offbit(int x, int i) {
return ((1 << i) ^ x);
}
int main() {
// freopen("test.in", "r", stdin);
//freopen("test.out", "w", stdout);
ios_base::sync_with_stdio(0);
cin >> n >> k;
if (k >= n) {
cout << "0";
return 0;
}
for(int i = 1; i <= n; i++) {
for(int j = 1; j <= n; j++)
cin >> c[i][j];
}
int res = oo;
int ss = (1 << n) - 1;
for(int i = 1; i <= ss; i++)
f[i] = oo;
f[ss] = 0;
for(int s = ss; s >= 1; s--) {
for(int i = 0; i < n; i++)
if (getbit(s, i)) {
int now = offbit(s, i);
for(int j = 0; j < n; j++)
if (getbit(now, j))
f[now] = min(f[now], f[s] + c[i + 1][j + 1]);
}
}
for(int i = 1; i <= ss; i++) {
int cnt = 0;
for(int j = 0; j < n; j++)
cnt += getbit(i, j);
if (cnt == k)
res = min(res, f[i]);
}
cout << res;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
376 KB |
Output is correct |
2 |
Correct |
2 ms |
376 KB |
Output is correct |
3 |
Correct |
2 ms |
376 KB |
Output is correct |
4 |
Correct |
2 ms |
376 KB |
Output is correct |
5 |
Correct |
10 ms |
452 KB |
Output is correct |
6 |
Correct |
20 ms |
632 KB |
Output is correct |
7 |
Correct |
41 ms |
632 KB |
Output is correct |
8 |
Correct |
87 ms |
888 KB |
Output is correct |
9 |
Correct |
866 ms |
4492 KB |
Output is correct |
10 |
Correct |
956 ms |
4600 KB |
Output is correct |