#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back
#define sz(x) (int)x.size()
#define int long long
typedef pair<int,int>pii;
const int mxN=20,M=1e9+7;
//did u check when n=1?
//is mxN correct?
int n,k,tot;
int tab[mxN][mxN];
int dp[1<<mxN];
int solve(int mask){
if(dp[mask]!=-1)return dp[mask];
if(__builtin_popcount(mask)<=k) return dp[mask]=0;
int ans=M;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(!(mask & (1<<i))||!(mask & (1<<j)))continue;
if(i==j)continue;
ans=min(ans,solve(mask^(1<<i))+tab[i][j]);
}
}
return dp[mask]=ans;
}
int32_t main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
memset(dp,-1,sizeof(dp));
cin>>n>>k;
tot=(1<<n)-1;
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
cin>>tab[i][j];
}
}
cout<<solve(tot);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
9 ms |
8576 KB |
Output is correct |
2 |
Correct |
9 ms |
8576 KB |
Output is correct |
3 |
Correct |
9 ms |
8576 KB |
Output is correct |
4 |
Correct |
9 ms |
8576 KB |
Output is correct |
5 |
Correct |
18 ms |
8576 KB |
Output is correct |
6 |
Correct |
32 ms |
8576 KB |
Output is correct |
7 |
Correct |
53 ms |
8576 KB |
Output is correct |
8 |
Correct |
113 ms |
8576 KB |
Output is correct |
9 |
Correct |
1618 ms |
8696 KB |
Output is correct |
10 |
Correct |
120 ms |
8576 KB |
Output is correct |