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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
typedef long long ll;

int dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0};

using namespace std;
struct Edge {
  int src, dst;
  int weight;
  Edge(int s, int d, int w) : src(s), dst(d), weight(w) {}
  Edge() {}
};

using Graph = vector<vector<Edge>>;

int V, E;
int num_stand; // num of stands
string src, dst;
int cap;
Graph g;

vector<string> c1, c2;
vector<int> d;
vector<string> s;

map<string, int> m;

const int inf = 1e9;

int dist[4010][2100]; // at town i with gas j
int canCharge[6010];

int dijkstra(int s, int t) {
  fill((int *)begin(dist), (int *)end(dist), inf);
  priority_queue<tuple<int, int, int>> q; // -dist, town, cap

  dist[s][cap] = 0;
  q.emplace(0, s, cap);

  while (q.size()) {
    int d, v, rem;
    tie(d, v, rem) = q.top();
    q.pop();
    d = -d;

    if (v == t)
      return d;

    if (dist[v][rem] < d)
      continue;
    for (auto &e : g[v]) {
      int nv = e.dst;
      int nrem = rem - e.weight;
      if (nrem < 0)
        continue;
      if (canCharge[nv]) {
        nrem = cap;
      }
      int nd = d + e.weight;
      if (dist[nv][nrem] > nd) {
        dist[nv][nrem] = nd;
        q.emplace(-nd, nv, nrem);
      }
    }
  }

  return 1e9;
}

int main() {
  while (cin >> E >> num_stand >> cap && E) {
    m.clear();
    V = 0;
    c1.resize(E);
    c2.resize(E);
    d.resize(E);
    cap *= 10;
    cin >> src >> dst;
    if (m.count(src) == 0)
      m[src] = V++;
    if (m.count(dst) == 0)
      m[dst] = V++;

    for (int i = 0; i < E; ++i) {
      cin >> c1[i] >> c2[i] >> d[i];
      if (m.count(c1[i]) == 0)
        m[c1[i]] = V++;
      if (m.count(c2[i]) == 0)
        m[c2[i]] = V++;
    }

    memset(canCharge, 0, sizeof(canCharge));
    s.resize(num_stand);
    for (int i = 0; i < num_stand; ++i) {
      cin >> s[i];
      canCharge[m[s[i]]] = true;
    }

    g.assign(V, {});
    for (int i = 0; i < E; ++i) {
      int a = m[c1[i]];
      int b = m[c2[i]];
      g[a].emplace_back(a, b, d[i]);
      g[b].emplace_back(b, a, d[i]);
    }

    int ans = dijkstra(m[src], m[dst]);
    if (ans == inf)
      ans = -1;
    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
typedef long long ll;

int dx[] = {0, 1, 0, -1}, dy[] = {-1, 0, 1, 0};

using namespace std;
struct Edge {
  int src, dst;
  int weight;
  Edge(int s, int d, int w) : src(s), dst(d), weight(w) {}
  Edge() {}
};

using Graph = vector<vector<Edge>>;

int V, E;
int num_stand; // num of stands
string src, dst;
int cap;
Graph g;

vector<string> c1, c2;
vector<int> d;
vector<string> s;

map<string, int> m;

const int inf = 1e9;

int dist[6010][2100]; // at town i with gas j
int canCharge[6010];

int dijkstra(int s, int t) {
  fill((int *)begin(dist), (int *)end(dist), inf);
  priority_queue<tuple<int, int, int>> q; // -dist, town, cap

  dist[s][cap] = 0;
  q.emplace(0, s, cap);

  while (q.size()) {
    int d, v, rem;
    tie(d, v, rem) = q.top();
    q.pop();
    d = -d;

    if (v == t)
      return d;

    if (dist[v][rem] < d)
      continue;
    for (auto &e : g[v]) {
      int nv = e.dst;
      int nrem = rem - e.weight;
      if (nrem < 0)
        continue;
      if (canCharge[nv]) {
        nrem = cap;
      }
      int nd = d + e.weight;
      if (dist[nv][nrem] > nd) {
        dist[nv][nrem] = nd;
        q.emplace(-nd, nv, nrem);
      }
    }
  }

  return 1e9;
}

int main() {
  while (cin >> E >> num_stand >> cap && E) {
    m.clear();
    V = 0;
    c1.resize(E);
    c2.resize(E);
    d.resize(E);
    cap *= 10;
    cin >> src >> dst;
    if (m.count(src) == 0)
      m[src] = V++;
    if (m.count(dst) == 0)
      m[dst] = V++;

    for (int i = 0; i < E; ++i) {
      cin >> c1[i] >> c2[i] >> d[i];
      if (m.count(c1[i]) == 0)
        m[c1[i]] = V++;
      if (m.count(c2[i]) == 0)
        m[c2[i]] = V++;
    }

    memset(canCharge, 0, sizeof(canCharge));
    s.resize(num_stand);
    for (int i = 0; i < num_stand; ++i) {
      cin >> s[i];
      canCharge[m[s[i]]] = true;
    }

    g.assign(V, {});
    for (int i = 0; i < E; ++i) {
      int a = m[c1[i]];
      int b = m[c2[i]];
      g[a].emplace_back(a, b, d[i]);
      g[b].emplace_back(b, a, d[i]);
    }

    int ans = dijkstra(m[src], m[dst]);
    if (ans == inf)
      ans = -1;
    cout << ans << endl;
  }
}