Ankit_132

Absolute Differences

Jun 28th, 2023
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.68 KB | None | 0 0
  1.  
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. #define ll     long long
  6. #define _test   int _TEST; cin>>_TEST; while(_TEST--)
  7. #define pb     push_back
  8.  
  9. int main()
  10. {
  11.     _test
  12.     {
  13.         int n, m, k;
  14.         cin>>n>>m>>k;
  15.  
  16.         vector<ll int> mmin(n, -1e18);
  17.         vector<ll int> mmax(n,  1e18);
  18.  
  19.         int f = 1;
  20.  
  21.         vector<vector<array<ll int, 2>>> va(n);
  22.         vector<int> st;
  23.  
  24.         for(int i=0; i<m; i++)
  25.         {
  26.             ll int pos, val;
  27.             cin>>pos>>val;
  28.  
  29.             pos--;
  30.  
  31.             if(mmin[pos]>val || mmax[pos]<val)
  32.                 f = 0;
  33.  
  34.             st.pb(pos);
  35.  
  36.             mmin[pos] = mmax[pos] = val;
  37.         }
  38.  
  39.         for(int i=0; i<k; i++)
  40.         {
  41.             ll int u, v, d;
  42.             cin>>u>>v>>d, u--, v--;
  43.  
  44.             va[u].pb({v, d});
  45.             va[v].pb({u, d});
  46.         }
  47.  
  48.         vector<int> vis(n);
  49.         queue<int> que;
  50.  
  51.         for(int _=0; _<n && f; _++)
  52.         {
  53.             fill(vis.begin(), vis.end(), 0);
  54.  
  55.             for(auto e: st)     que.push(e), vis[e] = 1;
  56.  
  57.             while(que.size() && f)
  58.             {
  59.                 auto e = que.front();
  60.                 que.pop();
  61.  
  62.                 for(auto [v, d]: va[e])
  63.                 {
  64.                     mmin[v] = max(mmin[v], mmin[e]-d);
  65.                     mmax[v] = min(mmax[v], mmax[e]+d);
  66.  
  67.                     f &= (mmin[v] <= mmax[v]);
  68.  
  69.                     if(!vis[v])
  70.                     {
  71.                         vis[v] = 1;
  72.                         que.push(v);
  73.                     }
  74.                 }
  75.             }
  76.         }
  77.  
  78.         if(f)       cout<<"YES\n";
  79.         else        cout<<"NO\n";
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment