Problem p01324 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 100;
const int MAX_UN = MAX_N * 2;

const int INF = 1 << 28;

/* typedef */

typedef map<string, int> msi;

/* global variables */

int n, un;
msi umap;
int dp[MAX_UN][MAX_UN];

/* subroutines */

int unit2id(string unit) {
  msi::iterator mit = umap.find(unit);
  if (mit == umap.end()) {
    umap[unit] = un;
    return un++;
  }
  return mit->second;
}

/* main */

int main() {
  for (;;) {
    cin >> n;
    if (n == 0)
      break;

    un = 0;
    umap.clear();

    for (int i = 0; i < 2 * n; i++) {
      for (int j = 0; j < 2 * n; j++)
        dp[i][j] = INF;
      dp[i][i] = 0;
    }

    for (int i = 0; i < n; i++) {
      string dmy0, ua, dmy1, exp, ub;
      cin >> dmy0 >> ua >> dmy1 >> exp >> ub;

      int aid = unit2id(ua);
      int bid = unit2id(ub);

      int sign = 1;
      int e = 0;
      int pos = 3;
      if (exp[pos] == '-') {
        sign = -1;
        pos++;
      }
      while (pos < exp.length())
        e = (10 * e) + (exp[pos++] - '0');
      e *= sign;

      dp[aid][bid] = e;
      dp[bid][aid] = -e;
      // printf("aid=%d,bid=%d,e=%d\\n", aid, bid, e);
    }

    bool ok = true;
    for (int k = 0; ok && k < un; k++)
      for (int i = 0; ok && i < un; i++)
        for (int j = 0; ok && j < un; j++) {
          if (dp[i][k] < INF && dp[k][j] < INF) {
            int dij = dp[i][k] + dp[k][j];
            if (dp[i][j] >= INF) {
              dp[i][j] = dij;
              dp[j][i] = -dij;
            } else if (dp[i][j] != dij) {
              ok = false;
              // printf("i,j,k=%d,%d,%d: %d,%d,%d\\n",
              // i, j, k, dp[i][j], dp[i][k], dp[k][j]);
            }
          }
        }

    cout << (ok ? "Yes" : "No") << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 100;
const int MAX_UN = MAX_N * 2;

const int INF = 1 << 28;

/* typedef */

typedef map<string, int> msi;

/* global variables */

int n, un;
msi umap;
int dp[MAX_N][MAX_N];

/* subroutines */

int unit2id(string unit) {
  msi::iterator mit = umap.find(unit);
  if (mit == umap.end()) {
    umap[unit] = un;
    return un++;
  }
  return mit->second;
}

/* main */

int main() {
  for (;;) {
    cin >> n;
    if (n == 0)
      break;

    un = 0;
    umap.clear();

    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++)
        dp[i][j] = INF;
      dp[i][i] = 0;
    }

    for (int i = 0; i < n; i++) {
      string dmy0, ua, dmy1, exp, ub;
      cin >> dmy0 >> ua >> dmy1 >> exp >> ub;

      int aid = unit2id(ua);
      int bid = unit2id(ub);

      int sign = 1;
      int e = 0;
      int pos = 3;
      if (exp[pos] == '-') {
        sign = -1;
        pos++;
      }
      while (pos < exp.length())
        e = (10 * e) + (exp[pos++] - '0');
      e *= sign;

      dp[aid][bid] = e;
      dp[bid][aid] = -e;
      // printf("aid=%d,bid=%d,e=%d\\n", aid, bid, e);
    }

    bool ok = true;
    for (int k = 0; ok && k < un; k++)
      for (int i = 0; ok && i < un; i++)
        for (int j = 0; ok && j < un; j++) {
          if (dp[i][k] < INF && dp[k][j] < INF) {
            int dij = dp[i][k] + dp[k][j];
            if (dp[i][j] >= INF) {
              dp[i][j] = dij;
              dp[j][i] = -dij;
            } else if (dp[i][j] != dij) {
              ok = false;
              // printf("i,j,k=%d,%d,%d: %d,%d,%d\\n",
              // i, j, k, dp[i][j], dp[i][k], dp[k][j]);
            }
          }
        }

    cout << (ok ? "Yes" : "No") << endl;
  }

  return 0;
}