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

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toStr(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, (n)-1)
const double EPS = 1e-12;
const double PI = acos(-1.0);
const int INF = INT_MAX / 10;

struct state {
  int p, c, g;
  state(int p, int c, int g) : p(p), c(c), g(g){};
  bool operator<(const state &o) const { return o.c < c; }
};

int main() {
  int N, M, cap;
  while (cin >> N >> M >> cap, N) {
    string src, dest;
    cin >> src >> dest;
    int size = 0;
    vector<vs> data(N, vs(3));
    map<string, int> index;
    {
      set<string> cities;
      REP(i, N) {
        REP(j, 2) {
          cin >> data[i][j];
          cities.insert(data[i][j]);
        }
        cin >> data[i][2];
      }

      set<string>::iterator it = cities.begin();
      while (it != cities.end()) {
        index[*it] = size;
        size++;
        it++;
      }
    }

    vector<vector<pii>> dist(size);
    REP(i, N) {
      int c1 = index[data[i][0]];
      int c2 = index[data[i][1]];
      int cost = toInt(data[i][2]);

      dist[c1].push_back(make_pair(c2, cost));
      dist[c2].push_back(make_pair(c1, cost));
    }

    vi gas(size);
    string s;
    REP(i, M) {
      cin >> s;
      gas[index[s]] = 1;
    }

    vvi cost(size, vi(cap * 10 + 1, INF));
    priority_queue<state> Q;
    Q.push(state(index[src], 0, cap * 10));
    cost[index[src]][cap * 10] = 0;
    while (!Q.empty()) {
      state st = Q.top();
      Q.pop();

      if (st.p == index[dest]) {
        cout << st.c << endl;
        goto end;
      }

      REP(d, dist[st.p].size()) {
        int np = dist[st.p][d].first;
        int nc = st.c + dist[st.p][d].second;
        int ng = st.g - dist[st.p][d].second;

        if (ng < 0) {
          continue;
        }
        if (gas[np]) {
          ng = cap * 10;
        }

        if (nc < cost[np][ng]) {
          cost[np][ng] = nc;
          Q.push(state(np, nc, ng));
        }
      }
    }

    cout << -1 << endl;
  end:;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toStr(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define FOR(i, a, b) for (int i = (a); i <= (b); ++i)
#define REP(i, n) FOR(i, 0, (n)-1)
const double EPS = 1e-12;
const double PI = acos(-1.0);
// const int INF = INT_MAX/10;

const int INF = SHRT_MAX / 10;
typedef vector<short> vsh;
typedef vector<vsh> vvs;

struct state {
  int p, c, g;
  state(int p, int c, int g) : p(p), c(c), g(g){};
  bool operator<(const state &o) const { return o.c < c; }
};

int main() {
  int N, M, cap;
  while (cin >> N >> M >> cap, N) {
    string src, dest;
    cin >> src >> dest;
    int size = 0;
    vector<vs> data(N, vs(3));
    map<string, int> index;
    {
      set<string> cities;
      REP(i, N) {
        REP(j, 2) {
          cin >> data[i][j];
          cities.insert(data[i][j]);
        }
        cin >> data[i][2];
      }

      set<string>::iterator it = cities.begin();
      while (it != cities.end()) {
        index[*it] = size;
        size++;
        it++;
      }
    }

    vvs dist(size, vsh(size, INF));
    REP(i, size) {
      REP(j, size) { dist[i][j] = INF; }
    }
    REP(i, N) {
      string c1 = data[i][0];
      string c2 = data[i][1];
      int cost = toInt(data[i][2]);
      dist[index[c1]][index[c2]] = dist[index[c2]][index[c1]] = cost;
    }

    vi gas(size);
    string s;
    REP(i, M) {
      cin >> s;
      gas[index[s]] = 1;
    }

    vsh cost(size, INF);
    priority_queue<state> Q;
    Q.push(state(index[src], 0, cap * 10));
    cost[index[src]] = 0;
    while (!Q.empty()) {
      state st = Q.top();
      Q.pop();

      if (st.p == index[dest]) {
        cout << st.c << endl;
        goto end;
      }

      REP(d, size) {
        if (st.p != d && dist[st.p][d] != INF) {
          int nc = st.c + dist[st.p][d];
          int ng = st.g - dist[st.p][d];
          if (ng < 0) {
            continue;
          }
          if (gas[d]) {
            ng = cap * 10;
          }

          if (nc < cost[d]) {
            cost[d] = nc;
            Q.push(state(d, nc, ng));
          }
        }
      }
    }

    cout << -1 << endl;
  end:;
  }

  return 0;
}