mr_dot_convict

1307-D-Cows-and-fields-codeforces-mr.convict

May 5th, 2020
221
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.80 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 sz(x)     (int)x.size()
  26. #define fr(i,x,y) for (int i = (int)x; i <= (int)y; ++i)
  27. #define rv(i,x,y) for (int i = (int)x; i >= (int)y; --i)
  28. #define cnt(x)    __builtin_popcount(x)
  29. #define cntll(x)  __builtin_popcountll(x)
  30. #define bg(x)     " [ " << #x << " : " << (x) << " ] "
  31. #define un(x)     sort(x.begin(), x.end()), \
  32.   x.erase(unique(x.begin(), x.end()), x.end())
  33. using   ll  =     long long;
  34. using   ull =     unsigned long long;
  35. using   ff  =     long double;
  36. using   pii =     pair<int,int>;
  37. using   pil =     pair<int,ll>;
  38. using   pll =     pair<ll,ll>;
  39. using   vi  =     vector <int>;
  40. using   vl  =     vector<ll>;
  41. using   vvi =     vector <vi>;
  42. using   vvl =     vector <vl>;
  43. using   vp  =     vector <pii>;
  44. using   vvp =     vector <vp>;
  45. typedef tree
  46. < int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
  47. ordered_set;
  48.  
  49. struct chash {
  50.   int operator () (pii x) const { return x.X*31 + x.Y; }
  51. };
  52. gp_hash_table <pii, int, chash> gmp;
  53.  
  54. #if __cplusplus > 201103L
  55. seed_seq seq{
  56.   (uint64_t) chrono::duration_cast<chrono::nanoseconds>
  57.     (chrono::high_resolution_clock::now().time_since_epoch()).count(),
  58.     (uint64_t) __builtin_ia32_rdtsc(),
  59.     (uint64_t) (uintptr_t) make_unique<char>().get()
  60. };
  61. mt19937 rng(seq); //uniform_int_distribution<int> (l, h)(rng); //[low, high]
  62. #else
  63. auto seed = chrono::high_resolution_clock::now().time_since_epoch().count();
  64. mt19937 rng(seed);
  65. #endif
  66.  
  67. #define debug(args...) { \
  68.   /* WARNING : do NOT compile this debug func calls with following flags: // \
  69.    * // -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC -D_FORTIFY_SOURCE=2*/ \
  70.   string _s = #args; replace(_s.begin(), _s.end(), ',', ' ');\
  71.   stringstream _ss(_s); \
  72.   istream_iterator<string> _it(_ss); err(_it, args); \
  73. }
  74. void err(istream_iterator<string> it) {
  75.   it->empty(); cerr << " (Line : " << __LINE__ << ")" << '\n';
  76. }
  77. template<typename T, typename... Args>
  78. void err(istream_iterator<string> it, T a, Args... args) {
  79.   cerr << fixed << setprecision(15)
  80.     << " [ " <<  *it << " : " << a  << " ] "<< ' ';
  81.   err(++it, args...);
  82. }
  83. //         __                                           __
  84. //        (**)                                         (**)
  85. //        IIII                                         IIII
  86. //        ####                                         ####
  87. //        HHHH     Madness comes, and madness goes     HHHH
  88. //        HHHH    An insane place, with insane moves   HHHH
  89. //        ####   Battles without, for battles within   ####
  90. //     ___IIII___        Where evil lives,          ___IIII___
  91. //  .-`_._"**"_._`-.      and evil rules         .-`_._"**"_._`-.
  92. // |/``  .`\/`.  ``\|    Breaking them up,      |/``  .`\/`.  ``\|
  93. // `     }    {     '  just breaking them in    `     }    {     '
  94. //       ) () (  Quickest way out, quickest way wins  ) () (
  95. //       ( :: )      Never disclose, never betray     ( :: )
  96. //       | :: |   Cease to speak or cease to breath   | :: |
  97. //       | )( |        And when you kill a man,       | )( |
  98. //       | || |          you're a murderer            | || |
  99. //       | || |             Kill many                 | || |
  100. //       | || |        and you're a conqueror         | || |
  101. //       | || |        Kill them all ... Ooh..        | || |
  102. //       | || |           Oh you're a God!            | || |
  103. //       ( () )                       -Megadeth       ( () )
  104. //        \  /                                         \  /
  105. //         \/                                           \/
  106. /*}}}*/
  107. /*****************************************************************************/
  108. //Don’t practice until you get it right. Practice until you can’t get it wrong
  109.  
  110. const int infi = (int)1e9;
  111. signed main() {
  112.   IOS; PREC;
  113.  
  114.   int n, m, k;
  115.   cin >> n >> m >> k;
  116.   vector <int> special(k);
  117.   for (int i = 0; i < k; ++i) {
  118.     cin >> special[i];
  119.     --special[i];
  120.   }
  121.  
  122.   vector <vector <int>> g(n, vector <int> ());
  123.   for (int i = 0; i < m; ++i) {
  124.     int u, v;
  125.     cin >> u >> v;
  126.     --u, --v;
  127.     g[u].push_back(v), g[v].push_back(u);
  128.   }
  129.  
  130.   auto bfs = [&] (int src) -> vector <int> {
  131.     vector <int> vis(n, false), dist(n, infi);
  132.     queue <int> q;
  133.     dist[src] = 0;
  134.     vis[src] = true;
  135.     q.push(src);
  136.     while (!q.empty()) {
  137.       int u = q.front(); q.pop();
  138.       for (int v : g[u]) {
  139.         if (!vis[v]) {
  140.           vis[v] = true;
  141.           dist[v] = dist[u] + 1;
  142.           q.push(v);
  143.         }
  144.       }
  145.     }
  146.     return dist;
  147.   };
  148.  
  149.   vector <int> dsrc = bfs(0), ddest = bfs(n-1);
  150.   vector <pair <int, int>> store;
  151.   for (int i = 0; i < k; ++i) {
  152.     store.push_back(make_pair(dsrc[special[i]], ddest[special[i]]));
  153.   }
  154.   sort (store.begin(), store.end());
  155.   int mn = 0;
  156.  
  157.   for (int i = 0; i + 1 < k; ++i) {
  158.     mn = max(mn, store[i].first + store[i + 1].second + 1);
  159.   }
  160.  
  161.   cout << min(mn, ddest[0]) << '\n';
  162.  
  163.  
  164.   return EXIT_SUCCESS;
  165. }
  166.  
Add Comment
Please, Sign In to add comment