#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define fi first
#define se second
#define MOD 1000000007
typedef long long ll;
const int N=22;
int a[N][N],dp[(1<<N)];
int n,k;
int _dp(int mask){
if((int)bitset<32>(mask).count()==k)return 0;
int &ans=dp[mask];
if(ans!=-1)return ans;
ans=1e9;
for(int i=0;i<n;++i){
if(!(mask&(1<<i)))continue;
for(int j=0;j<n;++j){
if(!(mask&(1<<j))||i==j)continue;
ans=min(ans,_dp(mask^(1<<i))+a[i][j]);
}
}
return ans;
}
signed main(){
cin>>n>>k;
for(int i=0;i<n;++i){
for(int j=0;j<n;++j){
cin>>a[i][j];
}
}
memset(dp,-1,sizeof dp);
cout<<_dp((1<<n)-1);
#ifdef LOCAL
cerr << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC << "s.";
#endif
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
16716 KB |
Output is correct |
2 |
Correct |
7 ms |
16716 KB |
Output is correct |
3 |
Correct |
7 ms |
16672 KB |
Output is correct |
4 |
Correct |
8 ms |
16716 KB |
Output is correct |
5 |
Correct |
17 ms |
16588 KB |
Output is correct |
6 |
Correct |
32 ms |
16712 KB |
Output is correct |
7 |
Correct |
58 ms |
16588 KB |
Output is correct |
8 |
Correct |
122 ms |
16692 KB |
Output is correct |
9 |
Correct |
1475 ms |
16692 KB |
Output is correct |
10 |
Correct |
139 ms |
16680 KB |
Output is correct |