Advertisement
Guest User

Untitled

a guest
Dec 17th, 2014
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.68 KB | None | 0 0
  1. // LDVSOFT team
  2.  
  3. #include <iostream>
  4. #include <cstdio>
  5. #include <cstdlib>
  6. #include <cstring>
  7. #include <cmath>
  8. #include <cassert>
  9. #include <string>
  10. #include <vector>
  11. #include <set>
  12. #include <map>
  13. #include <queue>
  14. #include <stack>
  15. #include <deque>
  16. #include <algorithm>
  17.  
  18. #define NAME "salesman"
  19.  
  20. using namespace std;
  21.  
  22. //#undef DEBUG
  23.  
  24. #ifdef HOME
  25.     #define DEBUG_INTER
  26. #endif
  27.  
  28. #ifdef DEBUG_INTER
  29.     #define iprintf(...) printf("OUT:" __VA_ARGS__ ), fflush(stdout)
  30. #else
  31.     #define iprintf(...) printf( __VA_ARGS__ ), fflush(stdout)
  32. #endif
  33.  
  34. #ifdef DEBUG
  35.     #define eprintf(...) printf( __VA_ARGS__ ), fflush(stdout)
  36. #else
  37.     #define eprintf(...)
  38. #endif
  39.  
  40. #ifdef WIN32
  41.     #define LLC "%I64d"
  42.     #define ULLC "%I64u"
  43. #else
  44.     #ifdef WIN64
  45.         #define LLC "%I64d"
  46.         #define ULLC "%I64u"
  47.     #else
  48.         #define LLC "%lld"
  49.         #define ULLC "%llu"
  50.     #endif
  51. #endif
  52.  
  53. #define pb push_back
  54. #define mp make_pair
  55. #define scn second
  56. #define frs first
  57. #define ins insert
  58.  
  59. template <typename A>
  60. using setit = typename set<A>::iterator;
  61. template <typename A, typename B>
  62. using mapit = typename map<A, B>::iterator;
  63.  
  64. using uchar = unsigned char;
  65. using uint  = unsigned int;
  66. using ll    =          long long;
  67. using ull   = unsigned long long;
  68. using ld    =          long double;
  69.  
  70. using pii = pair<int, int>;
  71.  
  72. int const N(19);
  73. int const MASK(1 << N);
  74. int const INF(2e9);
  75.  
  76. int n, Mask;
  77. int a[N][N];
  78. int d[N][MASK], back[N][MASK];
  79.  
  80. int main(void)
  81. {                          
  82.     assert(freopen(NAME".in", "r", stdin));
  83.     assert(freopen(NAME".out", "w", stdout));
  84.    
  85.     scanf("%d", &n);
  86.     Mask = 1 << n;
  87.     for (int i(0); i < n; ++i)
  88.         for (int j(0); j < n; ++j)
  89.             scanf("%d", &a[i][j]);
  90.  
  91.     for (int mask(0); mask < Mask; ++mask)
  92.         for (int i(0); i < n; ++i)
  93.         {
  94.             if ((mask & (1 << i)) == 0)
  95.                 continue;
  96.             int altmask(mask ^ (1 << i));
  97.             if (altmask == 0)
  98.             {
  99.                 d[i][mask] = 0;
  100.                 back[i][mask] = -1;
  101.             }
  102.             else
  103.             {
  104.                 d[i][mask] = +INF;
  105.                 for (int j(0); j < n; ++j)
  106.                 {
  107.                     if ((altmask & (1 << j)) == 0)
  108.                         continue;
  109.                     if (d[i][mask] <= d[j][altmask] + a[j][i])
  110.                         continue;
  111.                     d[i][mask] = d[j][altmask] + a[j][i];
  112.                     back[i][mask] = j;
  113.                 }
  114.             }
  115.             eprintf("! d[ %d ][ %d ] = %d (%d)\n", i, mask, d[i][mask], back[i][mask]);
  116.     }
  117.  
  118.     int ans(-1);
  119.     for (int i(0); i < n; ++i)
  120.         if (ans == -1 || d[ans][Mask - 1] > d[i][Mask - 1])
  121.             ans = i;
  122.  
  123.     printf("%d\n", d[ans][Mask - 1]);
  124.     vector<int> an;
  125.     int mask(Mask - 1);
  126.     while (mask)
  127.     {
  128.         an.pb(ans);
  129.         int newMask = mask ^ (1 << ans);
  130.         int newAns = back[ans][mask];
  131.         mask = newMask;
  132.         ans = newAns;
  133.     }
  134.     reverse(an.begin(), an.end());
  135.     for (int i: an)
  136.         printf("%d ", i + 1);
  137.  
  138.     return 0;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement