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

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

const int V = 6010;
const int E = 3010;
int n, m, cap;

struct dot {
  int p, oil;
} front, rear;

struct edge {
  int to, w;
};

vector<edge> G[V];
char s1[20], s2[20];

void add_edge(int u, int v, int d) {
  edge ee;
  ee.to = v;
  ee.w = d;
  G[u].push_back(ee);
  ee.to = u;
  G[v].push_back(ee);
}

bool sta[V];
int dis[V][2010];
// bool inq[V][2010];

int main() { // freopen("in.txt", "r", stdin);
  while (~scanf("%d%d%d", &n, &m, &cap)) {
    if (!n && !m && !cap)
      break;
    map<string, int> city;
    cap *= 10;
    scanf("%s %s", s1, s2);
    city[string(s1)] = 1;
    city[string(s2)] = 2;
    for (int i = 0; i < V; i++)
      G[i].clear();
    int u, v, d, cou = 2;
    for (int i = 0; i < n; i++) {
      scanf("%s %s %d", s1, s2, &d);
      if (!city[string(s1)])
        city[string(s1)] = ++cou;
      if (!city[string(s2)])
        city[string(s2)] = ++cou;
      u = city[string(s1)];
      v = city[string(s2)];
      add_edge(u, v, d);
    }
    memset(sta, 0, sizeof(sta));
    for (int i = 0; i < m; i++) {
      scanf("%s", s1);
      if (!city[string(s1)])
        continue;
      else
        sta[city[string(s1)]] = 1;
    }
    queue<dot> q;
    front.oil = cap;
    front.p = 1;
    q.push(front);
    int mmin = 0x3f3f3f3f;
    // memset(inq, 0, sizeof(inq));
    memset(dis, 0x3f, sizeof(dis));
    dis[1][cap] = 0;
    int ok = 0;
    while (!q.empty()) {
      front = q.front();
      q.pop();
      //	inq[front.p][front.oil] = false;
      if (front.p == 2) {
        ok = 1;
        if (dis[2][front.oil] < mmin)
          mmin = dis[2][front.oil];
        continue;
      }
      for (int i = 0; i < G[front.p].size(); i++) {
        if (G[front.p][i].w > front.oil)
          continue;
        rear.oil = front.oil - G[front.p][i].w;
        rear.p = G[front.p][i].to;
        if (sta[rear.p])
          rear.oil = cap;
        if (dis[rear.p][rear.oil] > dis[front.p][front.oil] + G[front.p][i].w) {
          dis[rear.p][rear.oil] = dis[front.p][front.oil] + G[front.p][i].w;
          // if(!inq[rear.p][rear.oil]){
          //	inq[rear.p][rear.oil] = 1;
          q.push(rear);
          //}
        }
      }
    }
    if (!ok)
      puts("-1");
    else
      printf("%d\\n", mmin);
  }
}

Error: MLE

Fixed submission:

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

const int V = 6010;
const int E = 3010;
int n, m, cap;

struct dot {
  int p, oil;
} front, rear;

struct edge {
  int to, w;
};

vector<edge> G[V];
char s1[20], s2[20];

void add_edge(int u, int v, int d) {
  edge ee;
  ee.to = v;
  ee.w = d;
  G[u].push_back(ee);
  ee.to = u;
  G[v].push_back(ee);
}

bool sta[V];
int dis[V][2010];
bool inq[V][2010];

int main() { // freopen("in.txt", "r", stdin);
  while (~scanf("%d%d%d", &n, &m, &cap)) {
    if (!n && !m && !cap)
      break;
    map<string, int> city;
    cap *= 10;
    scanf("%s %s", s1, s2);
    city[string(s1)] = 1;
    city[string(s2)] = 2;
    for (int i = 0; i < V; i++)
      G[i].clear();
    int u, v, d, cou = 2;
    for (int i = 0; i < n; i++) {
      scanf("%s %s %d", s1, s2, &d);
      if (!city[string(s1)])
        city[string(s1)] = ++cou;
      if (!city[string(s2)])
        city[string(s2)] = ++cou;
      u = city[string(s1)];
      v = city[string(s2)];
      add_edge(u, v, d);
    }
    memset(sta, 0, sizeof(sta));
    for (int i = 0; i < m; i++) {
      scanf("%s", s1);
      if (!city[string(s1)])
        continue;
      else
        sta[city[string(s1)]] = 1;
    }
    queue<dot> q;
    front.oil = cap;
    front.p = 1;
    q.push(front);
    int mmin = 0x3f3f3f3f;
    memset(inq, 0, sizeof(inq));
    memset(dis, 0x3f, sizeof(dis));
    dis[1][cap] = 0;
    int ok = 0;
    while (!q.empty()) {
      front = q.front();
      q.pop();
      inq[front.p][front.oil] = false;
      if (front.p == 2) {
        ok = 1;
        if (dis[2][front.oil] < mmin)
          mmin = dis[2][front.oil];
        continue;
      }
      for (int i = 0; i < G[front.p].size(); i++) {
        if (G[front.p][i].w > front.oil)
          continue;
        rear.oil = front.oil - G[front.p][i].w;
        rear.p = G[front.p][i].to;
        if (sta[rear.p])
          rear.oil = cap;
        if (dis[rear.p][rear.oil] > dis[front.p][front.oil] + G[front.p][i].w) {
          dis[rear.p][rear.oil] = dis[front.p][front.oil] + G[front.p][i].w;
          if (!inq[rear.p][rear.oil]) {
            inq[rear.p][rear.oil] = 1;
            q.push(rear);
          }
        }
      }
    }
    if (!ok)
      puts("-1");
    else
      printf("%d\\n", mmin);
  }
}