Language: C++

#include <bits/stdc++.h>
using namespace std;
const long long inf = 1e17;
const int MAX = 100010;
int R, C, N;

bool used[MAX + MAX];
long long val[MAX + MAX];
vector< tuple<int, long long> > G[MAX + MAX];

long long minr, minc;

void dfsc(int c, long long a){
  used[c] = true;
  minc = min( minc, a );
  val[c] = a;
  for(int i = 0; i < G[c].size(); i++){
    int r;
    long long b;
    tie(r,b) = G[c][i];
    if( !used[r] )
      dfsc( r, b - a );
  }
}

void dfsr(int r, long long a){
  used[r] = true;
  minr = min( minr, a );
  val[r] = a;
  for(int i = 0; i < G[r].size(); i++){
    int c;
    long long b;
    tie(c,b) = G[r][i];
    if( !used[c] )
      dfsc( c, b - a );
  }
}

int r[MAX], c[MAX];
long long a[MAX];
int main(){
  cin >> R >> C >> N;
  for(int i = 0; i < N; i++){
    int h, w;
    long long v;
    cin >> h >> w >> v;
    G[ h ].emplace_back( MAX + w, v, i );
    G[ MAX + w ].emplace_back( h, v, i );
    r[i] = h;
    c[i] = MAX + w;
    a[i] = v;
  }

  bool ans = true;
  for(int i = 1; i <= R; i++){
    minr = inf;
    minc = inf;
    if( ! used[i] ){
      dfsr(i, 0);
    }
    if( minr + minc < 0 ) ans = false;
  }

  for(int i = 0; i < N; i++){
    if(a[i] != val[ r[i] ] + val[ c[i] ]) ans = false;
  }
  
  if( ans ) cout << "Yes" << endl;
  else cout << "No" << endl;
}


Compiler output:
In file included from /usr/include/x86_64-linux-gnu/c++/14/bits/c++allocator.h:33,
                 from /usr/include/c++/14/bits/allocator.h:46,
                 from /usr/include/c++/14/string:43,
                 from /usr/include/c++/14/bitset:52,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:52,
                 from a.cc:1:
/usr/include/c++/14/bits/new_allocator.h: In instantiation of 'void std::__new_allocator<_Tp>::construct(_Up*, _Args&& ...) [with _Up = std::tuple<int, long long int>; _Args = {int, long long int&, int&}; _Tp = std::tuple<int, long long int>]':
/usr/include/c++/14/bits/alloc_traits.h:575:17:   required from 'static void std::allocator_traits<std::allocator<_CharT> >::construct(allocator_type&, _Up*, _Args&& ...) [with _Up = std::tuple<int, long long int>; _Args = {int, long long int&, int&}; _Tp = std::tuple<int, long long int>; allocator_type = std::allocator<std::tuple<int, long long int> >]'
  575 |           __a.construct(__p, std::forward<_Args>(__args)...);
      |           ~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/vector.tcc:117:30:   required from 'std::vector<_Tp, _Alloc>::reference std::vector<_Tp, _Alloc>::emplace_back(_Args&& ...) [with _Args = {int, long long int&, int&}; _Tp = std::tuple<int, long long int>; _Alloc = std::allocator<std::tuple<int, long long int> >; reference = std::tuple<int, long long int>&]'
  117 |             _Alloc_traits::construct(this->_M_impl, this->_M_impl._M_finish,
      |             ~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  118 |                                      std::forward<_Args>(__args)...);
      |                                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:47:24:   required from here
   47 |     G[ h ].emplace_back( MAX + w, v, i );
      |     ~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/new_allocator.h:191:11: error: no matching function for call to 'std::tuple<int, long long int>::tuple(int, long long int&, int&)'
  191 |         { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/bits/memory_resource.h:47,
                 from /usr/include/c++/14/string:68:
/usr/include/c++/14/tuple:2323:9: note: candidate: 'template<class _Alloc, class _U1, class _U2, typename std::enable_if<__is_explicitly_constructible<_U1, _U2>(), bool>::type <anonymous> > std::tuple<_T1, _T2>::tuple(std::allocator_arg_t, const _Alloc&, std::pair<_U1, _U2>&&) [with _U1 = _Alloc; _U2 = _U1; typename std::enable_if<std::_TupleConstraints<true, _T1, _T2>::__is_explicitly_constructible<_U1, _U2>(), bool>::type <anonymous> = _U2; _T1 = int; _T2 = long long int]'
 2323 |         tuple(allocator_arg_t __tag, const _Alloc& __a, pair<_U1, _U2>&& __in)
      |         ^~~~~
/usr/include/c++/14/tuple:2323:9: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/new_allocator.h:191:11: note:   mismatched types 'std::pair<_Tp1, _Tp2>' and 'int'
  191 |         { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/tuple:2314:9: note: candidate: 'template<class _Alloc, class _U1, class _U2, typename std::enable_if<__is_implicitly_constructible<_U1, _U2>(), bool>::type <anonymous> > std::tuple<_T1, _T2>::tuple(std::allocator_arg_t, const _Alloc&, std::pair<_U1, _U2>&&) [with _U1 = _Alloc; _U2 = _U1; typename std::enable_if<std::_TupleConstraints<true, _T1, _T2>::__is_implicitly_constructible<_U1, _U2>(), bool>::type <anonymous> = _U2; _T1 = int; _T2 = long long int]'
 2314 |         tuple(allocator_arg_t __tag, const _Alloc& __a, pair<_U1, _U2>&& __in)
      |         ^~~~~
/usr/include/c++/14/tuple:2314:9: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/new_allocator.h:191:11: note:   mismatched types 'std::pair<_Tp1, _Tp2>' and 'int'
  191 |         { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/tuple:2306:9: note: candidate: 'template<class _Alloc, class _U1, class _U2, typename std::enable_if<__is_explicitly_constructible<const _U1&, const _U2&>(), bool>::type <anonymous> > std::tuple<_T1, _T2>::tuple(std::allocator_arg_t, const _Alloc&, const std::pair<_U1, _U2>&) [with _U1 = _Alloc; _U2 = _U1; typename std::enable_if<std::_TupleConstraints<true, _T1, _T2>::__is_explicitly_constructible<const _U1&, const _U2&>(), bool>::type <anonymous> = _U2; _T1 = int; _T2 = long long int]'
 2306 |         tuple(allocator_arg_t __tag, const _Alloc& __a,
      |         ^~~~~
/usr/include/c++/14/tuple:2306:9: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/new_allocator.h:191:11: note:   mismatched types 'const std::pair<_Tp1, _Tp2>' and 'int'
  191 |         { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/tuple:2297:9: note: candidate: 'template<class _Alloc, class _U1, class _U2, typename std::enable_if<__is_implicitly_constructible<const _U1&, const _U2&>(), bool>::type <anonymous> > std::tuple<_T1, _T2>::tuple(std::allocator_arg_t, const _Alloc&, const std::pair<_U1, _U2>&) [with _U1 = _Alloc; _U2 = _U1; typename std::enable_if<std::_TupleConstraints<true, _T1, _T2>::__is_implicitly_constructible<const _U1&, const _U2&>(), bool>::type <anonymous> = _U2; _T1 = int; _T2 = long long int]'
 2297 |         tuple(allocator_arg_t __tag, const _Alloc& __a,
      |         ^~~~~
/usr/include/c++/14/tuple:2297:9: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/new_allocator.h:191:11: note:   mismatched types 'const std::pair<_Tp1, _Tp2>' and 'int'
  191 |         { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/tuple:2290:9: note: candidate: 'template<class _Alloc, class _U1, class _U2, typename std::enable_if<__is_explicitly_constructible<_U1, _U2>(), bool>::type <anonymous> > std::tuple<_T1, _T2>::tuple(std::allocator_arg_t, const _Alloc&, std::tuple<_U1, _U2>&&) [with _U1 = _Alloc; _U2 = _U1; typename std::enable_if<std::_TupleConstraints<true, _T1, _T2>::__is_explicitly_constructible<_U1, _U2>(), bool>::type <anonymous> = _U2; _T1 = int; _T2 = long long int]'
 2290 |         tuple(allocator_arg_t __tag, const _Alloc& __a, tuple<_U1, _U2>&& __in)
      |         ^~~~~
/usr/include/c++/14/tuple:2290:9: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/new_allocator.h:191:11: note:   mismatched types 'std::tuple<_T1, _T2>' and 'int'
  191 |         { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/tuple:2282:9: note: candidate: 'template<class _Alloc, class _U1, class _U2, typename std::enable_if<__is_implicitly_constructible<_U1, _U2>(), bool>::type <anonymous> > std::tuple<_T1, _T2>::tuple(std::allocator_arg_t, const _Alloc&, std::tuple<_U1, _U2>&&) [with _U1 = _Alloc; _U2 = _U1; typename std::enable_if<std::_TupleConstraints<true, _T1, _T2>::__is_implicitly_constructible<_U1, _U2>(), bool>::type <anonymous> = _U2; _T1 = int; _T2 = long long int]'
 2282 |         tuple(allocator_arg_t __tag, const _Alloc& __a, tuple<_U1, _U2>&& __in)
      |         ^~~~~
/usr/include/c++/14/tuple:2282:9: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/new_allocator.h:191:11: note:   mismatched types 'std::tuple<_T1, _T2>' and 'int'
  191 |         { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/tuple:2273:9: note: candidate: 'template<class _Alloc, class _U1, class _U2, typename std::enable_if<__is_explicitly_constructible<const _U1&, const _U2&>(), bool>::type <anonymous> > std::tuple<_T1, _T2>::tuple(std::allocator_arg_t, const _Alloc&, const std::tuple<_U1, _U2>&) [with _U1 = _Alloc; _U2 = _U1; typename std::enable_if<std::_TupleConstraints<true, _T1, _T2>::__is_explicitly_constructible<const _U1&, const _U2&>(), bool>::type <anonymous> = _U2; _T1 = int; _T2 = long long int]'
 2273 |         tuple(allocator_arg_t __tag, const _Alloc& __a,
      |         ^~~~~
/usr/include/c++/14/tuple:2273:9: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/new_allocator.h:191:11: note:   mismatched types 'const std::tuple<_T1, _T2>' and 'int'
  191 |         { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/tuple:2263:9: note: candidate: 'template<class _Alloc, class _U1, class _U2, typename std::enable_if<__is_implicitly_constructible<const _U1&, const _U2&>(), bool>::type <anonymous> > std::tuple<_T1, _T2>::tuple(std::allocator_arg_t, const _Alloc&, const std::tuple<_U1, _U2>&) [with _U1 = _Alloc; _U2 = _U1; typename std::enable_if<std::_TupleConstraints<true, _T1, _T2>::__is_implicitly_constructible<const _U1&, const _U2&>(), bool>::type <anonymous> = _U2; _T1 = int; _T2 = long long int]'
 2263 |         tuple(allocator_arg_t __tag, const _Alloc& __a,
      |         ^~~~~
/usr/include/c++/14/tuple:2263:9: note:   template argument deduction/substitution failed:
/usr/include/c++/14/bits/new_allocator.h:191:11: note:   mismatched types 'const std::tuple<_T1, _T2>' and 'int'
  191 |         { ::new((void *)__p) _Up(std::forward<_Args>(__args)...); }
      |           ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/tuple:2257:9: note: candidate: 'template<class _Alloc> std::tuple<_T1, _T2>::tuple(std::allocator_arg_t, const _Alloc&, std::tuple<_T1, _T2>&&) [with _T1 = int; _T2 = long long int]'
 2257 |         tuple(allocator_arg_t __tag, const _Alloc& __a, tuple&& __in)
      |         ^~~~~
/usr/include/c++/14/tuple:2257:9: note:   template argumen