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

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;
typedef unsigned long long ULL;

struct Dict {
  map<string, int> dict;
  int id;
  Dict() : id(0) {}

  int get(const string &name) {
    if (dict.count(name))
      return dict[name];
    return dict[name] = id++;
  }
};

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  while (true) {
    int N;
    cin >> N;
    if (!N)
      break;

    Dict dict;
    vector<ULL> price(N);
    vector<vector<int>> materials(N);
    for (int i = 0; i < N; ++i) {
      string name;
      int p;
      cin >> name >> p;
      price[dict.get(name)] = p;
    }
    int M;
    cin >> M;
    for (int i = 0; i < M; ++i) {
      string to;
      cin >> to;
      const int to_id = dict.get(to);
      int cnt;
      cin >> cnt;
      while (cnt--) {
        string material;
        cin >> material;
        materials[to_id].push_back(dict.get(material));
      }
    }
    vector<ULL> memo(price);
    while (true) {
      bool updated = false;
      for (int i = 0; i < N; ++i) {
        const vector<int> &list = materials[i];
        if (list.size() == 0)
          continue;
        ULL acc = 0;
        for (vector<int>::const_iterator it = list.begin(); it != list.end();
             ++it) {
          acc += memo[*it];
        }
        if (acc < memo[i]) {
          memo[i] = acc;
          updated = true;
        }
      }
      if (!updated)
        break;
    }
    string target;
    cin >> target;
    cout << memo[dict.get(target)] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;
typedef unsigned long long ULL;

struct Dict {
  map<string, int> dict;
  int id;
  Dict() : id(0) {}

  int get(const string &name) {
    if (dict.count(name))
      return dict[name];
    return dict[name] = id++;
  }
};

ULL dfs(int pos, vector<ULL> &memo, vector<int> &visiting,
        const vector<vector<int>> &materials) {
  const vector<int> &list = materials[pos];
  if (visiting[pos])
    return memo[pos];
  if (list.size() == 0)
    return memo[pos];

  visiting[pos] = 1;
  ULL acc = 0;
  for (vector<int>::const_iterator it = list.begin(); it != list.end(); ++it) {
    acc += dfs(*it, memo, visiting, materials);
  }
  visiting[pos] = 0;
  return memo[pos] = min(acc, memo[pos]);
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(0);
  while (true) {
    int N;
    cin >> N;
    if (!N)
      break;

    Dict dict;
    vector<ULL> price(N);
    vector<vector<int>> materials(N);
    for (int i = 0; i < N; ++i) {
      string name;
      int p;
      cin >> name >> p;
      price[dict.get(name)] = p;
    }
    int M;
    cin >> M;
    for (int i = 0; i < M; ++i) {
      string to;
      cin >> to;
      const int to_id = dict.get(to);
      int cnt;
      cin >> cnt;
      while (cnt--) {
        string material;
        cin >> material;
        materials[to_id].push_back(dict.get(material));
      }
    }
    string target;
    cin >> target;
    vector<ULL> memo(price);
    vector<int> visiting(N, 0);
    cout << dfs(dict.get(target), memo, visiting, materials) << endl;
  }
  return 0;
}