Problem p03995 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define ll long long
inline ll read() {
  char ch = getchar();
  ll x = 0, f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
const ll N = 1e5 + 7;
struct node {
  ll x, y, v;
} a[N];
bool mmp(node a, node b) { return a.x < b.x; }
bool mp(node a, node b) { return a.y < b.y; }
ll dis[N], hang[N], lie[N], smi[N];
bool boo[N];
vector<ll> now, si[N], v[N];
ll mi = 1e9 + 7;
void dfs(ll x) {
  mi = min(mi, dis[x]);
  boo[x] = 1;
  now.push_back(x);
  for (ll j = 0; j < si[x].size(); ++j) {
    ll p = si[x][j];
    if (!boo[p]) {
      dis[p] = dis[x] + v[x][j];
      dfs(p);
    }
  }
}
void add(ll x, ll y, ll vv) {
  si[x].push_back(y);
  v[x].push_back(vv);
}
int main() {
  ll T = read();
  while (T--) {
    bool flat = 0;
    ll R = read(), C = read();
    ll n = read();
    for (ll i = 1; i <= n; i++) {
      a[i].x = read();
      a[i].y = read();
      a[i].v = read();
    }
    sort(a + 1, a + 1 + n, mmp);
    for (ll i = 1; i < n; i++) {
      if (a[i].x == a[i + 1].x) {
        add(a[i + 1].y, a[i].y, a[i].v - a[i + 1].v);
        add(a[i].y, a[i + 1].y, a[i + 1].v - a[i].v);
      }
    }
    for (ll i = 1; i <= C; i++)
      if (!boo[i]) {
        mi = 1e9 + 7;
        dis[i] = 0;
        dfs(i);
        for (ll j = 0; j < now.size(); ++j)
          smi[now[j]] = mi;
        now.clear();
      }
    for (ll i = 1; i <= C; i++)
      for (ll j = 0; j < si[i].size(); ++j)
        if ((dis[si[i][j]] - dis[i]) != v[i][j]) {
          flat = 1;
          break;
        }
    for (ll i = 1; i <= C; i++) {
      lie[i] = dis[i] - smi[i];
      boo[i] = 0;
      si[i].clear();
      v[i].clear();
    }

    sort(a + 1, a + 1 + n, mp);
    for (ll i = 1; i < n; i++) {
      if (a[i].x == a[i + 1].x) {
        add(a[i + 1].y, a[i].y, a[i].v - a[i + 1].v);
        add(a[i].y, a[i + 1].y, a[i + 1].v - a[i].v);
      }
    }
    for (ll i = 1; i <= R; i++)
      if (!boo[i]) {
        mi = 1e9 + 7;
        dis[i] = 0;
        dfs(i);
        for (ll j = 0; j < now.size(); ++j)
          smi[now[j]] = mi;
        now.clear();
      }
    for (ll i = 1; i <= R; i++)
      for (ll j = 0; j < si[i].size(); ++j)
        if ((dis[si[i][j]] - dis[i]) != v[i][j]) {
          flat = 1;
          break;
        }
    for (ll i = 1; i <= R; i++) {
      hang[i] = dis[i] - smi[i];
      boo[i] = 0;
      si[i].clear();
      v[i].clear();
    }
    for (ll i = 1; i <= n; i++)
      if ((a[i].v - hang[a[i].x] - lie[a[i].y]) < 0)
        flat = 1;
    if (flat)
      cout << "No" << endl;
    else
      cout << "Yes" << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll long long
inline ll read() {
  char ch = getchar();
  ll x = 0, f = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while ('0' <= ch && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}
const ll N = 1e5 + 7;
struct node {
  ll x, y, v;
} a[N];
bool mmp(node a, node b) { return a.x < b.x; }
bool mp(node a, node b) { return a.y < b.y; }
ll dis[N], hang[N], lie[N], smi[N];
bool boo[N];
vector<ll> now, si[N], v[N];
ll mi = 1e9 + 7;
void dfs(ll x) {
  mi = min(mi, dis[x]);
  boo[x] = 1;
  now.push_back(x);
  for (ll j = 0; j < si[x].size(); ++j) {
    ll p = si[x][j];
    if (!boo[p]) {
      dis[p] = dis[x] + v[x][j];
      dfs(p);
    }
  }
}
void add(ll x, ll y, ll vv) {
  si[x].push_back(y);
  v[x].push_back(vv);
}
int main() {
  ll T = 1;
  while (T--) {
    bool flat = 0;
    ll R = read(), C = read();
    ll n = read();
    for (ll i = 1; i <= n; i++) {
      a[i].x = read();
      a[i].y = read();
      a[i].v = read();
    }
    sort(a + 1, a + 1 + n, mmp);
    for (ll i = 1; i < n; i++) {
      if (a[i].x == a[i + 1].x) {
        add(a[i + 1].y, a[i].y, a[i].v - a[i + 1].v);
        add(a[i].y, a[i + 1].y, a[i + 1].v - a[i].v);
      }
    }
    for (ll i = 1; i <= C; i++)
      if (!boo[i]) {
        mi = 1e9 + 7;
        dis[i] = 0;
        dfs(i);
        for (ll j = 0; j < now.size(); ++j)
          smi[now[j]] = mi;
        now.clear();
      }
    for (ll i = 1; i <= C; i++)
      for (ll j = 0; j < si[i].size(); ++j)
        if ((dis[si[i][j]] - dis[i]) != v[i][j]) {
          flat = 1;
          break;
        }
    for (ll i = 1; i <= C; i++) {
      lie[i] = dis[i] - smi[i];
      boo[i] = 0;
      si[i].clear();
      v[i].clear();
    }

    sort(a + 1, a + 1 + n, mp);
    for (ll i = 1; i < n; i++) {
      if (a[i].x == a[i + 1].x) {
        add(a[i + 1].y, a[i].y, a[i].v - a[i + 1].v);
        add(a[i].y, a[i + 1].y, a[i + 1].v - a[i].v);
      }
    }
    for (ll i = 1; i <= R; i++)
      if (!boo[i]) {
        mi = 1e9 + 7;
        dis[i] = 0;
        dfs(i);
        for (ll j = 0; j < now.size(); ++j)
          smi[now[j]] = mi;
        now.clear();
      }
    for (ll i = 1; i <= R; i++)
      for (ll j = 0; j < si[i].size(); ++j)
        if ((dis[si[i][j]] - dis[i]) != v[i][j]) {
          flat = 1;
          break;
        }
    for (ll i = 1; i <= R; i++) {
      hang[i] = dis[i] - smi[i];
      boo[i] = 0;
      si[i].clear();
      v[i].clear();
    }
    for (ll i = 1; i <= n; i++)
      if ((a[i].v - hang[a[i].x] - lie[a[i].y]) < 0)
        flat = 1;
    if (flat)
      cout << "No" << endl;
    else
      cout << "Yes" << endl;
  }
}