mr_dot_convict

515-King-UVa-mr.convict

May 15th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. /*author* Priyanshu Shrivastav (from IIT Palakkad) *
  2.  * *_ __ ___  _ ______ ___  _ ____   ___  ___| |_  *
  3.  * | '_ ` _ \| '__/ __/ _ \| '_ \ \ / / |/ __| __| *
  4.  * | | | | | | | | (_| (_) | | | \ V /| | (__| |_  *
  5.  * |_| |_| |_|_|(_)___\___/|_| |_|\_/ |_|\___|\__| *
  6. When I wrote this, only God and I understood what I was doing
  7.  ** * * * * * * * Now, only God knows * * * * * * */
  8. #include         <bits/stdc++.h>
  9. #pragma GCC      optimize ("Ofast")
  10. #pragma GCC      optimize ("unroll-loops")
  11. #pragma GCC      target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
  12.  
  13. #define IOS      ios_base::sync_with_stdio(false); cin.tie (nullptr)
  14. #define PREC     cout.precision (10); cout << fixed
  15. #define x        first
  16. #define y        second
  17. using namespace std;
  18. using pii = pair <int,int>;
  19.  
  20. const int N = (int)1e2 + 10;
  21. const long long inf = (long long)1e18;
  22.  
  23. int n, m, V;
  24. vector <vector <pii> > Adj;
  25.  
  26. bool spfa (int src) {
  27.    bool inQ[N];
  28.    int rel[N];
  29.    long long d[N];
  30.    queue <int> Q;
  31.  
  32.    for (int i = 0; i < V; ++i)
  33.       inQ[i] = false, d[i] = inf, rel[i] = 0;
  34.  
  35.    d[src] = 0;
  36.    Q.push(src);
  37.    inQ[src] = true;
  38.  
  39.    while (!Q.empty()) {
  40.       int u = Q.front();
  41.       Q.pop();
  42.       inQ[u] = false;
  43.  
  44.       for (auto vPair : Adj[u]) {
  45.          int v = vPair.x;
  46.          long long w = vPair.y;
  47.  
  48.          if (d[u] != inf && d[v] > d[u] + w) {
  49.             d[v] = d[u] + w;
  50.             ++rel[v];
  51.  
  52.             if (rel[v] > V) return false;
  53.             if (!inQ[v]) {
  54.                Q.push(v);
  55.                inQ[v] = true;
  56.             }
  57.          }
  58.       }
  59.    }
  60.    return true;
  61. }
  62.  
  63. void read() {
  64.    int u, v;
  65.    long long w;
  66.    while (cin >> n, n) {
  67.       cin >> m;
  68.       V = n + 2;
  69.  
  70.       Adj.assign(V, vector <pii> ());
  71.       int si, ni;
  72.       long long ki;
  73.       string oi;
  74.  
  75.       for (int i = 0; i < n + 1; ++i)
  76.          Adj[n + 1].push_back(pii(i, 0));
  77.  
  78.       for (int i = 0; i < m; ++i) {
  79.          cin >> si >> ni >> oi >> ki;
  80.          if (oi == "lt")
  81.             u = si - 1, v = si + ni, w = ki - 1;
  82.          else if (oi == "gt")
  83.             u = si + ni, v = si - 1, w = -1*ki - 1;
  84.          else assert (false);
  85.  
  86.          Adj[u].push_back(pii(v, w));
  87.       }
  88.       if (spfa(n + 1)) cout << "lamentable kingdom\n";
  89.       else cout << "successful conspiracy\n";
  90.    }
  91. }
  92.  
  93. signed main() {
  94.    IOS; PREC;
  95.    read();
  96.  
  97.    return EXIT_SUCCESS;
  98. }
Add Comment
Please, Sign In to add comment