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

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

const int INF = (1 << 21);

int main() {
  int n;
  while (cin >> n && n) {
    int nn = 0;
    string str;
    getline(cin, str);
    map<string, int> m;
    vector<vector<int>> v;
    v.assign(n * 2, vector<int>(n * 2, INF));
    while (n--) {
      string str2, str3;
      bool f = false, f2 = false, minus = false;
      int a = 0;
      getline(cin, str);

      for (int i = 2; i < str.size(); i++) {
        if (!f && !f2 && str[i] != ' ') {
          str2 += str[i];
          // str2.insert(str2.size(),1,str[i]);
        } else if (!f && !f2 && str[i] == ' ') {
          f = true;
          i += 6;
          if (str[i] == '-') {
            minus = true;
            i++;
            a = str[i] - '0';
            a *= -1;
          } else
            a = str[i] - '0';
        } else if (!f2 && str[i] != ' ') {
          a *= 10;
          if (minus)
            a -= str[i] - '0';
          else
            a += str[i] - '0';
        } else if (!f2 && str[i] == ' ')
          f2 = true;
        else {
          str3 += str[i];
          // str3.insert(str3.size(),1,str[i]);
        }
      }
      if (m.find(str2) == m.end())
        m[str2] = nn++;
      if (m.find(str3) == m.end())
        m[str3] = nn++;
      // cout << str2 << " " << str3 << " " << a << endl;
      v[m[str2]][m[str3]] = a;
      v[m[str3]][m[str2]] = -1 * a;
    }

    bool ans = true;

    for (int k = 0; k < v.size(); k++) {
      for (int i = 0; i < v.size(); i++) {
        for (int j = 0; j < v.size(); j++) {
          if (i != j && v[i][j] == INF && v[i][k] < INF && v[k][j] < INF)
            v[i][j] = v[i][k] + v[k][j];
        }
      }
    }

    for (int k = 0; k < v.size(); k++) {
      for (int i = 0; i < v.size(); i++) {
        for (int j = 0; j < v.size(); j++) {
          if (v[i][j] >= INF || v[i][k] >= INF || v[k][j] >= INF)
            continue;
          if (v[i][j] != v[i][k] + v[k][j]) {
            ans = false;
            break;
          }
        }
        if (!ans)
          break;
      }
      if (!ans)
        break;
    }

    /*for(int i=0;i<v.size();i++){
      for(int j=0;j<v.size();j++){
        if(v[i][j] == INF) cout << " I ";
        else printf("%2d ",v[i][j]);
      }
      cout << endl;
      }*/

    if (ans)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

const int INF = (1 << 21);

int main() {
  int n;
  while (cin >> n && n) {
    int nn = 0;
    string str;
    getline(cin, str);
    map<string, int> m;
    // vector<vector<int> > v;
    // v.assign(n, vector<int>(n,INF));
    vector<int> v[100];
    for (int i = 0; i < n; i++)
      v[i].assign(n, INF);
    while (n--) {
      string str2, str3;
      bool f = false, f2 = false, minus = false;
      int a = 0;
      getline(cin, str);

      for (int i = 2; i < str.size(); i++) {
        if (!f && !f2 && str[i] != ' ') {
          // str2 += str[i];
          str2.insert(str2.size(), 1, str[i]);
        } else if (!f && !f2 && str[i] == ' ') {
          f = true;
          i += 6;
          if (str[i] == '-') {
            minus = true;
            i++;
            a = str[i] - '0';
            a *= -1;
          } else
            a = str[i] - '0';
        } else if (!f2 && str[i] != ' ') {
          a *= 10;
          if (minus)
            a -= str[i] - '0';
          else
            a += str[i] - '0';
        } else if (!f2 && str[i] == ' ')
          f2 = true;
        else {
          // str3 += str[i];
          str3.insert(str3.size(), 1, str[i]);
        }
      }
      if (m.find(str2) == m.end())
        m[str2] = nn++;
      if (m.find(str3) == m.end())
        m[str3] = nn++;
      // cout << str2 << " " << str3 << " " << a << endl;
      v[m[str2]][m[str3]] = a;
      v[m[str3]][m[str2]] = -1 * a;
    }

    bool ans = true;

    for (int k = 0; k < v[0].size(); k++) {
      for (int i = 0; i < v[0].size(); i++) {
        for (int j = 0; j < v[0].size(); j++) {
          if (i != j && v[i][j] == INF && v[i][k] < INF && v[k][j] < INF)
            v[i][j] = v[i][k] + v[k][j];
        }
      }
    }

    for (int k = 0; k < v[0].size(); k++) {
      for (int i = 0; i < v[0].size(); i++) {
        for (int j = 0; j < v[0].size(); j++) {
          if (v[i][j] >= INF || v[i][k] >= INF || v[k][j] >= INF)
            continue;
          if (v[i][j] != v[i][k] + v[k][j]) {
            ans = false;
            break;
          }
        }
        if (!ans)
          break;
      }
      if (!ans)
        break;
    }

    /*for(int i=1;i<v.size();i++){
      for(int j=1;j<v.size();j++){
        if(v[i][j] == INF) cout << " I ";
        else printf("%2d ",v[i][j]);
      }
      cout << endl;
      }*/

    if (ans)
      cout << "Yes" << endl;
    else
      cout << "No" << endl;
  }
  return 0;
}