Advertisement
sleepy_coder

Negative Cycle

Nov 17th, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.48 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/tree_policy.hpp>
  3. #include <ext/pb_ds/assoc_container.hpp>
  4. #include <ext/pb_ds/detail/standard_policies.hpp>
  5. using namespace   std;
  6. using namespace __gnu_cxx;
  7. using namespace __gnu_pbds;
  8.  
  9. typedef long long ll;
  10. typedef vector<ll> vl;
  11. typedef vector<int> vi;
  12. typedef pair<ll, ll> pll;
  13. typedef pair<int, int> pii;
  14.  
  15. #define     sz                 size()
  16. #define     pb                 push_back
  17. #define     inf                (1<<30)
  18. #define     mod                (1000000007)
  19. #define     pi                 (acos(-1.0))
  20. #define     rep(i, n)          for(__typeof(n) i=0; i<(n); i++)
  21. #define     foab(i, a, b)      for(__typeof(b) i=(a); i<=(b); i++)
  22. #define     foba(i, a, b)      for(__typeof(b) i=(b); i>=(a); i--)
  23. #define     foch(it, l)        for(__typeof((l).begin()) it = (l).begin();  it != (l).end(); ++it)
  24. #define     what_is_x(x)       cout << (#x) << " is " << (x) << endl;
  25.  
  26. template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
  27. template<typename T> ostream& operator<<(ostream& os, const vector<T> &t) { ll n = t.size(); rep(i, n) os<<t[i]<<" "; return os; }
  28. template<typename T,typename TT> ostream& operator<<(ostream &os, const pair<T,TT> &t) { return os<<"("<<t.first<<","<<t.second<<")"; }
  29. /**__________________________________________________________________________________________________________________________________*/
  30.  
  31.  
  32. struct data{
  33.     int u, v, w;
  34.     int prevIndx;
  35. }edgeList[205];
  36.  
  37.  
  38. int ara[205];
  39. int d[205];
  40. bool cycle[205];
  41. int lastIndx[205];
  42. int nodes, edges;
  43.  
  44. void dfs(int u){
  45.     if(!cycle[u]){
  46.         cycle[u] = true;
  47.         for(int e = lastIndx[u] ; e != -1 ; e = edgeList[e].prevIndx){
  48.             int v = edgeList[e].v;
  49.             dfs(v);
  50.         }
  51.     }
  52. }
  53.  
  54. void bellmanFord(int source = 1){
  55.     foab(i, 1, nodes){
  56.         d[i] = inf;
  57.         cycle[i] = false;
  58.     }
  59.  
  60.     d[source] = 0;
  61.  
  62.     foab(i, 1, nodes-1){
  63.         foab(j, 1, edges){
  64.             int u = edgeList[j].u, v = edgeList[j].v, w = edgeList[j].w;
  65.             if(d[u] < inf){
  66.                 if(d[v] > d[u] + w){
  67.                     d[v] = d[u] + w;
  68.                 }
  69.             }
  70.         }
  71.     }
  72.  
  73.     foab(j, 1, edges){
  74.         int u = edgeList[j].u, v = edgeList[j].v, w = edgeList[j].w;
  75.         if(d[u] < inf && !cycle[u]){
  76.             if(d[v] > d[u] + w){
  77.                 dfs(u);
  78.             }
  79.         }
  80.     }
  81. }
  82.  
  83.  
  84.  
  85. int main(){
  86. #ifdef LOCAL
  87.     freopen("input.txt", "r", stdin);
  88.     freopen("output.txt", "w", stdout);
  89. #endif
  90.     ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  91.  
  92.     int T;cin>>T;
  93.     foab(t, 1, T){
  94.         cin>>nodes;
  95.         foab(i, 1, nodes){
  96.             cin>>ara[i];
  97.             lastIndx[i] = -1;
  98.         }
  99.         cin>>edges;
  100.         foab(i, 1, edges){
  101.             int u, v;
  102.             cin>>u>>v;
  103.             edgeList[i].u = u, edgeList[i].v = v, edgeList[i].w = (ara[v] - ara[u])*(ara[v] - ara[u])*(ara[v] - ara[u]);
  104.             edgeList[i].prevIndx = lastIndx[u];
  105.             lastIndx[u] = i;
  106.         }
  107.  
  108.         bellmanFord(1);
  109.  
  110.         int q;
  111.         cin>>q;
  112.         cout<<"Case "<<t<<":\n";
  113.         while(q--){
  114.             int vv;
  115.             cin>>vv;
  116.             if(cycle[vv] || d[vv] == inf || d[vv] < 3){
  117.                 cout<<"?"<<endl;
  118.             }
  119.             else{
  120.                 cout<<d[vv]<<endl;
  121.             }
  122.         }
  123.  
  124.     }    
  125.    
  126.     return 0;
  127.    
  128. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement