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

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

struct Node {
  vector<int> con;
  vector<int> cost;
};

const int inf = 1 << 27;

bool BellmanFord(const vector<Node> &Graph, int s, int A[], bool vis[]) {
  bool ret = false;
  const int n = Graph.size();
  for (int k = 0; k < n; ++k) {
    for (int i = 0; i < n; ++i) {
      for (int e = 0; e < (int)Graph[i].con.size(); ++e) {
        int dst = Graph[i].con[e];
        int src = i;
        if (A[dst] > A[src] + Graph[i].cost[e]) {
          A[dst] = A[src] + Graph[i].cost[e];

          if (k == n - 1) {
            A[dst] = -inf;
            ret = true;
          }
        }
      }
    }
  }
  return ret;
}

int main() {
  int n;
  while (cin >> n && n) {
    // A km = 10^B km
    // A == 1 , B == 0
    int id = 0;
    vector<Node> Graph(200);
    map<string, int> D;
    for (int i = 0; i < n; ++i) {
      char c;
      string A, B;
      int power;
      cin >> c >> A >> c >> c >> c >> c >> power >> B;
      // cout << A << ' ' << power << ' ' << B << endl;
      if (D.find(A) == D.end()) {
        D[A] = id++;
      }
      if (D.find(B) == D.end()) {
        D[B] = id++;
      }
      Graph[D[A]].con.push_back(D[B]);
      Graph[D[A]].cost.push_back(power);
      Graph[D[B]].con.push_back(D[A]);
      Graph[D[B]].cost.push_back(-power);
    }
    Graph.resize(id);
    bool vis[id];
    int A[id];
    for (int i = 0; i < id; ++i) {
      vis[i] = false;
      A[i] = inf * 2;
    }
    bool res = true;
    for (int i = 0; i < id; ++i) {
      if (!vis[i]) {
        if (BellmanFord(Graph, i, A, vis)) {
          res = false;
        }
      }
    }

    if (res) {
      puts("Yes");
    } else {
      puts("No");
    }
  }
  return 0;
}

Error: 0

Fixed submission:

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

struct Node {
  vector<int> con;
  vector<int> cost;
};

const int inf = 1 << 27;

bool BellmanFord(const vector<Node> &Graph, int s, int A[], bool vis[]) {
  bool ret = false;
  const int n = Graph.size();
  for (int k = 0; k < n; ++k) {
    for (int i = 0; i < n; ++i) {
      for (int e = 0; e < (int)Graph[i].con.size(); ++e) {
        int dst = Graph[i].con[e];
        int src = i;
        if (A[dst] > A[src] + Graph[i].cost[e]) {
          A[dst] = A[src] + Graph[i].cost[e];

          if (k == n - 1) {
            A[dst] = -inf;
            ret = true;
          }
        }
      }
    }
  }
  return ret;
}

int main() {
  int n;
  while (cin >> n && n) {
    // A km = 10^B km
    // A == 1 , B == 0
    int id = 0;
    vector<Node> Graph(100);
    map<string, int> D;
    for (int i = 0; i < n; ++i) {
      char c;
      string A, B;
      int power;
      cin >> c >> A >> c >> c >> c >> c >> power >> B;
      // cout << A << ' ' << power << ' ' << B << endl;
      if (D.find(A) == D.end()) {
        D[A] = id++;
      }
      if (D.find(B) == D.end()) {
        D[B] = id++;
      }
      Graph[D[A]].con.push_back(D[B]);
      Graph[D[A]].cost.push_back(power);
      Graph[D[B]].con.push_back(D[A]);
      Graph[D[B]].cost.push_back(-power);
    }
    Graph.resize(id);
    bool vis[id];
    int A[id];
    for (int i = 0; i < id; ++i) {
      vis[i] = false;
      A[id] = inf * 2;
    }
    bool res = true;
    for (int i = 0; i < id; ++i) {
      if (!vis[i]) {
        if (BellmanFord(Graph, i, A, vis)) {
          res = false;
        }
      }
    }

    if (res) {
      puts("Yes");
    } else {
      puts("No");
    }
  }
  return 0;
}