fork download
  1. #include <stdio.h>
  2.  
  3. int c[25][25];
  4. int dp[(1<<20)+5];
  5.  
  6. int main(){
  7. int N, K;
  8. scanf("%d%d", &N, &K);
  9. for(int i=0; i<N; i++) for(int j=0; j<N; j++) scanf("%d", &c[i][j]);
  10. for(int i=1; i<(1<<N); i++) dp[i] = 2e9;
  11. int res = 2e9;
  12. for(int mask=0; mask<(1<<N); mask++){
  13. int bcnt = __builtin_popcount(mask);
  14. if(N-bcnt <= K && dp[mask] < res) res = dp[mask];
  15. for(int i=0; i<N; i++){
  16. if(mask&(1<<i)) continue;
  17. int newbit = mask|(1<<i);
  18. int cost = 2e5;
  19. for(int j=0; j<N; j++){
  20. if((mask&(1<<j)) || j == i) continue;
  21. if(c[i][j] < cost) cost = c[i][j];
  22. }
  23. if(dp[mask]+cost < dp[newbit]) dp[newbit] = dp[mask]+cost;
  24. }
  25. }
  26. printf("%d", res);
  27. return 0;
  28. }
Time limit exceeded #stdin #stdout 5s 7556KB
stdin
Standard input is empty
stdout
Standard output is empty