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

#include <bits/stdc++.h>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }

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 pb push_back
#define mp make_pair
#define each(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define exist(s, e) ((s).find(e) != (s).end())
#define range(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define clr(a, b) memset((a), (b), sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi = acos(-1.0);
const ll INF = 1LL << 62;
const int inf = 1 << 29;

vector<pii> graph[6000];
int graph2[310][310];

int dist[6010];
int dist2[310][2010];

int station[310];

typedef tuple<int, int, int> state;

inline bool update(int c, int v, int g) {
  if (g >= 0 && (dist2[v][g] == -1 || dist2[v][g] > c)) {
    dist2[v][g] = c;
    return true;
  }
  return false;
}

void dijkstra(int s, int m) {
  clr(dist, -1);
  int src = station[s];
  dist[src] = 0;
  priority_queue<state, vector<state>, greater<state>> q;
  state init(0, src, -1);
  q.push(init);
  while (!q.empty()) {
    state cur = q.top();
    q.pop();
    int c, v, g;
    tie(c, v, g) = cur;

    if (dist[v] < c)
      continue;

    for (auto &v2 : graph[v]) {
      int nc = c + v2.second;
      if (dist[v2.first] == -1 || dist[v2.first] > nc) {
        dist[v2.first] = nc;
        state next(nc, v2.first, -1);
        q.push(next);
      }
    }
  }
  rep(i, m) graph2[s][i] = dist[station[i]];
  return;
}

int dijkstra2(int cap, int m) {
  clr(dist2, -1);
  dist2[0][cap] = 0;
  priority_queue<state, vector<state>, greater<state>> q;
  state init(0, 0, cap);
  q.push(init);
  while (!q.empty()) {
    state cur = q.top();
    q.pop();
    int c, v, g;
    tie(c, v, g) = cur;

    if (v == 1)
      return c;

    if (dist2[v][g] < c)
      continue;

    if (v >= 2)
      g = cap;

    for (int i = 0; i < m; ++i) {
      int nc = c + graph2[v][i];
      int ng = g - graph2[v][i];
      if (update(nc, i, ng)) {
        state next(nc, i, ng);
        q.push(next);
      }
    }
  }
  return -1;
}

int main(void) {
  int n, m, cap;
  while (cin >> n >> m >> cap) {
    if (n == 0 && m == 0 && cap == 0)
      break;

    map<string, int> con;

    int nmax = 0;
    string src, dest;
    cin >> src >> dest;
    con[src] = nmax++, con[dest] = nmax++;
    rep(i, n) {
      string a, b;
      cin >> a >> b;
      if (con.find(a) == con.end())
        con[a] = nmax++;
      if (con.find(b) == con.end())
        con[b] = nmax++;
      int aa, bb, c;
      cin >> c;
      aa = con[a], bb = con[b];
      pii in(bb, c);
      graph[aa].pb(in);
      in.first = aa, graph[bb].pb(in);
    }

    m += 2;
    clr(station, -1);
    station[0] = con[src], station[1] = con[dest];
    for (int i = 2; i < m; ++i) {
      string s;
      cin >> s;
      station[i] = con[s];
    }
    rep(i, m) dijkstra(i, m);

    cap *= 10;
    int ans = dijkstra2(cap, m);
    cout << ans << endl;

    rep(i, nmax) graph[i].clear();
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
template <class T> inline T sqr(T x) { return x * x; }

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 pb push_back
#define mp make_pair
#define each(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define exist(s, e) ((s).find(e) != (s).end())
#define range(i, a, b) for (int i = (a); i < (b); ++i)
#define rep(i, n) range(i, 0, n)
#define clr(a, b) memset((a), (b), sizeof(a))
#define dump(x) cerr << #x << " = " << (x) << endl;
#define debug(x)                                                               \\
  cerr << #x << " = " << (x) << " (L" << __LINE__ << ")"                       \\
       << " " << __FILE__ << endl;

const double eps = 1e-10;
const double pi = acos(-1.0);
const ll INF = 1LL << 62;
const int inf = 1 << 29;

vector<pii> graph[6000];
vi dist[6000];
bool station[6000];

typedef tuple<int, int, int> state;

inline bool update(int c, int v, int g) {
  if (g >= 0 && (dist[v][g] == -1 || dist[v][g] > c)) {
    dist[v][g] = c;
    return true;
  }
  return false;
}

int dijkstra(int cap) {
  dist[0][cap] = 0;
  priority_queue<state, vector<state>, greater<state>> q;
  state init(0, 0, cap);
  q.push(init);
  while (!q.empty()) {
    state cur = q.top();
    q.pop();
    int c, v, g;
    tie(c, v, g) = cur;

    if (v == 1)
      return c;

    if (dist[v][g] < c)
      continue;

    if (station[v]) {
      g = cap;
      dist[v][g] = c;
    }
    for (auto &v2 : graph[v]) {
      int nc = c + v2.second;
      int ng = g - v2.second;
      if (update(nc, v2.first, ng)) {
        state next(nc, v2.first, ng);
        q.push(next);
      }
    }
  }
  return -1;
}

int main(void) {
  int n, m, cap;
  while (cin >> n >> m >> cap) {
    map<string, int> con;
    if (n == 0 && m == 0 && cap == 0)
      break;
    int nmax = 0;
    string src, dest;
    cin >> src >> dest;
    con[src] = nmax++, con[dest] = nmax++;
    rep(i, n) {
      string a, b;
      cin >> a >> b;
      if (con.find(a) == con.end())
        con[a] = nmax++;
      if (con.find(b) == con.end())
        con[b] = nmax++;
      int aa, bb, c;
      cin >> c;
      aa = con[a], bb = con[b];
      pii in(bb, c);
      graph[aa].pb(in);
      in.first = aa, graph[bb].pb(in);
    }
    rep(i, m) {
      string s;
      cin >> s;
      station[con[s]] = true;
    }
    cap *= 10;
    rep(i, nmax) dist[i].assign(cap + 1, -1);
    int ans = dijkstra(cap);
    cout << ans << endl;

    rep(i, nmax) graph[i].clear();
    rep(i, nmax) dist[i].clear();
    clr(station, false);
  }
  return 0;
}