#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector<vector<int>> c(n, vector<int>(n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
cin >> c[i][j];
}
}
int ans = INT_MAX;
vector<int> dp((1<<n), 1e9);
dp[0] = 0, dp[(1<<n)-1] = 0;
for (int mask = (1<<n); mask >= 0; mask--) {
if (__builtin_popcount(mask) == k) continue;
for (int i = 0; i < n; i++) {
if (!(mask & (1<<i))) continue;
// try pouring contents of this glass into every other glass
for (int j = 0; j < n; j++) {
if (i != j && mask & (1<<j))
dp[mask-(1<<i)] = min(dp[mask-(1<<i)], dp[mask]+c[i][j]);
}
}
}
for (int mask = 0; mask < (1<<n); mask++) {
if (__builtin_popcount(mask) == k) ans = min(ans, dp[mask]);
}
cout << ans << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
5 ms |
348 KB |
Output is correct |
6 |
Correct |
12 ms |
348 KB |
Output is correct |
7 |
Correct |
25 ms |
604 KB |
Output is correct |
8 |
Correct |
56 ms |
860 KB |
Output is correct |
9 |
Correct |
636 ms |
4444 KB |
Output is correct |
10 |
Correct |
528 ms |
4536 KB |
Output is correct |