mr_dot_convict

spoj-connect-line-segments-mr.convict

May 11th, 2020
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.55 KB | None | 0 0
  1. #include      <bits/stdc++.h> /*{{{*/
  2. #include      <ext/pb_ds/assoc_container.hpp>
  3. #include      <ext/pb_ds/tree_policy.hpp>
  4. using namespace std;
  5. using namespace __gnu_pbds;
  6.  
  7. #ifdef CONVICTION
  8. #pragma GCC       optimize ("Ofast")
  9. #pragma GCC       optimize ("unroll-loops")
  10. #pragma GCC       target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  11. #endif
  12.  
  13. #define IOS       ios_base::sync_with_stdio(false); cin.tie (nullptr)
  14. #define PREC      cout.precision (5); cout << fixed
  15. #define X         first
  16. #define Y         second
  17. #define sz(x)     (int)x.size()
  18. #define fr(i,x,y) for (int i = (int)x; i <= (int)y; ++i)
  19. #define rv(i,x,y) for (int i = (int)x; i >= (int)y; --i)
  20. #define bcnt(x)   __builtin_popcount(x)
  21. #define bcntll(x) __builtin_popcountll(x)
  22. #define bg(x)     " [ " << #x << " : " << (x) << " ] "
  23. #define un(x)     sort(x.begin(), x.end()), \
  24.   x.erase(unique(x.begin(), x.end()), x.end())
  25. using   ll  =     long long;
  26. using   ull =     unsigned long long;
  27. using   ff  =     long double;
  28. using   pii =     pair<int,int>;
  29. using   pil =     pair<int,ll>;
  30. using   pll =     pair<ll,ll>;
  31. using   vi  =     vector <int>;
  32. using   vl  =     vector<ll>;
  33. using   vvi =     vector <vi>;
  34. using   vvl =     vector <vl>;
  35. using   vp  =     vector <pii>;
  36. using   vvp =     vector <vp>;
  37. typedef tree
  38. < int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
  39. ordered_set;
  40.  
  41. struct chash {
  42.   int operator () (pii x) const { return x.X*31 + x.Y; }
  43. };
  44. gp_hash_table <pii, int, chash> gmp;
  45.  
  46. #if __cplusplus > 201103L
  47. seed_seq seq{
  48.   (uint64_t) chrono::duration_cast<chrono::nanoseconds>
  49.     (chrono::high_resolution_clock::now().time_since_epoch()).count(),
  50.     (uint64_t) __builtin_ia32_rdtsc(),
  51.     (uint64_t) (uintptr_t) make_unique<char>().get()
  52. };
  53. mt19937 rng(seq); //uniform_int_distribution<int> (l, h)(rng); //[low, high]
  54. #else
  55. auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
  56. mt19937 rng(seed);
  57. #endif
  58.  
  59. #ifdef CONVICTION
  60. void __print(int x) {cerr << x;}
  61. void __print(long x) {cerr << x;}
  62. void __print(long long x) {cerr << x;}
  63. void __print(unsigned x) {cerr << x;}
  64. void __print(unsigned long x) {cerr << x;}
  65. void __print(unsigned long long x) {cerr << x;}
  66. void __print(float x) {cerr << x;}
  67. void __print(double x) {cerr << x;}
  68. void __print(long double x) {cerr << x;}
  69. void __print(char x) {cerr << '\'' << x << '\'';}
  70. void __print(const char *x) {cerr << '\"' << x << '\"';}
  71. void __print(const string &x) {cerr << '\"' << x << '\"';}
  72. void __print(bool x) {cerr << (x ? "true" : "false");}
  73.  
  74. template<typename T, typename V>
  75. void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
  76. template<typename T>
  77. void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
  78. void _print() {cerr << "]" << endl;}
  79. template <typename T, typename... V>
  80. void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
  81. #define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
  82. #else
  83. #define debug(x...)
  84. #endif
  85. //         __                                           __
  86. //        (**)                                         (**)
  87. //        IIII                                         IIII
  88. //        ####                                         ####
  89. //        HHHH     Madness comes, and madness goes     HHHH
  90. //        HHHH    An insane place, with insane moves   HHHH
  91. //        ####   Battles without, for battles within   ####
  92. //     ___IIII___        Where evil lives,          ___IIII___
  93. //  .-`_._"**"_._`-.      and evil rules         .-`_._"**"_._`-.
  94. // |/``  .`\/`.  ``\|    Breaking them up,      |/``  .`\/`.  ``\|
  95. // `     }    {     '  just breaking them in    `     }    {     '
  96. //       ) () (  Quickest way out, quickest way wins  ) () (
  97. //       ( :: )      Never disclose, never betray     ( :: )
  98. //       | :: |   Cease to speak or cease to breath   | :: |
  99. //       | )( |        And when you kill a man,       | )( |
  100. //       | || |          you're a murderer            | || |
  101. //       | || |             Kill many                 | || |
  102. //       | || |        and you're a conqueror         | || |
  103. //       | || |        Kill them all ... Ooh..        | || |
  104. //       | || |           Oh you're a God!            | || |
  105. //       ( () )                       -Megadeth       ( () )
  106. //        \  /                                         \  /
  107. //         \/                                           \/
  108. /*}}}*/
  109.  
  110. /*****************************************************************************/
  111. //Don’t practice until you get it right. Practice until you can’t get it wrong
  112.  
  113. void preproc() {
  114. }
  115.  
  116. struct Seg {
  117.   double x1, y1, x2, y2, d;
  118. };
  119.  
  120. int n;
  121. const int N = 15;
  122. Seg sg[N];
  123. double one[N][1 << N];
  124. double two[N][1 << N];
  125.  
  126. void solve() {
  127.   for (int i = 0; i < n; ++i) {
  128.     cin >> sg[i].x1 >> sg[i].y1 >> sg[i].x2 >> sg[i].y2;
  129.     sg[i].d = hypot(sg[i].x1 - sg[i].x2, sg[i].y1 - sg[i].y2);
  130.   }
  131.  
  132.   for (int i = 0; i < n; ++i) {
  133.     for (int mask = 0; mask < (1 << n); ++mask)
  134.       one[i][mask] = two[i][mask] = 1e18;
  135.     one[i][1 << i] = two[i][1 << i] = sg[i].d;
  136.   }
  137.  
  138.   for (int mask = 1; mask < (1 << n); ++mask) {
  139.     for (int i = 0; i < n; ++i) {
  140.       if (mask & (1 << i)) {
  141.         double &olddp_one = one[i][mask];
  142.         double &olddp_two = two[i][mask];
  143.         for (int j = 0; j < n; ++j) {
  144.           // i and j to join
  145.           if (!(mask & (1 << j))) { // i is present and j isn't, so update j in the mask
  146.             double &nwdp_one = one[j][mask ^ (1 << j)];
  147.             double &nwdp_two = two[j][mask ^ (1 << j)];
  148.  
  149.             nwdp_one = min({nwdp_one,
  150.                 olddp_one + hypot(sg[i].x2 - sg[j].x1, sg[i].y2 - sg[j].y1) + sg[j].d,
  151.                 olddp_two + hypot(sg[i].x1 - sg[j].x1, sg[i].y1 - sg[j].y1) + sg[j].d
  152.                 });
  153.             nwdp_two = min({nwdp_two,
  154.                 olddp_one + hypot(sg[i].x2 - sg[j].x2, sg[i].y2 - sg[j].y2) + sg[j].d,
  155.                 olddp_two + hypot(sg[i].x1 - sg[j].x2, sg[i].y1 - sg[j].y2) + sg[j].d
  156.                 });
  157.  
  158.           }
  159.         }
  160.       }
  161.     }
  162.   }
  163.  
  164.   double res = 1e18;
  165.   for (int i = 0; i < n; ++i)
  166.     res = min({res, one[i][(1 << n) - 1], two[i][(1 << n) - 1]});
  167.   cout << res << '\n';
  168. }
  169.  
  170. signed main() {
  171.   IOS; PREC;
  172.   preproc();
  173.  
  174.   int tc = 1;
  175.   // cin >> tc;
  176.   for (int Tt = 1; Tt <= tc; ++Tt) {
  177.     int ti = 1;
  178.     while (cin >> n, n) {
  179.       cout << "Case " << ti << ": ";
  180.       solve();
  181.       ++ti;
  182.     }
  183.   }
  184.   return EXIT_SUCCESS;
  185. }
Add Comment
Please, Sign In to add comment