fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int n,k;
  5. int a[20][20];
  6. int dp[1500000];
  7.  
  8. int solve(int mask)
  9. {
  10. int temp,cnt=0,temp2;
  11. if(__builtin_popcount(mask)<=k) return 0;
  12. if(dp[mask]!=-1)
  13. {
  14. return dp[mask];
  15. }
  16. int mn=10e8;
  17. for(temp=0;temp<n;temp++)
  18. {
  19. for(temp2=temp+1;temp2<n;temp2++)
  20. {
  21. if((mask & (1<<temp))&& (mask & (1<<temp2)))
  22. {
  23. mn=min(mn,a[temp][temp2] + solve(mask ^ (1 << temp)));
  24. mn=min(mn,a[temp2][temp] + solve(mask ^ (1<< temp2)));
  25. }
  26. }
  27. }
  28. dp[mask]=mn;
  29. return mn;
  30. }
  31.  
  32. int main()
  33. {
  34. memset(dp,-1,sizeof(dp));
  35. scanf("%d %d",&n,&k);
  36. int temp,temp2;
  37. for(temp=0;temp<n;temp++)
  38. {
  39. for(temp2=0;temp2<n;temp2++)
  40. {
  41. scanf("%d",&a[temp][temp2]);
  42. }
  43. }
  44. printf("%d\n",solve((1<<n)-1));
  45. }
Success #stdin #stdout 0s 9336KB
stdin
5 2
0 5 4 3 2
7 0 4 4 4
3 3 0 1 2
4 3 1 0 5
4 5 5 5 0
stdout
5