#include<bits/stdc++.h>
using namespace std;
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
int n, m;
cin >> n >> m;
int a[n][n];
for(int i = 0; i < n; i++){
for(int j = 0; j < n; j++){
cin >> a[i][j];
}
}
int dp[(1 << n)];
memset(dp, 63, sizeof(dp));
dp[(1 << n) - 1] = 0;
for(int i = (1 << n) - 1; i >= 0; i--){
for(int j = 0; j < n; j++){
if((i >> j) & 1){
for(int k = 0; k < n; k++){
if(j == k){
continue;
}
if((i >> k) & 1){
dp[i ^ (1 << j)] = min(dp[i ^ (1 << j)], dp[i] + a[j][k]);
}
}
}
}
}
int ans = 2e9;
for(int i = 0; i < (1 << n); i++){
if(__builtin_popcount(i) <= m){
ans = min(ans, dp[i]);
}
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
3 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
10 ms |
384 KB |
Output is correct |
6 |
Correct |
17 ms |
384 KB |
Output is correct |
7 |
Correct |
35 ms |
632 KB |
Output is correct |
8 |
Correct |
78 ms |
768 KB |
Output is correct |
9 |
Correct |
749 ms |
4600 KB |
Output is correct |
10 |
Correct |
740 ms |
4480 KB |
Output is correct |