#include <bits/stdc++.h>
using namespace std;
int main() {
int n, k;
cin >> n >> k;
vector <vector <int>> adj(n, vector <int> (n));
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++)
cin >> adj[i][j];
}
vector <int> dp((1 << n) + 1, 1e9);
dp[0] = 0;
int ans = 1e9;
for (int i = 0; i < (1 << n); i++) {
for (int j = 0; j < n; j++) {
if (i & (1 << j)) {
for (int l = 0; l < n; l++) {
if (i & (1 << l))
continue;
dp[i] = min(dp[i], dp[i ^ (1 << j)] + adj[j][l]);
}
}
}
if (__builtin_popcount(i) == n-k)
ans = min(ans, dp[i]);
}
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
5 ms |
496 KB |
Output is correct |
6 |
Correct |
11 ms |
736 KB |
Output is correct |
7 |
Correct |
25 ms |
592 KB |
Output is correct |
8 |
Correct |
55 ms |
848 KB |
Output is correct |
9 |
Correct |
515 ms |
4540 KB |
Output is correct |
10 |
Correct |
522 ms |
4532 KB |
Output is correct |