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

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
const int INF = 100000000;
const int MAX_V = 301;
// d[u][v]Íu©çvÖÌRXgBßÉ×Ú·éGbWÌRXgðüêÄ¨¢½è·êÎæ¢Í¸B
int d[MAX_V][MAX_V];
// ¸_
int V;
// S_ÎÅZoHâèðÆ­O(V^3)AÓª ÁÄà®ì
void warshall_floyd() {
  for (int i = 0; i < V; i++) {
    for (int j = 0; j < V; j++) {
      for (int k = 0; k < V; k++) {
        int prv = d[j][k];
        d[j][k] = min(d[j][k], d[j][i] + d[i][k]);
      }
    }
  }
}
int main() {
  int n;
  while (cin >> n && n != 0) {
    int idx = 0;
    map<string, int> dict;
    string s, a, b, t;
    for (int j = 0; j < MAX_V; j++) {
      for (int k = 0; k < MAX_V; k++) {
        if (j == k)
          d[j][k] = 0;
        else
          d[j][k] = INF;
      }
    }
    for (int i = 0; i < n; i++) {
      cin >> s;
      cin >> a;
      if (dict.find(a) == dict.end())
        dict[a] = idx++;
      cin >> s;
      cin >> t;
      int cost;
      for (int j = 0; j < t.size(); j++) {
        if (t[j] == '^')
          cost = atoi(t.substr(j + 1).c_str());
      }
      cin >> b;
      if (dict.find(b) == dict.end())
        dict[b] = idx++;
      if (cost < 0) {
        d[dict[a]][dict[b]] = -cost;
        d[dict[b]][dict[a]] = cost;
      } else {
        d[dict[b]][dict[a]] = cost;
        d[dict[a]][dict[b]] = -cost;
      }
    }
    V = idx;
    bool no = false;
    warshall_floyd();
    // ÌÂHª êÎno
    for (int i = 0; i < idx; i++)
      if (d[i][i] < 0)
        no = true;
    if (no)
      cout << "No" << endl;
    else
      cout << "Yes" << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

const int MAX_V = 101;
// d[u][v]Íu©çvÖÌRXgBßÉ×Ú·éGbWÌRXgðüêÄ¨¢½è·êÎæ¢Í¸B
int d[MAX_V][MAX_V][MAX_V];
// ¸_
int V;
// S_ÎÅZoHâèðÆ­O(V^3)AÓª ÁÄà®ì
void warshall_floyd() {
  for (int i = 0; i < V; i++) {
    for (int j = 0; j < V; j++) {
      for (int k = 0; k < V; k++) {
        d[i][j][k] = min(d[i][j][k], d[k][j][i] + d[j][i][k]);
      }
    }
  }
}

const int INF = 1000000000;
int main() {
  int n;
  while (cin >> n && n != 0) {
    int idx = 0;
    map<string, int> dict;
    string s, a, b, t;
    for (int i = 0; i < MAX_V; i++) {
      for (int j = 0; j < MAX_V; j++) {
        for (int k = 0; k < MAX_V; k++) {
          if (j == k)
            d[i][j][k] = 0;
          else
            d[i][j][k] = INF;
        }
      }
    }
    for (int i = 0; i < n; i++) {
      cin >> s;
      cin >> a;
      if (dict.find(a) == dict.end())
        dict[a] = idx++;
      cin >> s;
      cin >> t;
      int cost;
      for (int j = 0; j < t.size(); j++) {
        if (t[j] == '^') {
          cost = atoi(t.substr(j + 1).c_str());
        }
      }
      cin >> b;
      if (dict.find(b) == dict.end())
        dict[b] = idx++;
      for (int j = 0; j < MAX_V; j++) {
        if (cost < 0) {
          d[j][dict[a]][dict[b]] = -cost;
        } else {
          d[j][dict[b]][dict[a]] = cost;
        }
      }
    }
    V = idx;
    warshall_floyd();
    bool f = false;
    for (int i = 0; i < idx; i++) {
      for (int j = 0; j < idx; j++) {
        // kðoRµÄS[·é
        int path = d[0][i][j];
        for (int k = 1; k < idx; k++) {
          if (path != d[k][i][j]) {
            f = true;
            break;
          }
        }
        if (f)
          break;
      }
      if (f)
        break;
    }
    if (f) {
      cout << "No" << endl;
    } else
      cout << "Yes" << endl;
  }

  return 0;
}