Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- using namespace std;
- #define ll long long
- #define _test int _TEST; cin>>_TEST; while(_TEST--)
- #define pb push_back
- int main()
- {
- _test
- {
- int n, m, k;
- cin>>n>>m>>k;
- vector<ll int> mmin(n, -1e18);
- vector<ll int> mmax(n, 1e18);
- int f = 1;
- vector<vector<array<ll int, 2>>> va(n);
- vector<int> st;
- for(int i=0; i<m; i++)
- {
- ll int pos, val;
- cin>>pos>>val;
- pos--;
- if(mmin[pos]>val || mmax[pos]<val)
- f = 0;
- st.pb(pos);
- mmin[pos] = mmax[pos] = val;
- }
- for(int i=0; i<k; i++)
- {
- ll int u, v, d;
- cin>>u>>v>>d, u--, v--;
- va[u].pb({v, d});
- va[v].pb({u, d});
- }
- vector<int> vis(n);
- queue<int> que;
- for(int _=0; _<n && f; _++)
- {
- fill(vis.begin(), vis.end(), 0);
- for(auto e: st) que.push(e), vis[e] = 1;
- while(que.size() && f)
- {
- auto e = que.front();
- que.pop();
- for(auto [v, d]: va[e])
- {
- mmin[v] = max(mmin[v], mmin[e]-d);
- mmax[v] = min(mmax[v], mmax[e]+d);
- f &= (mmin[v] <= mmax[v]);
- if(!vis[v])
- {
- vis[v] = 1;
- que.push(v);
- }
- }
- }
- }
- if(f) cout<<"YES\n";
- else cout<<"NO\n";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment