#include <stdio.h>
#include <string.h>
#define N 20
#define INF 0x3f3f3f3f
int min(int a, int b) { return a < b ? a : b; }
int count(int b) {
return b == 0 ? 0 : count(b & b - 1) + 1;
}
int main() {
static int aa[N][N], bb[N][N], dp[1 << N];
int n, k, i, j, b, ans;
scanf("%d%d", &n, &k);
for (i = 0; i < n; i++) {
for (j = 0; j < n; j++) {
scanf("%d", &aa[i][j]);
if (i == j)
aa[i][j] = INF;
}
for (j = n - 1; j >= 0; j--)
bb[i][j] = min(j + 1 == n ? INF : bb[i][j + 1], aa[i][j]);
}
memset(dp, 0x3f, (1 << n) * sizeof *dp), dp[(1 << n) - 1] = 0;
ans = INF;
for (b = (1 << n) - 1; b > 0; b--) {
if (count(b) <= k)
ans = min(ans, dp[b]);
for (i = 0; i < n; i++)
if ((b & 1 << i) != 0)
dp[b ^ 1 << i] = min(dp[b ^ 1 << i], dp[b] + bb[i][0]);
j = 0;
while ((b & 1 << j) == 0)
j++;
for (i = 0; i < n; i++)
bb[i][j] = j + 1 == n ? INF : bb[i][j + 1];
while (j--)
for (i = 0; i < n; i++)
bb[i][j] = min(bb[i][j + 1], aa[i][j]);
}
printf("%d\n", ans);
return 0;
}
Compilation message
kronican.c: In function 'count':
kronican.c:10:34: warning: suggest parentheses around '-' in operand of '&' [-Wparentheses]
10 | return b == 0 ? 0 : count(b & b - 1) + 1;
| ~~^~~
kronican.c: In function 'main':
kronican.c:17:2: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
17 | scanf("%d%d", &n, &k);
| ^~~~~~~~~~~~~~~~~~~~~
kronican.c:20:4: warning: ignoring return value of 'scanf' declared with attribute 'warn_unused_result' [-Wunused-result]
20 | scanf("%d", &aa[i][j]);
| ^~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
288 KB |
Output is correct |
5 |
Correct |
2 ms |
332 KB |
Output is correct |
6 |
Correct |
4 ms |
332 KB |
Output is correct |
7 |
Correct |
8 ms |
460 KB |
Output is correct |
8 |
Correct |
15 ms |
796 KB |
Output is correct |
9 |
Correct |
136 ms |
4360 KB |
Output is correct |
10 |
Correct |
162 ms |
4364 KB |
Output is correct |