Advertisement
Guest User

Shortest Path - Taukir Khatri

a guest
Nov 15th, 2021
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.23 KB | None | 0 0
  1. // ====================================
  2. // CODE BY Taukir Khatri
  3. // ====================================
  4.  
  5. #include "bits/stdc++.h"
  6. // #include "ext/pb_ds/assoc_container.hpp"
  7. // #include "ext/pb_ds/tree_policy.hpp"
  8. using namespace std;
  9. // using namespace __gnu_pbds;
  10.  
  11. #define fast_io ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0)
  12.  
  13. typedef long long  ll;
  14. #define vi         vector<ll>
  15. #define vb         vector<bool>
  16. #define vvi        vector<vector<ll>>
  17. #define pi         pair<ll,ll>
  18. #define vpi        vector<pi>
  19. #define mii        map<ll,ll>
  20. #define qi         queue<ll>
  21. #define si         stack<ll>
  22. #define pqd        priority_queue<ll>
  23. #define pqa        priority_queue<ll, vi, greater<ll>()>
  24. #define ff         first
  25. #define ss         second
  26. #define pb         push_back
  27. #define ins        insert
  28. #define pob        pop_back
  29. #define puf        push_front
  30. #define pof        pop_front
  31. #define sz         size
  32. #define mp         make_pair
  33. #define all(x)     x.begin(), x.end()
  34. #define bs(a,x)    binary_search(all(a), x)
  35. #define lb(a,x)    lower_bound(all(a), x)
  36. #define lbi(a,x)   lb(a,x) - a.begin()
  37. #define ub(a,x)    upper_bound(all(a), x)
  38. #define ubi(a,x)   ub(a,x) - a.begin()
  39.  
  40. #define sortasc(x) sort(all(x))
  41. #define sortdes(x) sort(all(x), greater<ll>())
  42.  
  43. #define setbits(x)       __builtin_popcountll(x)
  44. #define zrobits(x)       __builtin_ctzll(x)
  45. #define leadingzrs(x)    __builtin_clzll(x)
  46. #define trailingzrs(x)   __builtin_ctzll(x)
  47. #define parity(x)        __builtin_parityll(x)
  48.  
  49. const ll M = 1e9+7;
  50.  
  51. ll mos() { return 0LL; }
  52. template<typename T, typename... Args>
  53. T mos(T a, Args... args) { return ((a + mos(args...))%M + M)%M; }
  54.  
  55. long long mop() { return 1LL; }
  56. template<typename T, typename... Args>
  57. T mop(T a, Args... args) { return (a*mop(args...))%M; }
  58.  
  59. #define pow2(x)    (1ll<<(x))
  60. #define PINF       LLONG_MAX
  61. #define NINF       LLONG_MIN
  62.  
  63. // template<typename T = ll>
  64. // using oset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
  65.  
  66. #define nl         "\n"
  67. #define print(a)   for (auto &i : a) cout << i << " "; cout << nl
  68. #define yesno(x)   cout << (((x)) ? "YES" : "NO") << nl
  69. #define cntdig(x)  floor(log(x) + 1)
  70.  
  71. #define fra(i,init,n,inc) for(i=(init);i<(n);i+=(inc))
  72. #define frd(i,init,n,dec) for(i=(init);i>=(n);i-=(dec))
  73.  
  74. #define ts to_string
  75. string ts(char c) { return string(1, c); }
  76. string ts(bool b) { return b ? "true" : "false"; }
  77. string ts(const char* s) { return (string)s; }
  78. string ts(string s) { return s; }
  79. template<class A> string ts(complex<A> c) { stringstream ss; ss << c; return ss.str(); }
  80. string ts(vector<bool> v) { string res = "{"; for(int i = 0; i < v.sz(); ++i) res += char('0' + v[i]); res += "}"; return res; }
  81. template<size_t SZ> string ts(bitset<SZ> b) { string res = ""; for(int i = 0; i < SZ; ++i) res += char('0' + b[i]); return res; }
  82. template<class A, class B> string ts(pair<A,B> p);
  83. template<class T> string ts(T v) { bool fst = 1; string res = "{"; for (const auto& x: v) { if (!fst) res += ", "; fst = 0; res += ts(x); } res += "}"; return res; }
  84. template<class A, class B> string ts(pair<A,B> p) { return "(" + ts(p.f) + ", " + ts(p.s) + ")"; }
  85.  
  86. void DBG() { cerr << "]" << endl; }
  87. template<class H, class... T> void DBG(H h, T... t) { cerr << ts(h); if (sizeof...(t)) cerr << ", "; DBG(t...); }
  88. void EDBG(string names) { names = names; }
  89. template<class H, class... T> void EDBG(string names, H h, T... t) { auto pos = names.find(','); auto first_name = names.substr(0, pos); auto rest = names.substr(pos + 1); while(rest.front() == ' ') { rest = rest.substr(1); } cerr << "[" << first_name << "] : [" << ts(h) << "]" << nl; EDBG(rest, t...); }
  90.  
  91. #ifdef LOCAL
  92. #define dbg(...) cerr << "[" << #__VA_ARGS__ << "] : [", DBG(__VA_ARGS__)
  93. #define edbg(...) EDBG(#__VA_ARGS__, __VA_ARGS__)
  94. #else
  95. #define dbg(...) 0
  96. #define edbg(...) 0
  97. #endif
  98.  
  99. template<typename T = ll>
  100. void tv(vector<T> &a, ll n) { ll i = 0; fra(i,0,n,1) { T t; cin >> t; a.pb(t); } }
  101.  
  102. void my_sol() {
  103.     ll n, m, k, i = 0, j = 0, cnt = 0, sum = 0, maxi = NINF, mini = PINF;
  104.     string s,s1,s2;
  105.     cin >> n >> m >> k;
  106.     vi g[n+1];
  107.     fra(i,0,m,1) {
  108.         ll u, v;
  109.         cin >> u >> v;
  110.         g[u].pb(v);
  111.         g[v].pb(u);
  112.     }
  113.     set<tuple<ll,ll,ll>> blocked;
  114.     fra(i,0,k,1) {
  115.         ll a, b, c;
  116.         cin >> a >> b >> c;
  117.         blocked.insert({a,b,c});
  118.     }
  119.     vector<bool> vis(n+1);
  120.     vi d(n+1), p(n+1, -1);
  121.     queue<ll> q;
  122.     q.push(1);
  123.     vis[1] = true;
  124.     d[1] = 0;
  125.     p[1] = -1;
  126.     while(!q.empty()) {
  127.         ll u = q.front();
  128.         q.pop();
  129.         for(auto v:g[u]) {
  130.             dbg(u, v, vis);
  131.             if(!vis[v]) {
  132.                 d[v]=d[u]+1;
  133.                 p[v]=u;
  134.                 vis[v] = true;
  135.                 vi cur;
  136.                 ll cn = v;
  137.                 while(cur.sz() < 3 && cn!=-1) {
  138.                     cur.pb(cn);
  139.                     cn = p[cn];
  140.                 }
  141.                 dbg(cur);
  142.                 if(cur.sz()==3) {
  143.                     tuple<ll,ll,ll> check = {cur[2], cur[1], cur[0]};
  144.                     if(blocked.count(check)) {
  145.                         dbg("NOT VISITING", u, v);
  146.                         d[v]=0;
  147.                         p[v]=-1;
  148.                         vis[v] = false;
  149.                         continue;
  150.                     }
  151.                 }
  152.                 q.push(v);
  153.             }
  154.         }
  155.     }
  156.    
  157.     if(!vis[n]) {
  158.         cout << -1 << nl;
  159.         return;
  160.     }
  161.  
  162.     vi ans;
  163.     for(ll i=n;i!=-1;i=p[i]) ans.pb(i);
  164.  
  165.     reverse(all(ans));
  166.     cout << d[n] << nl;
  167.     print(ans);
  168. }
  169.  
  170. int main() {
  171.     fast_io;
  172.     #ifndef ONLINE_JUDGE
  173.         freopen("input.txt", "r", stdin);
  174.         freopen("output.txt", "w", stdout);
  175.         auto start = std::chrono::high_resolution_clock::now();
  176.     #endif
  177.  
  178.     ll t = 1;
  179.     while(t--)
  180.         my_sol();
  181.  
  182.     #ifndef ONLINE_JUDGE
  183.         auto stop = std::chrono::high_resolution_clock::now();
  184.         auto duration = std::chrono::duration_cast<std::chrono::microseconds>(stop - start);
  185.         long double t1 = (long double)duration.count()/1000000;
  186.         cout << "\n\nTime taken "<< fixed << setprecision(6) << t1 << " seconds" << "\n";
  187.     #endif
  188.     return 0;
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement