mr_dot_convict

Synchronous-Shopping-Hackerrank-mr.convict

Nov 15th, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.15 KB | None | 0 0
  1. // Hack it and have it ;) //
  2. /*author* Priyanshu Shrivastav (from IIT Palakkad) *
  3.  * *_ __ ___  _ ______ ___  _ ____   ___  ___| |_  *
  4.  * | '_ ` _ \| '__/ __/ _ \| '_ \ \ / / |/ __| __| *
  5.  * | | | | | | | | (_| (_) | | | \ V /| | (__| |_  *
  6.  * |_| |_| |_|_|(_)___\___/|_| |_|\_/ |_|\___|\__| *
  7. When I wrote this, only God and I understood what I was doing
  8.  ** * * * * * * * Now, only God knows!* * * * * * */
  9. #include      <bits/stdc++.h> /*{{{*/
  10. #include      <ext/pb_ds/assoc_container.hpp>
  11. #include      <ext/pb_ds/tree_policy.hpp>
  12. using namespace std;
  13. using namespace __gnu_pbds;
  14.  
  15. #ifndef CONVICTION
  16. #pragma GCC       optimize ("Ofast")
  17. #pragma GCC       optimize ("unroll-loops")
  18. #pragma GCC       target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  19. #endif
  20.  
  21. #define IOS       ios_base::sync_with_stdio(false); cin.tie (nullptr)
  22. #define PREC      cout.precision (10); cout << fixed
  23. #define x         first
  24. #define y         second
  25. #define fr(i,x,y) for (int i = (int)x; i <= (int)y; ++i)
  26. #define rv(i,x,y) for (int i = (int)x; i >= (int)y; --i)
  27. #define cnt(x)    __builtin_popcount(x)
  28. #define cntll(x)  __builtin_popcountll(x)
  29. #define bg(x)     " [ " << #x << " : " << (x) << " ] "
  30. #define un(x)     sort(x.begin(), x.end()), \
  31.                   x.erase(unique(x.begin(), x.end()), x.end())
  32. using   ll  =     long long;
  33. using   ull =     unsigned long long;
  34. using   ff  =     long double;
  35. using   pii =     pair<int,int>;
  36. using   pil =     pair<int,ll>;
  37. using   vi  =     vector <int>;
  38. using   vl  =     vector<ll>;
  39. using   vvi =     vector <vi>;
  40. using   vp  =     vector <pii>;
  41. using   vvp =     vector <vp>;
  42. typedef tree
  43. < int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
  44. ordered_set;
  45.  
  46. struct chash {
  47.    int operator () (pii x) const { return x.x*31 + x.y; }
  48. };
  49. gp_hash_table <pii, int, chash> mp;
  50.  
  51. #if __cplusplus > 201103L
  52. seed_seq seq{
  53.    (uint64_t) chrono::duration_cast<chrono::nanoseconds>
  54.       (chrono::high_resolution_clock::now().time_since_epoch()).count(),
  55.       (uint64_t) __builtin_ia32_rdtsc(),
  56.       (uint64_t) (uintptr_t) make_unique<char>().get()
  57. };
  58. mt19937 rng(seq); //uniform_int_distribution<int> (l, h)(rng); //[low, high]
  59. #else
  60. auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
  61. mt19937 rng(seed);
  62. #endif
  63.  
  64. #define debug(args...) { \
  65.    /* WARNING : do NOT compile this debug func calls with following flags: // \
  66.     * // -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC -D_FORTIFY_SOURCE=2*/ \
  67.    string _s = #args; replace(_s.begin(), _s.end(), ',', ' ');\
  68.    stringstream _ss(_s); \
  69.    istream_iterator<string> _it(_ss); err(_it, args); \
  70. }
  71. void err(istream_iterator<string> it) {
  72.    it->empty(); cerr << " (Line : " << __LINE__ << ")" << '\n';
  73. }
  74. template<typename T, typename... Args>
  75. void err(istream_iterator<string> it, T a, Args... args) {
  76.    cerr << fixed << setprecision(15)
  77.       << " [ " <<  *it << " : " << a  << " ] "<< ' ';
  78.    err(++it, args...);
  79. }
  80. //         __                                           __
  81. //        (**)                                         (**)
  82. //        IIII                                         IIII
  83. //        ####                                         ####
  84. //        HHHH     Madness comes, and madness goes     HHHH
  85. //        HHHH    An insane place, with insane moves   HHHH
  86. //        ####   Battles without, for battles within   ####
  87. //     ___IIII___        Where evil lives,          ___IIII___
  88. //  .-`_._"**"_._`-.      and evil rules         .-`_._"**"_._`-.
  89. // |/``  .`\/`.  ``\|    Breaking them up,      |/``  .`\/`.  ``\|
  90. // `     }    {     '  just breaking them in    `     }    {     '
  91. //       ) () (  Quickest way out, quickest way wins  ) () (
  92. //       ( :: )      Never disclose, never betray     ( :: )
  93. //       | :: |   Cease to speak or cease to breath   | :: |
  94. //       | )( |        And when you kill a man,       | )( |
  95. //       | || |          you're a murderer            | || |
  96. //       | || |             Kill many                 | || |
  97. //       | || |        and you're a conqueror         | || |
  98. //       | || |        Kill them all ... Ooh..        | || |
  99. //       | || |           Oh you're a God!            | || |
  100. //       ( () )                       -Megadeth       ( () )
  101. //        \  /                                         \  /
  102. //         \/                                           \/
  103. /*}}}*/
  104. /*****************************************************************************/
  105. //Don’t practice until you get it right. Practice until you can’t get it wrong
  106.  
  107. const int N = (int)1e3 + 10;
  108. const ll inf = (ll)1e18;
  109. ll dp[N][1 << 10];
  110. bool vis[N][1 << 10];
  111. vi A; vvp G;
  112. int n, m, k;
  113.  
  114. struct Node {
  115.   int u, mask; ll d;
  116.   bool operator < (const Node&o) const {return d > o.d;}
  117. };
  118.  
  119. void dijkstra () {
  120.   priority_queue <Node> q;
  121.  
  122.   int sub_mask = A[0];
  123.   dp[0][sub_mask] = 0;
  124.   q.push(Node{0, sub_mask, 0});
  125.  
  126.   while (!q.empty()) {
  127.     Node u_node = q.top();
  128.     q.pop();
  129.     int u = u_node.u, mask = u_node.mask;
  130.     // if (u == n-1) debug(u, show_mask(mask), dp[u][mask]);
  131.     if (vis[u][mask]) continue;
  132.     vis[u][mask] = true;
  133.  
  134.     for (auto v_p : G[u]) {
  135.       int v = v_p.x, w = v_p.y;
  136.       int new_mask = mask | A[v];
  137.  
  138.       if (dp[v][new_mask] > dp[u][mask] + w) {
  139.         dp[v][new_mask] = dp[u][mask] + w;
  140.         q.push({v, new_mask, dp[v][new_mask]});
  141.       }
  142.     }
  143.   }
  144. }
  145.  
  146. signed main() {
  147.    IOS; PREC;
  148.    cin >> n >> m >> k;
  149.  
  150.    A.assign(n, 0);
  151.    G.assign(n, vp());
  152.    fr (i, 0, n-1) fr (mask, 0, (1 << k)-1)
  153.      dp[i][mask] = inf, vis[i][mask] = false;
  154.  
  155.    fr (i, 0, n-1) {
  156.      int t;
  157.      cin >> t;
  158.      int foo;
  159.      fr (j, 0, t-1) cin >> foo, A[i] |= (1 << (foo-1));
  160.    }
  161.  
  162.    fr(e, 0, m-1) {
  163.      int u, v, w;
  164.      cin >> u >> v >> w;
  165.      --u, --v;
  166.      G[u].emplace_back(v, w), G[v].emplace_back(u, w);
  167.    }
  168.  
  169.    dijkstra();
  170.  
  171.    ll mn = inf;
  172.  
  173.    for (int f_mask = 0; f_mask < (1 << k); ++f_mask)
  174.      for (int s_mask = 0; s_mask < (1 << k); ++s_mask)
  175.        if ((f_mask | s_mask) == (1 << k) - 1) {
  176.          mn = min(mn, max(dp[n-1][f_mask], dp[n-1][s_mask]));
  177.        }
  178.  
  179.    cout << mn << '\n';
  180.    return EXIT_SUCCESS;
  181. }
Add Comment
Please, Sign In to add comment