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

#include <iostream>
#include <map>
#include <queue>
#include <set>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

bool used[6000][2001];
vector<pair<short, short>> edge[6000];

int main() {
  while (true) {
    rep(i, 6000) edge[i].clear();
    rep(i, 6000) rep(j, 2001) used[i][j] = false;
    int n, m, cap;
    string src, dest;
    cin >> n >> m >> cap;
    cap *= 10;
    if (n == 0)
      break;
    cin >> src >> dest;
    map<string, int> mp;
    rep(i, n) {
      string c1, c2;
      int d;
      cin >> c1 >> c2 >> d;
      short nc1, nc2;
      if (mp.count(c1)) {
        nc1 = mp[c1];
      } else {
        nc1 = mp.size();
        mp[c1] = nc1;
      }
      if (mp.count(c2)) {
        nc2 = mp[c2];
      } else {
        nc2 = mp.size();
        mp[c2] = nc2;
      }
      edge[nc1].push_back(make_pair(nc2, d));
      edge[nc2].push_back(make_pair(nc1, d));
    }
    set<int> st;
    rep(i, m) {
      string s;
      cin >> s;
      st.insert(mp[s]);
    }
    priority_queue<pair<int, pair<short, short>>,
                   vector<pair<int, pair<short, short>>>,
                   greater<pair<int, pair<short, short>>>>
        que;
    que.push(make_pair(0, make_pair(mp[src], cap)));
    int res = 1e9;
    while (!que.empty()) {
      pair<int, pair<short, short>> now = que.top();
      que.pop();
      int cost = now.first;
      short pos = now.second.first;
      short f = now.second.second;
      if (used[pos][f])
        continue;
      used[pos][f] = true;
      if (mp[dest] == pos) {
        res = cost;
        break;
      }
      rep(i, edge[pos].size()) {
        int nc = cost + edge[pos][i].second;
        short np = edge[pos][i].first;
        short nf = f - edge[pos][i].second;
        if (nf < 0)
          continue;
        if (st.count(np))
          nf = cap;
        if (used[np][nf])
          continue;
        que.push(make_pair(nc, make_pair(np, nf)));
      }
    }
    if (res == 1e9)
      cout << -1 << endl;
    else
      cout << res << endl;
  }
}

Error: MLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <set>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

int dis[6000][2001];
vector<pair<short, short>> edge[6000];

int main() {
  while (true) {
    rep(i, 6000) edge[i].clear();
    rep(i, 6000) rep(j, 2001) dis[i][j] = 1e9;
    int n, m, cap;
    string src, dest;
    cin >> n >> m >> cap;
    cap *= 10;
    if (n == 0)
      break;
    cin >> src >> dest;
    map<string, int> mp;
    rep(i, n) {
      string c1, c2;
      int d;
      cin >> c1 >> c2 >> d;
      short nc1, nc2;
      if (mp.count(c1)) {
        nc1 = mp[c1];
      } else {
        nc1 = mp.size();
        mp[c1] = nc1;
      }
      if (mp.count(c2)) {
        nc2 = mp[c2];
      } else {
        nc2 = mp.size();
        mp[c2] = nc2;
      }
      edge[nc1].push_back(make_pair(nc2, d));
      edge[nc2].push_back(make_pair(nc1, d));
    }
    set<int> st;
    rep(i, m) {
      string s;
      cin >> s;
      st.insert(mp[s]);
    }
    priority_queue<pair<int, pair<short, short>>,
                   vector<pair<int, pair<short, short>>>,
                   greater<pair<int, pair<short, short>>>>
        que;
    que.push(make_pair(0, make_pair(mp[src], cap)));
    while (!que.empty()) {
      pair<int, pair<short, short>> now = que.top();
      que.pop();
      int cost = now.first;
      short pos = now.second.first;
      short f = now.second.second;
      if (dis[pos][f] < cost)
        continue;
      if (mp[dest] == pos)
        break;
      rep(i, edge[pos].size()) {
        int nc = cost + edge[pos][i].second;
        short np = edge[pos][i].first;
        short nf = f - edge[pos][i].second;
        if (nf < 0)
          continue;
        if (st.count(np))
          nf = cap;
        if (dis[np][nf] <= nc)
          continue;
        dis[np][nf] = nc;
        if (np == mp[dest])
          break;
        que.push(make_pair(nc, make_pair(np, nf)));
      }
    }
    int res = 1e9;
    rep(i, 2001) res = min(res, dis[mp[dest]][i]);
    if (res == 1e9)
      cout << -1 << endl;
    else
      cout << res << endl;
  }
}