fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. const int N = 30;
  4. const long long INF = 1e18;
  5. int n;
  6. long long c[N][N];
  7. bool check[N];
  8. long long choose[N];
  9. long long ans[N];
  10. long long res = INF;
  11. long long minrow[N];
  12. void prepare(){
  13. for (int i = 0; i < n; i++) {
  14. long long minval = INF;
  15. for (int j = 0; j < n; j++) {
  16. if (c[i][j] < minval) {
  17. minval = c[i][j];
  18. }
  19. }
  20. minrow[i] = minval;
  21. }
  22. for (int i = n - 2; i >= 0; i--) {
  23. minrow[i] += minrow[i+1];
  24. }
  25. }
  26. void dequy ( int id , long long curc ){
  27. if ( id < n && curc + minrow[id] >= res ) return;
  28. if ( id == n ){
  29. if ( curc < res ){
  30. res = curc;
  31. for ( int i = 0 ; i < n ; i++ )
  32. ans[i] = choose[i];
  33. }
  34. return ;
  35. }
  36. for (int i = 0; i < n; i++ ){
  37. if ( !check[i]) {
  38. check[i] = true;
  39. choose[id] = i+1;
  40. dequy(id+1, curc + c[id][i]);
  41. check[i] = false;
  42. }
  43. }
  44. }
  45. int main(){
  46. cin >> n;
  47. for (int i = 0; i < n; i++) {
  48. for (int j = 0; j < n; j++) {
  49. cin >> c[i][j];
  50. }
  51. }
  52. prepare();
  53. dequy(0, 0);
  54. cout << res << endl;
  55. for (int i = 0; i < n; i++) {
  56. cout << ans[i] << " ";
  57. }
  58. }
Success #stdin #stdout 0s 5328KB
stdin
Standard input is empty
stdout
0