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

#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

int tot;
map<string, int> mp;
string s, s1, s2;
vector<int> station;
const int maxn = 10010;
const int maxm = 10010;
const int inf = 1000000000;
bool vis[maxn];
int que[maxn];
int d[maxn], d2[maxn];
bool use[maxn];
int n, m, C;
struct EE {
  int to, ne, w;
} e[maxm * 2];
int L, head[maxn];
void adde(int u, int v, int w) {
  // cout<<u<<' '<<v<<' '<<w<<endl;
  e[L].to = v;
  e[L].w = w;
  e[L].ne = head[u];
  head[u] = L++;
}
void spfa(int t) {
  int l, r;
  l = r = 0;
  for (int i = 1; i <= tot; i++)
    vis[i] = false;
  que[r++] = t;
  vis[t] = true;
  while (l != r) {
    int u = que[l++];
    vis[u] = false;
    l %= maxn;
    for (int i = head[u]; i != -1; i = e[i].ne) {
      int v = e[i].to;
      if (d2[v] > d2[u] + e[i].w) {
        d2[v] = d2[u] + e[i].w;
        if (!vis[v]) {
          que[r++] = v;
          r %= maxn;
          vis[v] = true;
        }
      }
    }
  }
}
void work() {
  for (int i = 1; i <= tot; i++) {
    d2[i] = inf;
  }
  int t, tp;
  tp = inf;
  for (int j = 0; j < station.size(); j++)
    if (!use[j] && d[station[j]] < tp) {
      t = j;
      tp = d[station[j]];
    }
  // cout<<d[station[0]]<<endl;
  if (tp == inf)
    return;
  // cout<<t<<endl;
  use[t] = true;
  t = station[t];
  d2[t] = 0;
  spfa(t);
  // cout<<C<<endl;
  // for (int j = 1; j <= tot; j++)
  //    cout<<j<<' '<<d2[j]<<endl;
  // cout<<t<<endl;
  for (int j = 1; j <= tot; j++)
    if (d2[j] <= C * 10)
      d[j] = min(d[j], d2[j] + d[t]);
}
int main() {
  while (scanf("%d%d%d", &n, &m, &C) != EOF) {
    if (n == 0 && m == 0)
      break;
    L = 0;
    memset(head, -1, sizeof(head));
    mp.clear();
    tot = 0;
    cin >> s1 >> s2;
    mp[s1] = ++tot;
    mp[s2] = ++tot;
    for (int i = 0; i < n; i++) {
      cin >> s1 >> s2;
      if (mp.find(s1) == mp.end()) {
        mp[s1] = ++tot;
      }
      if (mp.find(s2) == mp.end()) {
        mp[s2] = ++tot;
      }
      int d;
      scanf("%d", &d);
      adde(mp[s1], mp[s2], d);
      adde(mp[s2], mp[s1], d);
    }
    station.clear();
    station.push_back(1);
    for (int i = 0; i < m; i++) {
      cin >> s1;
      station.push_back(mp[s1]);
    }
    for (int i = 0; i <= tot; i++)
      d[i] = inf;
    d[1] = 0;
    for (int i = 0; i < station.size(); i++)
      use[i] = false;
    for (int i = 0; i <= m; i++) {
      work();
    }
    if (d[2] == inf)
      printf("-1\\n");
    else
      printf("%d\\n", d[2]);
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
#define inf 999999999
using namespace std;
struct xxx {
  int y, c;
};
struct yyy {
  int num, oil;
};
int n, m, cap, tot;
int dis[6001][2001];
bool f[6001], vi[6001][2001];
string st, en;
map<string, int> mp1;
vector<xxx> edge[6001];
queue<yyy> q;
void spfa() {
  while (!q.empty())
    q.pop();
  for (int i = 1; i <= tot; i++)
    for (int j = 0; j <= cap * 10; j++) {
      dis[i][j] = inf;
      vi[i][j] = 0;
    }
  dis[1][cap * 10] = 0;
  yyy start;
  start.num = 1;
  start.oil = cap * 10;
  q.push(start);
  vi[1][cap * 10] = 1;
  while (!q.empty()) {
    yyy now = q.front();
    q.pop();
    for (int i = 0; i < edge[now.num].size(); i++) {
      int y = edge[now.num][i].y;
      if (now.oil >= edge[now.num][i].c) {
        if (dis[y][now.oil - edge[now.num][i].c] >
            dis[now.num][now.oil] + edge[now.num][i].c) {
          if (f[y]) {
            dis[y][cap * 10] = dis[now.num][now.oil] + edge[now.num][i].c;
            if (!vi[y][cap * 10]) {
              vi[y][cap * 10] = 1;
              yyy news;
              news.num = y;
              news.oil = cap * 10;
              q.push(news);
            }
          } else {
            dis[y][now.oil - edge[now.num][i].c] =
                dis[now.num][now.oil] + edge[now.num][i].c;
            if (!vi[y][now.oil - edge[now.num][i].c]) {
              vi[y][now.oil - edge[now.num][i].c] = 1;
              yyy news;
              news.num = y;
              news.oil = now.oil - edge[now.num][i].c;
              q.push(news);
            }
          }
        }
      }
    }
  }
}
int main() {
  while (~scanf("%d%d%d", &n, &m, &cap)) {
    if (n == 0 && m == 0 && cap == 0)
      break;
    tot = 0;
    cin >> st >> en;
    mp1[st] = ++tot;
    mp1[en] = ++tot;
    for (int i = 1; i <= n; i++) {
      string s1, s2;
      int len;
      cin >> s1 >> s2 >> len;
      if (!mp1[s1])
        mp1[s1] = ++tot;
      if (!mp1[s2])
        mp1[s2] = ++tot;
      int n1, n2;
      n1 = mp1[s1];
      n2 = mp1[s2];
      xxx pp;
      pp.c = len;
      pp.y = n2;
      edge[n1].push_back(pp);
      pp.y = n1;
      edge[n2].push_back(pp);
    }
    memset(f, 0, sizeof(f));
    for (int i = 1; i <= m; i++) {
      string ss;
      cin >> ss;
      if (!mp1[ss])
        mp1[ss] = ++tot;
      f[mp1[ss]] = 1;
    }
    spfa();
    int ans = inf;
    for (int i = 0; i <= cap * 10; i++)
      ans = min(ans, dis[2][i]);
    if (ans < inf)
      printf("%d\\n", ans);
    else
      printf("-1\\n");
  }
  return 0;
}