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

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

const int ME = 3010;
const int MV = 6010;
int fir[MV], rfir[MV];
int n, m, cap, u[ME], v[ME], nex[ME], rnex[ME], w[ME];
int dis[MV][2010];
bool oil_sta[MV];

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

int main() {
  // freopen("in.txt", "r", stdin);
  while (~scanf("%d%d%d", &n, &m, &cap)) {
    //		printf("%d %d %d\\n", n, m, cap);
    if (!n && !m && !cap)
      break;
    map<string, int> city;
    string start, des;
    cin >> start >> des;
    city[start] = 1;
    city[des] = 2;
    int d;
    string x, y;
    int cou = 2;
    memset(fir, -1, sizeof(fir));
    memset(rfir, -1, sizeof(rfir));
    // int ee = 0;
    for (int ee = 0; ee < n; ee++) {
      cin >> x >> y >> d;
      if (city[x] == 0)
        city[x] = ++cou;
      if (city[y] == 0)
        city[y] = ++cou;
      // if(d > cap * 10 ) continue;
      u[ee] = city[x];
      v[ee] = city[y];
      w[ee] = d;
      nex[ee] = fir[u[ee]];
      fir[u[ee]] = ee;
      rnex[ee] = rfir[v[ee]];
      rfir[v[ee]] = ee;
      // ee++;
    }
    memset(oil_sta, 0, sizeof(oil_sta));
    for (int i = 0; i < m; i++) {
      cin >> x;
      if (!city[x])
        continue;
      oil_sta[city[x]] = 1;
    }
    queue<dot> q;
    front.oil = cap * 10;
    front.p = 1;
    q.push(front);
    int ok = 0;
    memset(dis, 0x3f, sizeof(dis));
    //	memset(inq, 0, sizeof(inq));
    dis[1][cap * 10] = 0;
    //	inq[1][cap*10] = 1;
    int mmin = 0x3f3f3f3f;
    while (!q.empty()) {
      front = q.front();
      q.pop();
      //	inq[front.p][front.oil] = 0;
      if (front.p == 2 && !ok) {
        ok = 1;
        continue;
      }
      for (int e = fir[front.p]; e != -1; e = nex[e]) {
        if (front.oil < w[e])
          continue;
        rear.oil = front.oil - w[e];
        rear.p = v[e];
        if (oil_sta[v[e]])
          rear.oil = cap * 10;
        if (dis[v[e]][rear.oil] > dis[front.p][front.oil] + w[e]) {
          dis[v[e]][rear.oil] = dis[front.p][front.oil] + w[e];
          //		if(!inq[v[e]][rear.oil]){
          //			inq[v[e]][rear.oil] = 1;
          q.push(rear);
          //	}
        }
      }
      for (int e = rfir[front.p]; e != -1; e = rnex[e]) {
        if (front.oil < w[e])
          continue;
        rear.oil = front.oil - w[e];
        rear.p = u[e];
        if (oil_sta[u[e]])
          rear.oil = cap * 10;
        if (dis[u[e]][rear.oil] > dis[front.p][front.oil] + w[e]) {
          dis[u[e]][rear.oil] = dis[front.p][front.oil] + w[e];
          //	if(!inq[u[e]][rear.oil]){
          //	inq[u[e]][rear.oil] = 1;
          q.push(rear);
          //	}
        }
      }
    }
    if (ok) {
      for (int i = 0; i <= cap * 10; i++)
        if (dis[2][i] < mmin)
          mmin = dis[2][i];
      printf("%d\\n", mmin);
    } else
      puts("-1");
  }
}

Error: MLE

Fixed submission:

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

const int ME = 3010;
const int MV = 6010;
int fir[MV], rfir[MV];
int n, m, cap, u[ME], v[ME], nex[ME], rnex[ME], w[ME];
int dis[MV][2010];
bool inq[MV][2010], oil_sta[MV];

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

int main() {
  // freopen("in.txt", "r", stdin);
  while (~scanf("%d%d%d", &n, &m, &cap)) {
    //		printf("%d %d %d\\n", n, m, cap);
    if (!n && !m && !cap)
      break;
    map<string, int> city;
    string start, des;
    cin >> start >> des;
    city[start] = 1;
    city[des] = 2;
    int d;
    string x, y;
    int cou = 2;
    memset(fir, -1, sizeof(fir));
    memset(rfir, -1, sizeof(rfir));
    // int ee = 0;
    for (int ee = 0; ee < n; ee++) {
      cin >> x >> y >> d;
      if (city[x] == 0)
        city[x] = ++cou;
      if (city[y] == 0)
        city[y] = ++cou;
      // if(d > cap * 10 ) continue;
      u[ee] = city[x];
      v[ee] = city[y];
      w[ee] = d;
      nex[ee] = fir[u[ee]];
      fir[u[ee]] = ee;
      rnex[ee] = rfir[v[ee]];
      rfir[v[ee]] = ee;
      // ee++;
    }
    memset(oil_sta, 0, sizeof(oil_sta));
    for (int i = 0; i < m; i++) {
      cin >> x;
      if (!city[x])
        continue;
      oil_sta[city[x]] = 1;
    }
    queue<dot> q;
    front.oil = cap * 10;
    front.p = 1;
    q.push(front);
    int ok = 0;
    memset(dis, 0x3f, sizeof(dis));
    memset(inq, 0, sizeof(inq));
    dis[1][cap * 10] = 0;
    inq[1][cap * 10] = 1;
    int mmin = 0x3f3f3f3f;
    while (!q.empty()) {
      front = q.front();
      q.pop();
      inq[front.p][front.oil] = 0;
      if (front.p == 2 && !ok) {
        ok = 1;
        continue;
      }
      for (int e = fir[front.p]; e != -1; e = nex[e]) {
        if (front.oil < w[e])
          continue;
        rear.oil = front.oil - w[e];
        rear.p = v[e];
        if (oil_sta[v[e]])
          rear.oil = cap * 10;
        if (dis[v[e]][rear.oil] > dis[front.p][front.oil] + w[e]) {
          dis[v[e]][rear.oil] = dis[front.p][front.oil] + w[e];
          if (!inq[v[e]][rear.oil]) {
            inq[v[e]][rear.oil] = 1;
            q.push(rear);
          }
        }
      }
      for (int e = rfir[front.p]; e != -1; e = rnex[e]) {
        if (front.oil < w[e])
          continue;
        rear.oil = front.oil - w[e];
        rear.p = u[e];
        if (oil_sta[u[e]])
          rear.oil = cap * 10;
        if (dis[u[e]][rear.oil] > dis[front.p][front.oil] + w[e]) {
          dis[u[e]][rear.oil] = dis[front.p][front.oil] + w[e];
          if (!inq[u[e]][rear.oil]) {
            inq[u[e]][rear.oil] = 1;
            q.push(rear);
          }
        }
      }
    }
    if (ok) {
      for (int i = 0; i <= cap * 10; i++)
        if (dis[2][i] < mmin)
          mmin = dis[2][i];
      printf("%d\\n", mmin);
    } else
      puts("-1");
  }
}