#include <bits/stdc++.h>
using namespace std;
const int maxmask = (1<<20)+10, inf = 0x3f3f3f3f;
int n, k, m[30][30];
int dp[maxmask];
int solve(int mask, int qtd){
if(dp[mask] != inf) return dp[mask];
if(qtd >= k) return 0;
for(int i = 0 ; i < n ; ++i){
if(mask & (1<<i)) continue;
for(int j = 0 ; j < n ; ++j){
if(i == j) continue;
if(mask & (1<<j)) continue;
dp[mask] = min(dp[mask], m[i][j] + solve(mask | (1<<i), qtd+1));
}
}
return dp[mask];
}
int main(){
cin >> n >> k;
for(int i = 0 ; i < n ; ++i)
for(int j = 0 ; j < n ; ++j)
cin >> m[i][j];
memset(dp, inf, sizeof dp);
k = n - k;
cout << solve(0, 0) << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
4352 KB |
Output is correct |
2 |
Correct |
6 ms |
4352 KB |
Output is correct |
3 |
Correct |
6 ms |
4352 KB |
Output is correct |
4 |
Correct |
7 ms |
4352 KB |
Output is correct |
5 |
Correct |
16 ms |
4352 KB |
Output is correct |
6 |
Correct |
24 ms |
4480 KB |
Output is correct |
7 |
Correct |
49 ms |
4352 KB |
Output is correct |
8 |
Correct |
116 ms |
4352 KB |
Output is correct |
9 |
Correct |
1317 ms |
4472 KB |
Output is correct |
10 |
Correct |
105 ms |
4472 KB |
Output is correct |