alexjercan/bugnet huggingface dataset:
Problem p00200 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back

int n, m;

int C[200][200];
int T[200][200];
int d[200];

int dijk(int s, int g, int r) {
  bool used[200];
  rep(i, 200) used[i] = false;
  rep(i, 200) d[i] = 999999999;

  d[s] = 0;

  while (1) {
    int v = -1;
    rep(u, m) {
      if (!used[u] && (v == -1 || d[u] < d[v]))
        v = u;
    }
    if (v == -1)
      break;

    used[v] = true;

    rep(u, m) {
      if (r == 0)
        d[u] = min(d[u], d[v] + C[v][u]);
      else
        d[u] = min(d[u], d[v] + T[v][u]);
    }
  }
  return d[g];
}

int main() {
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;

    rep(i, 200) rep(j, 200) {
      C[i][j] = 999999999;
      T[i][j] = 999999999;
    }
    rep(i, n) {
      int a, b, c, t;
      cin >> a >> b >> c >> t;
      a--, b--;
      C[a][b] = c;
      C[b][a] = c;
      T[a][b] = t;
      T[b][a] = t;
    }

    int k;
    cin >> k;
    rep(i, k) {
      int s, g, r;
      cin >> s >> g >> r;
      s--, g--;
      cout << dijk(s, g, r) << endl;
    }
  }
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back

int n, m;

int C[200][200];
int T[200][200];
int d[200];

int dijk(int s, int g, int r) {
  bool used[200];
  rep(i, 200) used[i] = false;
  rep(i, 200) d[i] = 999999999;

  d[s] = 0;

  while (1) {
    int v = -1;
    rep(u, n) {
      if (!used[u] && (v == -1 || d[u] < d[v]))
        v = u;
    }
    if (v == -1)
      break;

    used[v] = true;

    rep(u, n) {
      if (r == 0)
        d[u] = min(d[u], d[v] + C[v][u]);
      else
        d[u] = min(d[u], d[v] + T[v][u]);
    }
  }
  return d[g];
}

int main() {
  while (cin >> n >> m) {
    if (n == 0 && m == 0)
      break;

    rep(i, 200) rep(j, 200) {
      C[i][j] = 999999999;
      T[i][j] = 999999999;
    }
    rep(i, n) {
      int a, b, c, t;
      cin >> a >> b >> c >> t;
      a--, b--;
      C[a][b] = c;
      C[b][a] = c;
      T[a][b] = t;
      T[b][a] = t;
    }

    int k;
    cin >> k;
    rep(i, k) {
      int s, g, r;
      cin >> s >> g >> r;
      s--, g--;
      cout << dijk(s, g, r) << endl;
    }
  }
}
Problem p00200 (C++) — buggy submission, verdict: Runtime Error

// #include "bipartite-graph.hpp"
// #include <vector>
// using namespace std;
// int main(){
//	vector<vector<int> > G(4);
//	cout << bipartite_graph(G) << endl;
// }

// AOJ0200
#include <iostream>
#include <vector>
using namespace std;

struct edge {
  edge() {}
  edge(int to, int from, int cost, int time)
      : to(to), from(from), cost(cost), time(time) {}
  int to, from, cost, time;
};

const int INF = 100000000;

int d[101];
edge es[7000];
int n, m;

// 頂点sから各頂点への最短経路を求める
void bellman_ford(int s, bool r) {
  for (int i = 0; i < m; ++i)
    d[i] = INF;
  d[s] = 0;
  while (1) {
    bool update = false;
    for (int i = 0; i < n * 2; ++i) {
      edge e = es[i];
      int cost;
      if (r)
        cost = e.time;
      else
        cost = e.cost;
      if (d[e.from] != INF && d[e.to] > d[e.from] + cost) {
        d[e.to] = d[e.from] + cost;
        update = true;
      }
    }
    if (!update)
      break;
  }
}

int main() {
  while (cin >> n >> m, n + m) {
    for (int i = 0; i < n; ++i) {
      int to, from, cost, time;
      cin >> to >> from >> cost >> time;
      es[i] = edge(to - 1, from - 1, cost, time);
      es[n + i] = edge(from - 1, to - 1, cost, time);
    }
    int k;
    cin >> k;
    for (int i = 0; i < k; ++i) {
      int p, q, r;
      cin >> p >> q >> r;
      bellman_ford(p - 1, r);
      cout << d[q - 1] << endl;
    }
  }
}

Error: 0

Fixed submission:

// #include "bipartite-graph.hpp"
// #include <vector>
// using namespace std;
// int main(){
//	vector<vector<int> > G(4);
//	cout << bipartite_graph(G) << endl;
// }

// AOJ0200
#include <iostream>
#include <vector>
using namespace std;

struct edge {
  edge() {}
  edge(int to, int from, int cost, int time)
      : to(to), from(from), cost(cost), time(time) {}
  int to, from, cost, time;
};

const int INF = 100000000;

int d[101];
edge es[2000];
int n, m;

// 頂点sから各頂点への最短経路を求める
void bellman_ford(int s, bool r) {
  for (int i = 0; i < m; ++i)
    d[i] = INF;
  d[s] = 0;
  while (1) {
    bool update = false;
    for (int i = 0; i < n * 2; ++i) {
      edge e = es[i];
      int cost;
      if (r)
        cost = e.time;
      else
        cost = e.cost;
      if (d[e.from] != INF && d[e.to] > d[e.from] + cost) {
        d[e.to] = d[e.from] + cost;
        update = true;
      }
    }
    if (!update)
      break;
  }
}

int main() {
  while (cin >> n >> m, n + m) {
    for (int i = 0; i < n; ++i) {
      int to, from, cost, time;
      cin >> to >> from >> cost >> time;
      es[i] = edge(to - 1, from - 1, cost, time);
      es[n + i] = edge(from - 1, to - 1, cost, time);
    }
    int k;
    cin >> k;
    for (int i = 0; i < k; ++i) {
      int p, q, r;
      cin >> p >> q >> r;
      bellman_ford(p - 1, r);
      cout << d[q - 1] << endl;
    }
  }
}
Problem p00200 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define SORT(c) sort((c).begin(), (c).end())
#define pb push_back
using namespace std;

const int INF = 10000000;
int V;
struct edge {
  int to, money, time;
};
struct state {
  int from, d;
  bool operator<(const state &a) const { return d > a.d; }
};
vector<vector<edge>> G;

int d[111];
int ds(int s, int g, int r) {
  FOR(i, 0, 111) d[i] = INF;
  d[s] = 0;
  priority_queue<state> q;
  q.push({s, 0});
  while (q.size()) {
    state sta = q.top();
    q.pop();
    int from = sta.from;
    FOR(i, 0, G[from].size()) {
      const edge &e = G[from][i];
      int to = e.to;
      int cost = r ? e.time : e.money;
      if (d[to] > d[from] + cost) {
        d[to] = d[from] + cost;
        q.push({to, d[to]});
      }
    }
  }
  return d[g];
}

int main() {
  int n;
  while (1) {
    cin >> n >> V;
    if (!(n || V))
      break;
    G = vector<vector<edge>>(n + 1);
    FOR(i, 0, n) {
      int a, b, m, t;
      cin >> a >> b >> m >> t;
      G[a].pb({b, m, t});
      G[b].pb({a, m, t});
    }
    int k;
    cin >> k;
    FOR(i, 0, k) {
      int p, q, r;
      cin >> p >> q >> r;
      cout << ds(p, q, r) << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define SORT(c) sort((c).begin(), (c).end())
#define pb push_back
using namespace std;

const int INF = 10000000;
int V;
struct edge {
  int to, money, time;
};
struct state {
  int from, d;
  bool operator<(const state &a) const { return d < a.d; }
};
vector<vector<edge>> G;

int d[111];
int ds(int s, int g, int r) {
  FOR(i, 0, 111) d[i] = INF;
  d[s] = 0;
  vector<state> q;
  q.pb({s, 0});
  while (q.size()) {
    state sta = q.back();
    q.pop_back();
    int from = sta.from;
    FOR(i, 0, G[from].size()) {
      const edge &e = G[from][i];
      int to = e.to;
      int cost = r ? e.time : e.money;
      if (d[to] > d[from] + cost) {
        d[to] = d[from] + cost;
        q.pb({to, d[to]});
      }
    }
    SORT(q);
  }
  return d[g];
}

int main() {
  int n;
  while (1) {
    cin >> n >> V;
    if (!(n || V))
      break;
    G = vector<vector<edge>>(n + 1);
    FOR(i, 0, n) {
      int a, b, m, t;
      cin >> a >> b >> m >> t;
      G[a].pb({b, m, t});
      G[b].pb({a, m, t});
    }
    int k;
    cin >> k;
    FOR(i, 0, k) {
      int p, q, r;
      cin >> p >> q >> r;
      cout << ds(p, q, r) << endl;
    }
  }
}
Problem p00200 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <set>
#include <sstream>
#include <string>

using namespace std;

#define loop(i, a, b) for (int i = (a); i < int(b); i++)
#define rep(i, b) loop(i, 0, b)

typedef long long ll;
ll d[200][200];
ll e[200][200];
int main() {
  int n, m;
  while (cin >> n >> m && n) {
    rep(i, 200) rep(j, 200) d[i][j] = 1LL << 60;
    rep(i, 200) d[i][i] = 0;
    rep(i, 200) rep(j, 200) e[i][j] = 1LL << 60;
    rep(i, 200) e[i][i] = 0;
    rep(i, n) {
      ll a, b, c, c2;
      cin >> a >> b >> c >> c2;
      // printf("%lld %lld %lld %lld\\n",a,b,c,c2);
      d[a][b] = d[b][a] = c;
      e[a][b] = e[b][a] = c2;
    }
    rep(k, 200) rep(i, 200) rep(j, 200) d[i][j] =
        min(d[i][j], d[i][k] + d[k][j]);
    rep(k, 200) rep(i, 200) rep(j, 200) e[i][j] =
        min(e[i][j], e[i][k] + e[k][j]);
    int k;
    cin >> k;
    rep(i, k) {
      int p, q, r;
      cin >> p >> q >> r;
      auto t = r ? e : d;
      cout << t[p][q] << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;

int d[1000][1000][2];
int main() {
  int n, m;
  while (cin >> n >> m && n | m) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        d[i][j][0] = d[i][j][1] = 1 << 29;
        if (i == j)
          d[i][j][0] = d[i][j][1] = 0;
      }
    }
    for (int i = 0; i < n; i++) {
      int a, b, c, dd;
      cin >> a >> b >> c >> dd;
      a--, b--;
      d[a][b][0] = d[b][a][0] = c;
      d[a][b][1] = d[b][a][1] = dd;
    }
    for (int k = 0; k < n; k++) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          d[i][j][0] = min(d[i][j][0], d[i][k][0] + d[k][j][0]);
          d[i][j][1] = min(d[i][j][1], d[i][k][1] + d[k][j][1]);
        }
      }
    }
    int q;
    cin >> q;
    while (q--) {
      int x, y;
      cin >> x >> y;
      x--, y--;
      int a;
      cin >> a;
      cout << d[x][y][a] << endl;
    }
  }
}
Problem p00200 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>

using namespace std;

#define max_n 3000
#define INF (max_n + 100) * (1000 + 1000)

struct edge {
  int to, cost[2]; /// cost[0] 最少金額 cost[1] 最短距離
};

typedef pair<int, int> P;

int main(void) {
  int n, m, k;
  int p, q, r;
  int a, b, c, t;
  vector<edge> N[max_n];
  edge e;
  int d[max_n];
  priority_queue<P, vector<P>, greater<P>> que;
  int v;
  P pi;

  while (1) {

    cin >> n >> m;
    if (n == 0 && m == 0)
      break;

    for (int i = 0; i < max_n; i++)
      N[i].clear();

    for (int i = 0; i < n; i++) {
      cin >> a >> b >> c >> t;
      e.to = b, e.cost[0] = c, e.cost[1] = t;
      N[a].push_back(e);
      e.to = a;
      N[b].push_back(e);
    }

    cin >> k;

    for (int i = 0; i < k; i++) {

      cin >> p >> q >> r;

      fill(d, d + n, INF);
      d[p] = 0;
      que.push(P(0, p));
      while (!que.empty()) {
        pi = que.top();
        que.pop();
        v = pi.second;
        if (d[v] < pi.first)
          continue;
        for (int j = 0; j < N[v].size(); j++) {
          e = N[v][j];
          if (d[e.to] > d[v] + e.cost[r]) {
            d[e.to] = d[v] + e.cost[r];
            que.push(P(d[e.to], e.to));
          }
        }
      }

      cout << d[q] << endl;
      ;
    }
  }

  return 0;
}

Error: 0

Fixed submission:

#include <cmath>
#include <cstdio>
#include <iostream>
#include <queue>

#define NMAX 3000 * 2
#define MMAX 100 * 2
#define TMAX 1000 * 2
#define CMAX 1000 * 2
#define KMAX 200 * 2

using namespace std;

int main() {
  std::ios_base::sync_with_stdio(false);

  int n, m;
  int a, b, time, cost;
  int data[MMAX][MMAX][3];
  int k;
  int p, q, r;
  int dp[MMAX];
  queue<int> now;

  while (1) {
    for (int i = 0; i < MMAX; i++) {
      for (int j = 0; j < MMAX; j++) {
        for (int k = 0; k < 3; k++) {
          data[i][j][k] = -1;
        }
      }
    }

    for (int i = 0; i < MMAX; i++) {
      dp[i] = -1;
    }

    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i < n; i++) {
      cin >> a >> b >> cost >> time;
      data[a][b][1] = time;
      data[b][a][1] = time;
      data[a][b][0] = cost;
      data[b][a][0] = cost;
    }

    cin >> k;
    for (int i = 0; i < k; i++) {
      cin >> p >> q >> r;
      now.push(p);
      dp[p] = 0;
      while (!now.empty()) {
        for (int i = 0; i < n; i++) {
          if (data[now.front()][i][r] != -1) {
            if (dp[i] == -1 ||
                dp[i] > dp[now.front()] + data[now.front()][i][r]) {
              dp[i] = dp[now.front()] + data[now.front()][i][r];
              now.push(i);
            }
          }
        }
        now.pop();
      }
      cout << dp[q] << endl;
    }
  }

  return 0;
}
Problem p00201 (C++) — buggy submission, verdict: Time Limit Exceeded

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

map<string, int> price;
map<string, vector<string>> mix;

int res(string s) {
  if (!mix.count(s))
    return price[s];
  int ans = 0;
  for (int i = 0; i < mix[s].size(); i++) {
    ans += res(mix[s][i]);
  }
  if (price[s] > ans)
    price[s] = ans;
  return price[s];
}

int main() {
  int n, b, c;
  string a, d;
  while (cin >> n, n) {
    price.clear();
    mix.clear();
    for (int i = 0; i < n; i++) {
      cin >> a >> b;
      price[a] = b;
    }
    cin >> c;
    for (int i = 0; i < c; i++) {
      cin >> a >> b;
      for (int i = 0; i < b; i++) {
        cin >> d;
        mix[a].push_back(d);
      }
    }
    cin >> d;
    cout << res(d) << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

#define mp make_pair
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repi(i, a, b) for (int i = (a); i < (b); i++)

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef pair<int, int> pii;

const int INF = 1 << 29;
const double EPS = 1e-9;

const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
map<string, int> data;
map<string, vector<string>> ren;

int res(string s) {
  if (!ren.count(s))
    return data[s];
  int ans = 0;
  for (int i = 0; i < ren[s].size(); i++) {
    ans += (res(ren[s][i]));
  }
  if (data[s] > ans) {
    data[s] = ans;
  }
  return data[s];
}

int main() {
  int n, m;
  while (cin >> n, n) {
    for (int i = 0; i < n; i++) {
      string item;
      int cst;
      cin >> item >> cst;
      data[item] = cst;
    }
    cin >> m;
    int k;
    for (int i = 0; i < m; i++) {
      string renkin;
      cin >> renkin >> k;
      // int sum=0;
      for (int j = 0; j < k; j++) {
        string tmp;
        cin >> tmp;
        ren[renkin].push_back(tmp);
      }
      // data[renkin]=min(sum,data[renkin]);
    }
    string ans;
    cin >> ans;
    cout << res(ans) << endl;
  }
  return 0;
}
Problem p00201 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <vector>
#define F first
#define S second
#define MAX 10000000
using namespace std;
typedef pair<int, bool> P;
map<string, P> material;
vector<string> vec[110];
map<string, P> index;

int rec(string target) {

  int value = MAX;
  map<string, P>::iterator it = material.begin();
  while (it != material.end()) {
    if ((*it).F == target)
      value = min(value, (*it).S.F);
    it++;
  }

  int X = 10000000;
  int id = -1, pre = X;
  if (index[target].S)
    id = index[target].F;

  if (id != -1) {
    for (int i = 0; i < vec[id].size(); i++) {
      pre += rec(vec[id][i]);
    }
  }

  value = min(id == -1 ? X : pre - X, value);

  return value;
}

int main() {
  int n, m;

  while (true) {
    cin >> n;
    if (n == 0)
      break;

    material.clear();
    for (int i = 0; i < n; i++) {
      vec[i].clear();
      string s;
      int val;
      cin >> s >> val;

      material[s] = P(val, true);
    }

    index.clear();
    cin >> m;
    for (int i = 0; i < m; i++) {
      string s, ss;
      int cn;
      cin >> s >> cn;
      index[s] = P(i, true);
      for (int j = 0; j < cn; j++) {
        cin >> ss;
        vec[i].push_back(ss);
      }
    }

    string st;
    cin >> st;
    cout << rec(st) << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
#include <vector>
#define F first
#define S second
#define MAX 10000000
using namespace std;
typedef pair<int, bool> P;
map<string, P> material;
vector<string> vec[110];
map<string, P> index;

int rec(string target) {

  int value = MAX;
  map<string, P>::iterator it = material.begin();
  while (it != material.end()) {
    if ((*it).F == target)
      value = min(value, (*it).S.F);
    it++;
  }

  int X = 10000000;
  int id = -1, pre = X;
  if (index[target].S)
    id = index[target].F;

  if (id != -1) {
    for (int i = 0; i < vec[id].size(); i++) {
      pre += rec(vec[id][i]);
    }
  }

  value = min(id == -1 ? X : pre - X, value);
  assert(value != MAX);
  return value;
}

int main() {
  int n, m;

  while (true) {
    cin >> n;
    if (n == 0)
      break;

    material.clear();
    for (int i = 0; i < n; i++) {
      vec[i].clear();
      string s;
      int val;
      cin >> s >> val;

      material[s] = P(val, true);
    }

    index.clear();
    cin >> m;
    for (int i = 0; i < m; i++) {
      string s, ss;
      int cn;
      cin >> s >> cn;
      index[s] = P(i, true);
      for (int j = 0; j < cn; j++) {
        cin >> ss;
        vec[i].push_back(ss);
      }
    }

    string st;
    cin >> st;
    cout << rec(st) << endl;
  }
  return 0;
}
Problem p00201 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
int n, m;
map<string, int> t;
int c[111];
vector<int> v[111];

int solve(int k) {
  int sum = 0;
  if (v[k].empty())
    return c[k];
  for (int i = 0; i < (int)v[k].size(); i++) {
    sum += solve(v[k][i]);
  }
  return min(sum, c[k]);
}

int main(void) {
  while (cin >> n && n) {
    t.clear();
    for (int i = 0; i < 111; i++)
      v[i].clear();
    for (int i = 0; i < n; i++) {
      string s;
      int a;
      cin >> s >> a;
      t[s] = i;
      c[i] = a;
    }
    cin >> m;
    for (int j = 0; j < m; j++) {
      string s;
      int a;
      cin >> s >> a;
      int id = t[s];
      for (int i = 0; i < a; i++) {
        cin >> s;
        v[id].push_back(t[s]);
      }
    }
    string s;
    cin >> s;
    cout << solve(t[s]) << endl;
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;
int n, m;
map<string, int> t;
int c[111];
vector<int> v[111];

int solve(int k) {
  int sum = 0;
  if (v[k].empty())
    return c[k];
  for (int i = 0; i < (int)v[k].size(); i++) {
    sum += solve(v[k][i]);
  }
  return min(sum, c[k]);
}

int main(void) {
  while (cin >> n && n) {
    t.clear();
    for (int i = 0; i < 111; i++)
      v[i].clear();
    for (int i = 0; i < n; i++) {
      string s;
      int a;
      cin >> s >> a;
      t[s] = n;
      c[n] = a;
    }
    cin >> m;
    for (int j = 0; j < m; j++) {
      string s;
      int a;
      cin >> s >> a;
      int id = t[s];
      for (int i = 0; i < a; i++) {
        cin >> s;
        v[id].push_back(t[s]);
      }
    }
    string s;
    cin >> s;
    cout << solve(t[s]) << endl;
  }
}
Problem p00201 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using namespace std;

map<string, vector<string>> recipe;
map<string, int> cost;

int search(string item) {
  if (recipe[item].size() == 0)
    return cost[item];
  else {
    int sum = 0;
    for (int i = 0; i < recipe[item].size(); i++) {
      sum += search(recipe[item][i]);
    }
    return min(cost[item], sum);
  }
}

int main() {
  int n;
  while (cin >> n, n) {
    recipe.clear();
    cost.clear();
    for (int i = 0; i < n; i++) {
      string name;
      int m;
      cin >> name >> m;
      cost[name] = m;
    }
    cin >> n;
    for (int i = 0; i < n; i++) {
      string item;
      cin >> item;
      int m;
      cin >> m;
      for (int j = 0; j < m; j++) {
        string material;
        cin >> material;
        recipe[item].push_back(material);
      }
    }
    string item;
    cin >> item;
    cout << search(item) << endl;
  }
}

Error: TLE

Fixed submission:

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

using namespace std;

map<string, vector<string>> recipe;
map<string, int> cost;

int search(string item) {
  if (recipe[item].size() == 0)
    return cost[item];
  else {
    int sum = 0;
    for (int i = 0; i < recipe[item].size(); i++) {
      sum += search(recipe[item][i]);
    }
    return min(cost[item], sum);
  }
}

int main() {
  int n;
  while (cin >> n, n) {
    for (int i = 0; i < n; i++) {
      string name;
      int m;
      cin >> name >> m;
      cost[name] = m;
    }
    cin >> n;
    for (int i = 0; i < n; i++) {
      string item;
      cin >> item;
      int m;
      cin >> m;
      for (int j = 0; j < m; j++) {
        string material;
        cin >> material;
        recipe[item].push_back(material);
      }
    }
    string item;
    cin >> item;
    cout << search(item) << endl;
  }
}
Problem p00201 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

map<string, int> v;
map<string, vector<string>> l;

int main() {
  int n;
  while (scanf("%d", &n), n) {
    v.clear();
    l.clear();

    for (int i = 0; i < n; i++) {
      string str;
      int p;
      cin >> str >> p;
      v[str] = p;
    }

    int m;
    scanf("%d", &m);
    for (int i = 0; i < m; i++) {
      string o;
      int k;
      cin >> o >> k;
      for (int j = 0; j < k; j++) {
        string q;
        cin >> q;
        l[o].push_back(q);
      }
    }

    while (true) {
      bool update = false;

      typedef map<string, vector<string>>::iterator ite;
      for (ite i = l.begin(); i != l.end(); ++i) {
        int sum = 0;
        for (int j = 0; j < (*i).second.size(); j++) {
          sum += v[(*i).second[j]];
        }
        if (v[(*i).first] > sum) {
          v[(*i).first] = sum;
          update = true;
        }
      }
      if (!update)
        break;
    }

    string t;
    cin >> t;
    cout << v[t] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

map<string, int> v;
map<string, vector<string>> l;

int main() {
  int n;
  while (scanf("%d", &n), n) {
    for (int i = 0; i < n; i++) {
      string str;
      int p;
      cin >> str >> p;
      v[str] = p;
    }

    int m;
    scanf("%d", &m);
    for (int i = 0; i < m; i++) {
      string o;
      int k;
      cin >> o >> k;
      for (int j = 0; j < k; j++) {
        string q;
        cin >> q;
        l[o].push_back(q);
      }
    }

    for (int k = 0; k < 10000; k++) {
      // while (true){
      bool update = false;

      typedef map<string, vector<string>>::iterator ite;
      for (ite i = l.begin(); i != l.end(); ++i) {
        int sum = 0;
        for (int j = 0; j < (*i).second.size(); j++) {
          sum += v[(*i).second[j]];
        }
        if (v[(*i).first] > sum) {
          v[(*i).first] = sum;
          update = true;
        }
      }

      //	if (!update) break;
    }

    string t;
    cin >> t;
    cout << v[t] << endl;
  }

  return 0;
}