Problem p01158 (C++) — buggy submission, verdict: Memory Limit Exceeded

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

using namespace std;

typedef pair<int, int> pii;

bool used[1001];
int n;
vector<int> G[1001];
pii costs[1001];
int idx = 0;
map<string, int> indices;
int nxt[1001];
int in[1001];
int sumCosts = 0;
bool used2[1001];

// üü0Ìm[hÉÂ¢ÄTõ(ÂHÍ¶Ýµ¦È¢)
void search1(int s) {
  used[s] = true;
  for (int i = 0; i < (int)G[s].size(); i++) {
    int to = G[s][i];
    if (!used[to]) {
      nxt[s] = to;
      sumCosts += costs[to].second;
      search1(to);
    }
  }
}
// ü_ª ém[h©çTõðnßé
void search2(int s) {
  used[s] = true;
  used2[s] = true;
  for (int i = 0; i < (int)G[s].size(); i++) {
    int to = G[s][i];
    if (!used[to]) {
      nxt[s] = to;
      sumCosts += costs[to].second;
      search2(to);
    }
    // ÂHð­©µ½êAÂHÌÅÅàRXg·Ì¬³¢àÌðÌp
    else {
      // ¡ñÊßµ½êÅÍÈ¢ê
      if (!used2[to])
        continue;
      vector<pii> v;
      int cur = to;
      while (1) {
        v.push_back(pii(costs[cur].first - costs[cur].second, cur));
        if (cur == s)
          break;
        int tto = nxt[cur];
        cur = tto;
      }
      sort(v.begin(), v.end());
      sumCosts += costs[v[0].second].first;
      sumCosts -= costs[v[0].second].second;
    }
  }
}

int main() {

  while (cin >> n && n) {
    sumCosts = 0;
    memset(in, 0, sizeof(in));
    memset(used, 0, sizeof(used));
    indices.clear();
    idx = 0;
    for (int i = 0; i < 1001; i++)
      G[i].clear();
    for (int i = 0; i < n; i++) {
      string a, b;
      int c, d;
      cin >> a >> c >> b >> d;
      if (indices.find(a) == indices.end())
        indices[a] = idx++;
      if (indices.find(b) == indices.end())
        indices[b] = idx++;
      if (a != b) {
        G[indices[b]].push_back(indices[a]);
        in[indices[a]]++;
      }
      costs[indices[a]] = pii(c, d);
    }
    // ü_ª³¢¨ðTõ
    for (int i = 0; i < idx; i++) {
      if (in[i] == 0 && !used[i]) {
        // ÅÌvfÍK¸æé
        sumCosts += costs[i].first;
        search1(i);
      }
    }
    // Ü¾TõµÄ¢È¢àÌð·×ÄTõ
    for (int i = 0; i < idx; i++) {
      if (!used[i]) {
        memset(used2, 0, sizeof(used2));
        sumCosts += costs[i].second;
        search2(i);
      }
    }
    cout << sumCosts << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

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

using namespace std;

typedef pair<int, int> pii;

bool used[1001];
int n;
vector<int> G[1001];
pii costs[1001];
int idx = 0;
map<string, int> indices;
int nxt[1001];
int in[1001];
int sumCosts = 0;

void search1(int s) {
  used[s] = true;
  for (int i = 0; i < G[s].size(); i++) {
    int to = G[s][i];
    if (!used[to]) {
      nxt[s] = to;
      sumCosts += costs[to].second;
      search1(to);
    } else {
      // ÂHð­©µ½êAn_ÍmèÏÝÈÌÅAÁÉ½àsíÈ¢
    }
  }
}
// ü_ª ém[h©çTõðnßé
void search2(int s) {
  used[s] = true;
  for (int i = 0; i < G[s].size(); i++) {
    int to = G[s][i];
    if (!used[to]) {
      nxt[s] = to;
      sumCosts += costs[to].second;
      search2(to);
    }
    // ÂHð­©µ½êAÂHÌÅÅàRXg·Ì¬³¢àÌðÌp
    else {
      vector<pii> v;
      int cur = to;
      while (1) {
        v.push_back(pii(costs[cur].first - costs[cur].second, cur));
        if (cur == s)
          break;
        int tto = nxt[cur];
        cur = tto;
      }
      sort(v.begin(), v.end());
      sumCosts += costs[v[0].second].first;
      sumCosts -= costs[v[0].second].second;
    }
  }
}

int main() {

  while (cin >> n && n) {
    sumCosts = 0;
    memset(in, 0, sizeof(in));
    memset(used, 0, sizeof(used));
    indices.clear();
    idx = 0;
    for (int i = 0; i < 1001; i++)
      G[i].clear();
    for (int i = 0; i < n; i++) {
      string a, b;
      int c, d;
      cin >> a >> c >> b >> d;
      if (indices.find(a) == indices.end())
        indices[a] = idx++;
      if (indices.find(b) == indices.end())
        indices[b] = idx++;
      if (a != b) {
        G[indices[b]].push_back(indices[a]);
        in[indices[a]]++;
      }
      costs[indices[a]] = pii(c, d);
    }
    // ü_ª³¢¨ðTõ
    for (int i = 0; i < idx; i++) {
      if (in[i] == 0 && !used[i]) {
        // ÅÌvfÍK¸æé
        sumCosts += costs[i].first;
        search1(i);
      }
    }
    // Ü¾TõµÄ¢È¢àÌð·×ÄTõ
    for (int i = 0; i < idx; i++) {
      if (!used[i]) {
        sumCosts += costs[i].second;
        search2(i);
      }
    }
    cout << sumCosts << endl;
  }

  return 0;
}