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

#include <bits/stdc++.h>
using namespace std;
#define MAX_N 1000
#define MAX_S 305
map<string, int> mp;
string A, B;
int si, ti;
int n;
bool used[MAX_N];
vector<int> x[MAX_N], y[MAX_N];
vector<int> G[MAX_S][MAX_S];
int t[MAX_S][MAX_S];

bool solve() {
  for (int i = 0; i < MAX_N; i++)
    used[i] = false;
  for (int i = 0; i < MAX_S; i++)
    for (int j = 0; j < MAX_S; j++)
      t[i][j] = (i == j ? 0 : 1e7);

  queue<int> qa, qb;
  qa.push(si);
  qb.push(ti);
  while (!qa.empty()) {
    int a = qa.front();
    qa.pop();
    int b = qb.front();
    qb.pop();
    if (t[a][b] == -1)
      continue;
    t[a][b] = -1;
    for (int i = 0; i < (int)G[a][b].size(); i++) {
      int id = G[a][b][i];
      if (used[id])
        continue;
      used[id] = true;
      for (int j = 0; j < (int)x[id].size(); j++) {
        qa.push(x[id][j]);
        qb.push(y[id][j]);
      }
    }
    for (int i = 0; i < MAX_S; i++) {
      if (t[b][i] == -1) {
        qa.push(a);
        qb.push(i);
      }
      if (t[i][a] == -1) {
        qa.push(i);
        qb.push(b);
      }
    }
  }

  for (int k = 0; k < MAX_S; k++)
    for (int i = 0; i < MAX_S; i++)
      for (int j = 0; j < MAX_S; j++)
        t[i][j] = min(t[i][j], t[i][k] + t[k][j]);
  for (int i = 0; i < MAX_S; i++)
    if (t[i][i] < 0)
      return false;
  return true;
}

int input(string &s) {
  cin >> s;
  if (mp.count(s) == 0) {
    int res = mp.size();
    mp[s] = res;
    return res;
  } else {
    return mp[s];
  }
}

int main() {
  si = input(A);
  ti = input(B);
  cin >> n;
  for (int i = 0; i < n; i++) {
    int q;
    cin >> q;
    for (int j = 0; j < q; j++) {
      string str;
      int L = input(str);
      int R = input(str);
      x[i].push_back(L);
      y[i].push_back(R);
      G[L][R].push_back(i);
    }
  }
  cout << (solve() ? "Yes" : "No") << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MAX_N 300
map<string, int> mp;
string A, B;
int si, ti;
int n;
bool used[MAX_N];
vector<int> x[MAX_N], y[MAX_N];
vector<int> G[MAX_N][MAX_N];
int t[MAX_N][MAX_N];

bool solve() {
  for (int i = 0; i < MAX_N; i++) {
    used[i] = false;
    for (int j = 0; j < MAX_N; j++) {
      t[i][j] = (i == j ? 0 : 1e9);
    }
  }

  queue<int> qa, qb;
  qa.push(si);
  qb.push(ti);
  while (!qa.empty()) {
    int a = qa.front();
    qa.pop();
    int b = qb.front();
    qb.pop();
    if (t[a][b] == -1)
      continue;
    t[a][b] = -1;
    for (int i = 0; i < (int)G[a][b].size(); i++) {
      int id = G[a][b][i];
      if (used[id])
        continue;
      used[id] = true;
      for (int j = 0; j < (int)x[id].size(); j++) {
        qa.push(x[id][j]);
        qb.push(y[id][j]);
      }
    }
    for (int i = 0; i < MAX_N; i++) {
      if (t[b][i] == -1) {
        qa.push(b);
        qb.push(i);
      }
      if (t[i][a] == -1) {
        qa.push(i);
        qb.push(a);
      }
    }
  }

  for (int k = 0; k < MAX_N; k++)
    for (int i = 0; i < MAX_N; i++)
      for (int j = 0; j < MAX_N; j++)
        t[i][j] = min(t[i][j], t[i][k] + t[k][j]);
  for (int i = 0; i < MAX_N; i++)
    if (t[i][i] < 0)
      return false;
  return true;
}

int input(string &s) {
  cin >> s;
  if (mp.count(s) == 0) {
    int res = mp.size();
    mp[s] = res;
    return res;
  } else {
    return mp[s];
  }
}

int main() {
  si = input(A);
  ti = input(B);
  cin >> n;
  for (int i = 0; i < n; i++) {
    int q;
    cin >> q;
    for (int j = 0; j < q; j++) {
      string str;
      int L = input(str);
      int R = input(str);
      x[i].push_back(L);
      y[i].push_back(R);
      G[L][R].push_back(i);
    }
  }
  cout << (solve() ? "Yes" : "No") << endl;
  return 0;
}