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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define SQ(x) ((x) * (x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define INF (1 << 14)
#define EPS 1e-10
#define MOD 1000000007

typedef pair<int, int> pi;
typedef pair<int, pi> P;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

struct Edge {
  int to, d;
  Edge(int to, int d) : to(to), d(d) {}
};

int N, M, C, V;
vector<Edge> G[6001];
string city[6001];
char gs[6001];
short dist[2001][6001];
map<string, int> recity;

void dijkstra(int s) {
  rep(i, C + 1) fill(dist[i], dist[i] + V, INF);
  dist[C][s] = 0;
  priority_queue<P, vector<P>, greater<P>> q;
  q.push(MP(0, MP(C, s)));

  while (!q.empty()) {
    P p = q.top();
    q.pop();
    short cap = p.second.first, v = p.second.second, cost = p.first;

    if (dist[cap][v] < cost)
      continue;
    rep(i, G[v].size()) {
      Edge e = G[v][i];
      if (e.d > cap)
        continue;
      short tar = max(cap - e.d, gs[e.to] * C);
      if (dist[tar][e.to] > dist[cap][v] + e.d) {
        dist[tar][e.to] = dist[cap][v] + e.d;
        q.push(MP(dist[tar][e.to], MP(tar, e.to)));
      }
    }
  }
}

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

    rep(i, 3005) G[i].clear();
    memset(gs, 0, sizeof gs);
    recity.clear();
    C *= 10;
    V = 0;
    string src, dest;
    cin >> src >> dest;

    rep(i, N) {
      string s1, s2;
      short d, u, v;
      cin >> s1 >> s2 >> d;
      if (recity.find(s1) == recity.end()) {
        recity[s1] = V;
        city[V++] = s1;
      }
      if (recity.find(s2) == recity.end()) {
        recity[s2] = V;
        city[V++] = s2;
      }
      u = recity[s1];
      v = recity[s2];
      G[u].push_back(Edge(v, d));
      G[v].push_back(Edge(u, d));
    }

    rep(i, M) {
      string s;
      cin >> s;
      gs[recity[s]] = 1;
    }

    dijkstra(recity[src]);

    short res = INF;
    rep(i, C + 1) res = min(res, dist[i][recity[dest]]);
    cout << (res == INF ? -1 : res) << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define SQ(x) ((x) * (x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define INF (1 << 29)
#define EPS 1e-10
#define MOD 1000000007

typedef pair<int, int> pi;
typedef pair<int, pi> P;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

struct Edge {
  int to, d;
  Edge(int to, int d) : to(to), d(d) {}
};

int N, M, C, V;
vector<Edge> G[6001];
string city[6001];
char gs[6001];
int dist[2001][6001];
map<string, int> recity;

void dijkstra(int s) {
  rep(i, C + 1) fill(dist[i], dist[i] + V, INF);
  dist[C][s] = 0;
  priority_queue<P, vector<P>, greater<P>> q;
  q.push(MP(0, MP(C, s)));

  while (!q.empty()) {
    P p = q.top();
    q.pop();
    int cap = p.second.first, v = p.second.second, cost = p.first;

    if (dist[cap][v] < cost)
      continue;
    rep(i, G[v].size()) {
      Edge e = G[v][i];
      if (e.d > cap)
        continue;
      int tar = max(cap - e.d, gs[e.to] * C);
      if (dist[tar][e.to] > dist[cap][v] + e.d) {
        dist[tar][e.to] = dist[cap][v] + e.d;
        q.push(MP(dist[tar][e.to], MP(tar, e.to)));
      }
    }
  }
}

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

    rep(i, 3005) G[i].clear();
    memset(gs, 0, sizeof gs);
    recity.clear();
    C *= 10;
    V = 0;
    string src, dest;
    cin >> src >> dest;

    rep(i, N) {
      string s1, s2;
      int d, u, v;
      cin >> s1 >> s2 >> d;
      if (recity.find(s1) == recity.end()) {
        recity[s1] = V;
        city[V++] = s1;
      }
      if (recity.find(s2) == recity.end()) {
        recity[s2] = V;
        city[V++] = s2;
      }
      u = recity[s1];
      v = recity[s2];
      G[u].push_back(Edge(v, d));
      G[v].push_back(Edge(u, d));
    }

    rep(i, M) {
      string s;
      cin >> s;
      gs[recity[s]] = 1;
    }

    dijkstra(recity[src]);

    int res = INF;
    rep(i, C + 1) res = min(res, dist[i][recity[dest]]);
    cout << (res == INF ? -1 : res) << endl;
  }
  return 0;
}