abdukodir

Untitled

Oct 24th, 2015
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.75 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <iostream>
  3. #include <math.h>
  4. #include <vector>
  5. #include <string>
  6. #include <map>
  7. #include <bitset>
  8. #include <queue>
  9. #include <string.h>
  10. #include <algorithm>
  11. #define sc scanf
  12. #define pr printf
  13. #define pb push_backŝ
  14. #define mp make_pair
  15. #define fr first
  16. #define se second
  17. using namespace std;
  18.  
  19. typedef pair<int, int> pii;
  20. typedef pair<double, double> pdd;
  21.  
  22. const int P = 53;
  23. const int MN = 1000010;
  24. const long long INF = (1LL<<31) - 1LL;
  25. const double eps = (1e-7);
  26.  
  27. int a[25][25];
  28. int d[MN][25], p[MN][25];
  29. int n, cur;
  30.  
  31. int rec(int mask, int last) {
  32. if ((mask - (1 << last)) == 0) {
  33. p[mask][last] = -1;
  34. d[mask][last] = 0;
  35. return 0;
  36. }
  37. if (p[mask][last] != 0) {
  38. return d[mask][last];
  39. }
  40. d[mask][last] = INF;
  41. for (int i = 0; i < n; i++) {
  42. if (i != last && ((mask >> i) & 1 != 0)) {
  43. if (d[mask][last] > rec(mask - (1 << last), i) + a[i][last]) {
  44. d[mask][last] = rec(mask - (1 << last), i) + a[i][last];
  45. p[mask][last] = i + 1;
  46. }
  47. }
  48. }
  49. return d[mask][last];
  50. }
  51. void printAns(int t, int k) {
  52. if (k == -1) {
  53. return;
  54. }
  55. k--;
  56. printAns(t - (1 << k), p[t][k]);
  57. pr("%d ", k + 1);
  58. }
  59. main()
  60. {
  61. //freopen("in.txt","r",stdin); freopen("out.txt","w",stdout);
  62. sc("%d", &n);
  63. for (int i = 0; i < n; i++) {
  64. for (int j = 0; j < n; j++) {
  65. sc("%d", &a[i][j]);
  66. }
  67. }
  68. int k = 0;
  69. int t = (1 << n) - 1;
  70. for (int i = 0; i < n; i++) {
  71. cur = i;
  72. rec(t, i);
  73. if (d[t][i] < d[t][k]) {
  74. k = i;
  75. }
  76. }
  77. pr("%d\n", d[t][k]);
  78. printAns(t, k + 1);
  79. return 0;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment