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

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

const int INF = 0x3f3f3f3f;
typedef long long ll;

int d[6010][2010];
bool done[6010][2010];
int N, M, C;

typedef struct _Edge {
  int to, dist;
  _Edge(int to, int dist) : to(to), dist(dist){};
} Edge;

typedef struct _Node {
  int v, gas, d;
  _Node(int v, int gas, int d) : v(v), gas(gas), d(d){};
  bool operator<(const _Node &n) const { return d > n.d; }
} Node;

int dijkstra(vector<vector<Edge>> &G, set<int> &gass, int src, int dst) {
  int N = G.size();
  memset(d, 0x3f, sizeof(d));
  memset(done, 0, sizeof(done));

  priority_queue<Node> que;

  d[src][C * 10] = 0;
  que.push(Node(src, C * 10, 0));

  while (!que.empty()) {
    auto node = que.top();
    que.pop();
    if (node.v == dst)
      return node.d;

    auto p = make_pair(node.v, node.gas);

    if (done[p.first][p.second])
      continue;
    done[p.first][p.second] = true;

    for (auto e : G[node.v]) {
      if (node.gas < e.dist)
        continue;

      pair<int, int> np;
      np.first = e.to;
      if (gass.find(e.to) != gass.end())
        np.second = C * 10;
      else
        np.second = node.gas - e.dist;

      if (d[p.first][p.second] + e.dist < d[np.first][np.second]) {
        d[np.first][np.second] = d[p.first][p.second] + e.dist;
        que.push(Node(np.first, np.second, d[np.first][np.second]));
      }
    }
  }
  return -1;
}

void solve() {
  string src, dst;
  cin >> src >> dst;

  map<string, int> stoi;
  vector<string> cs(N), cd(N);
  vector<int> ds(N);
  for (int i = 0; i < N; i++) {
    cin >> cs[i] >> cd[i] >> ds[i];

    if (stoi.find(cs[i]) == stoi.end()) {
      int x = stoi.size();
      stoi[cs[i]] = x;
    }
    if (stoi.find(cd[i]) == stoi.end()) {
      int x = stoi.size();
      stoi[cd[i]] = x;
    }
  }

  set<int> gass;
  for (int i = 0; i < M; i++) {
    string s;
    cin >> s;
    gass.insert(stoi[s]);
  }

  vector<vector<Edge>> G(stoi.size());
  for (int i = 0; i < N; i++) {
    int si = stoi[cs[i]];
    int di = stoi[cd[i]];

    G[si].emplace_back(di, ds[i]);
    G[di].emplace_back(si, ds[i]);
  }

  cout << dijkstra(G, gass, stoi[src], stoi[dst]) << endl;
}

int main() {
  while (cin >> N >> M >> C, N | M | C)
    solve();

  return 0;
}

Error: MLE

Fixed submission:

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

const int INF = 0x3f3f3f3f;
typedef long long ll;

int N, M, C;

typedef struct _Edge {
  int to, dist;
  _Edge(int to, int dist) : to(to), dist(dist){};
} Edge;

typedef struct _Node {
  int v, gas, d;
  _Node(int v, int gas, int d) : v(v), gas(gas), d(d){};
  bool operator<(const _Node &n) const { return d > n.d; }
} Node;

int dijkstra(vector<vector<Edge>> &G, set<int> &gass, int src, int dst) {
  int N = G.size();
  map<pair<int, int>, int> d;
  set<pair<int, int>> done; // v, gas

  priority_queue<Node> que;

  d[{src, C * 10}] = 0;
  que.push(Node(src, C * 10, 0));

  while (!que.empty()) {
    auto node = que.top();
    que.pop();
    if (node.v == dst)
      return node.d;

    auto p = make_pair(node.v, node.gas);

    if (done.find(p) != done.end())
      continue;
    done.insert(p);

    for (auto e : G[node.v]) {
      if (node.gas < e.dist)
        continue;

      pair<int, int> np;
      np.first = e.to;
      if (gass.find(e.to) != gass.end())
        np.second = C * 10;
      else
        np.second = node.gas - e.dist;

      if (d.find(np) == d.end() || d[p] + e.dist < d[np]) {
        d[np] = d[p] + e.dist;
        que.push(Node(np.first, np.second, d[np]));
      }
    }
  }
  return -1;
}

void solve() {
  string src, dst;
  cin >> src >> dst;

  map<string, int> stoi;
  vector<string> cs(N), cd(N);
  vector<int> ds(N);
  for (int i = 0; i < N; i++) {
    cin >> cs[i] >> cd[i] >> ds[i];

    if (stoi.find(cs[i]) == stoi.end()) {
      int x = stoi.size();
      stoi[cs[i]] = x;
    }
    if (stoi.find(cd[i]) == stoi.end()) {
      int x = stoi.size();
      stoi[cd[i]] = x;
    }
  }

  set<int> gass;
  for (int i = 0; i < M; i++) {
    string s;
    cin >> s;
    gass.insert(stoi[s]);
  }

  vector<vector<Edge>> G(stoi.size());
  for (int i = 0; i < N; i++) {
    int si = stoi[cs[i]];
    int di = stoi[cd[i]];

    G[si].emplace_back(di, ds[i]);
    G[di].emplace_back(si, ds[i]);
  }

  cout << dijkstra(G, gass, stoi[src], stoi[dst]) << endl;
}

int main() {
  while (cin >> N >> M >> C, N | M | C)
    solve();

  return 0;
}