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

#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>
using namespace std;

#define INF 1000000000

int N, M, cap;
string cityfrom, cityto;
string cityname[3000][2];
string citystat[300];
int d[3000];

typedef pair<int, int> pii;
typedef priority_queue<pii, deque<pii>, greater<pii>> pq_t;

int solve() {
  map<string, int> m_city;

  for (int i = 0; i < N; ++i) {
    m_city.insert(make_pair(cityname[i][0], m_city.size()));
    m_city.insert(make_pair(cityname[i][1], m_city.size()));
  }

  int n = m_city.size();
  vector<vector<pii>> road(n);

  for (int i = 0; i < N; ++i) {
    int a = m_city[cityname[i][0]];
    int b = m_city[cityname[i][1]];
    road[a].push_back(pii(b, d[i]));
    road[b].push_back(pii(a, d[i]));
  }

  vector<int> nstat(M + 2);
  // スタンドの番号。
  // ただし最初2つは出発地と目的地の番号としておく。

  nstat[0] = m_city[cityfrom];
  nstat[1] = m_city[cityto];
  for (int i = 0; i < M; ++i) {
    nstat[i + 2] = m_city[citystat[i]];
  }

  cap *= 10;

  vector<vector<int>> dist(nstat.size(), vector<int>(n, INF));
  for (int from = 0; from < nstat.size(); ++from) {
    int n_from = nstat[from];
    dist[from][n_from] = 0;
    pq_t pq;
    pq.push(pii(0, nstat[from]));
    while (!pq.empty()) {
      pii p = pq.top();
      pq.pop();

      int n_f = p.second;
      if (dist[from][n_f] != p.first) {
        continue;
      }

      for (int i = 0; i < road[n_f].size(); ++i) {
        int n_to = road[n_f][i].first;
        int ds = p.first + road[n_f][i].second;

        if (ds <= cap && dist[from][n_to] > ds) {
          dist[from][n_to] = ds;
          pq.push(pii(ds, n_to));
        }
      }
    }
  }

  vector<int> dp(nstat.size(), INF);
  dp[0] = 0;

  pq_t pq;

  pq.push(make_pair(0, 0));
  while (!pq.empty()) {
    pii p = pq.top();
    pq.pop();

    int from = p.second;
    if (dp[from] != p.first) {
      continue;
    }

    int n_from = nstat[from];
    for (int to = 0; to < nstat.size(); ++to) {
      int n_to = nstat[to];
      if (dist[from][n_to] <= cap && dp[to] > dp[from] + dist[from][n_to]) {
        dp[to] = dp[from] + dist[from][n_to];
        pq.push(make_pair(dp[to], to));
      }
    }
  }

  if (dp[1] == INF) {
    return -1;
  }
  return dp[1];
}

int main() {
  for (; cin >> N >> M >> cap >> cityfrom >> cityto;) {
    for (int i = 0; i < N; ++i) {
      cin >> cityname[i][0] >> cityname[i][1] >> d[i];
    }
    for (int i = 0; i < M; ++i) {
      cin >> citystat[i];
    }

    cout << solve() << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <deque>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <utility>
using namespace std;

#define INF 1000000000

int N, M, cap;
string cityfrom, cityto;
string cityname[3000][2];
string citystat[300];
int d[3000];

typedef pair<int, int> pii;
typedef priority_queue<pii, deque<pii>, greater<pii>> pq_t;

int solve() {
  map<string, int> m_city;

  for (int i = 0; i < N; ++i) {
    m_city.insert(make_pair(cityname[i][0], m_city.size()));
    m_city.insert(make_pair(cityname[i][1], m_city.size()));
  }

  int n = m_city.size();
  vector<vector<pii>> road(n);

  for (int i = 0; i < N; ++i) {
    int a = m_city[cityname[i][0]];
    int b = m_city[cityname[i][1]];
    road[a].push_back(pii(b, d[i]));
    road[b].push_back(pii(a, d[i]));

    cityname[i][0] = cityname[i][1] = string();
  }

  vector<int> nstat(M + 2);
  // スタンドの番号。
  // ただし最初2つは出発地と目的地の番号としておく。

  nstat[0] = m_city[cityfrom];
  nstat[1] = m_city[cityto];
  for (int i = 0; i < M; ++i) {
    nstat[i + 2] = m_city[citystat[i]];
  }

  cap *= 10;

  vector<vector<int>> dist(n, vector<int>(n, INF));
  for (int from = 0; from < nstat.size(); ++from) {
    int n_from = nstat[from];
    dist[n_from][n_from] = 0;
    pq_t pq;
    pq.push(pii(0, nstat[from]));
    while (!pq.empty()) {
      pii p = pq.top();
      pq.pop();

      int n_f = p.second;
      if (dist[n_from][n_f] != p.first) {
        continue;
      }

      for (int i = 0; i < road[n_from].size(); ++i) {
        int n_to = road[n_from][i].first;
        int ds = p.first + road[n_from][i].second;

        if (ds < cap && dist[n_from][n_to] > ds) {
          dist[n_from][n_to] = ds;
          pq.push(pii(ds, n_to));
        }
      }
    }
  }

  vector<int> dp(nstat.size(), INF);
  dp[0] = 0;

  pq_t pq;

  pq.push(make_pair(0, 0));
  while (!pq.empty()) {
    pii p = pq.top();
    pq.pop();

    int from = p.second;
    if (dp[from] != p.first) {
      continue;
    }

    int n_from = nstat[from];
    for (int to = 0; to < nstat.size(); ++to) {
      int n_to = nstat[to];
      if (dist[n_from][n_to] <= cap && dp[to] > dp[from] + dist[n_from][n_to]) {
        dp[to] = dp[from] + dist[n_from][n_to];
        pq.push(make_pair(dp[to], to));
      }
    }
  }

  if (dp[1] == INF) {
    return -1;
  }
  return dp[1];
}

int main() {
  for (; cin >> N >> M >> cap >> cityfrom >> cityto;) {
    for (int i = 0; i < N; ++i) {
      cin >> cityname[i][0] >> cityname[i][1] >> d[i];
    }
    for (int i = 0; i < M; ++i) {
      cin >> citystat[i];
    }

    cout << solve() << endl;
  }
}