#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9+7;
const int N = 21;
const int INF = 1e9;
int n,k;
int dp[1<<N],a[N][N];
int solve(int mask){
if(__builtin_popcount(mask)==k)return 0;
if(dp[mask]!=-1)return dp[mask];
int sol=INF;
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;
sol=min(sol,solve(mask^(1<<i))+a[i][j]);
}
}
dp[mask]=sol;
return sol;
}
int main()
{
ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
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<<solve((1<<n)-1);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
8568 KB |
Output is correct |
2 |
Correct |
7 ms |
8568 KB |
Output is correct |
3 |
Correct |
7 ms |
8756 KB |
Output is correct |
4 |
Correct |
7 ms |
8756 KB |
Output is correct |
5 |
Correct |
18 ms |
8756 KB |
Output is correct |
6 |
Correct |
33 ms |
8756 KB |
Output is correct |
7 |
Correct |
56 ms |
8804 KB |
Output is correct |
8 |
Correct |
119 ms |
8840 KB |
Output is correct |
9 |
Correct |
1467 ms |
8972 KB |
Output is correct |
10 |
Correct |
143 ms |
8972 KB |
Output is correct |