#include<bits/stdc++.h>
#define rep(i, a, b) for(int i=(a) ; i<=(b) ; ++i)
#define Task ""
using namespace std;
const int N=21;
int dp[1ll<<N], c[N][N], n, k, luu[N], dem;
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen(Task".inp", "r", stdin);
//freopen(Task".out", "w", stdout);
cin >> n >> k;
rep(i, 1, n)
rep(j, 1, n) cin >> c[i][j];
rep(i, 0, (1ll<<n)-1) {
if(__builtin_popcount(i)<=k) dp[i]=0;
else dp[i]=1000000000;
}
rep(i, 0, (1ll<<n)-1) {
int num=__builtin_popcount(i);
if(num<=k) continue;
dem=0;
rep(j, 1, n) {
if((i>>(j-1))&1) luu[++dem]=j;
}
rep(j, 1, dem) {
int state=i^(1ll<<(luu[j]-1));
int xx=1000000000;
rep(k1, 1, dem) {
if(j==k1) continue;
xx=min(xx, c[luu[j]][luu[k1]]);
}
dp[i]=min(dp[i], dp[state]+xx);
}
}
cout << dp[(1ll<<n)-1];
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
380 KB |
Output is correct |
2 |
Correct |
2 ms |
508 KB |
Output is correct |
3 |
Correct |
2 ms |
512 KB |
Output is correct |
4 |
Correct |
2 ms |
512 KB |
Output is correct |
5 |
Correct |
4 ms |
512 KB |
Output is correct |
6 |
Correct |
7 ms |
780 KB |
Output is correct |
7 |
Correct |
13 ms |
796 KB |
Output is correct |
8 |
Correct |
26 ms |
1052 KB |
Output is correct |
9 |
Correct |
251 ms |
4648 KB |
Output is correct |
10 |
Correct |
34 ms |
4748 KB |
Output is correct |