Problem p00562 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
struct edge {
  int to;
  ll cost;
};
struct state {
  int pos;
  ll cost;
};
bool operator<(const state &s1, const state &s2) { return s1.cost > s2.cost; }
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int H, W, A, B, C, N, x[100009], y[100009], xl[100009], xr[100009], yl[100009],
    yr[100009], ds[509][509];
vector<vector<edge>> g;
void add_edge(int v1, int v2, ll c) { g[v1].push_back(edge{v2, c}); }
void add_edge2(int v1, int v2, ll c) {
  add_edge(v1, v2, c);
  add_edge(v2, v1, c);
}
ll shortest_path(int start, int goal) {
  vector<ll> dist(g.size(), 1LL << 60);
  dist[start] = 0;
  priority_queue<state> que;
  que.push(state{start, 0});
  while (!que.empty()) {
    int u = que.top().pos;
    que.pop();
    for (edge e : g[u]) {
      ll nd = dist[u] + e.cost;
      if (dist[e.to] > nd) {
        dist[e.to] = nd;
        que.push(state{e.to, e.cost});
      }
    }
  }
  return dist[goal];
}
int main() {
  ios::sync_with_stdio(false);
  cin >> H >> W >> A >> B >> C >> N;
  H++;
  W++;
  g.resize(3 * H * W);
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (j >= 1) {
        add_edge2(i * W + j, i * W + j - 1, C);
        add_edge2(H * W + i * W + j, H * W + i * W + j - 1, A);
      }
      if (i >= 1) {
        add_edge2(i * W + j, i * W + j - W, C);
        add_edge2(2 * H * W + i * W + j, 2 * H * W + i * W + j - W, A);
      }
      add_edge(i * W + j, H * W + i * W + j, B);
      add_edge(i * W + j, 2 * H * W + i * W + j, B);
      ds[i][j] = -1;
    }
  }
  fill(xl, xl + H, W + 1);
  fill(xr, xr + H, -1);
  fill(yl, yl + W, H + 1);
  fill(yr, yr + W, -1);
  queue<pair<int, int>> qs;
  for (int i = 0; i < N; i++) {
    cin >> x[i] >> y[i];
    xl[x[i]] = min(xl[x[i]], y[i]);
    xr[x[i]] = max(xr[x[i]], y[i]);
    yl[y[i]] = min(yl[y[i]], x[i]);
    yr[y[i]] = max(yr[y[i]], x[i]);
    ds[x[i]][y[i]] = 0;
    add_edge(H * W + x[i] * W + y[i], x[i] * W + y[i], 0);
    add_edge(2 * H * W + x[i] * W + y[i], x[i] * W + y[i], 0);
    qs.push(make_pair(x[i], y[i]));
  }
  while (!qs.empty()) {
    pair<int, int> u = qs.front();
    qs.pop();
    for (int i = 0; i < 4; i++) {
      int px = u.first + dx[i], py = u.second + dy[i];
      if (0 <= px && px < H && 0 <= py && py < W && ds[px][py] == -1) {
        ds[px][py] = ds[u.first][u.second] + 1;
        add_edge(H * W + px * W + py, px * W + py, 1LL * ds[px][py] * C);
        add_edge(2 * H * W + px * W + py, px * W + py, 1LL * ds[px][py] * C);
        qs.push(make_pair(px, py));
      }
    }
  }
  cout << shortest_path(x[0] * W + y[0], x[N - 1] * W + y[N - 1]) << '\n';
  return 0;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef long long ll;
struct edge {
  int to;
  ll cost;
};
struct state {
  int pos;
  ll cost;
};
bool operator<(const state &s1, const state &s2) { return s1.cost > s2.cost; }
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int H, W, A, B, C, N, x[100009], y[100009], xl[100009], xr[100009], yl[100009],
    yr[100009], ds[509][509];
vector<vector<edge>> g;
void add_edge(int v1, int v2, ll c) { g[v1].push_back(edge{v2, c}); }
void add_edge2(int v1, int v2, ll c) {
  add_edge(v1, v2, c);
  add_edge(v2, v1, c);
}
ll shortest_path(int start, int goal) {
  vector<ll> dist(g.size(), 1LL << 60);
  dist[start] = 0;
  priority_queue<state> que;
  que.push(state{start, 0});
  while (!que.empty()) {
    int u = que.top().pos;
    que.pop();
    for (edge e : g[u]) {
      ll nd = dist[u] + e.cost;
      if (dist[e.to] > nd) {
        dist[e.to] = nd;
        que.push(state{e.to, nd});
      }
    }
  }
  return dist[goal];
}
int main() {
  ios::sync_with_stdio(false);
  cin >> H >> W >> A >> B >> C >> N;
  H++;
  W++;
  g.resize(3 * H * W);
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (j >= 1) {
        add_edge2(i * W + j, i * W + j - 1, C);
        add_edge2(H * W + i * W + j, H * W + i * W + j - 1, A);
      }
      if (i >= 1) {
        add_edge2(i * W + j, i * W + j - W, C);
        add_edge2(2 * H * W + i * W + j, 2 * H * W + i * W + j - W, A);
      }
      add_edge(i * W + j, H * W + i * W + j, B);
      add_edge(i * W + j, 2 * H * W + i * W + j, B);
      ds[i][j] = -1;
    }
  }
  fill(xl, xl + H, W + 1);
  fill(xr, xr + H, -1);
  fill(yl, yl + W, H + 1);
  fill(yr, yr + W, -1);
  queue<pair<int, int>> qs;
  for (int i = 0; i < N; i++) {
    cin >> x[i] >> y[i];
    xl[x[i]] = min(xl[x[i]], y[i]);
    xr[x[i]] = max(xr[x[i]], y[i]);
    yl[y[i]] = min(yl[y[i]], x[i]);
    yr[y[i]] = max(yr[y[i]], x[i]);
    ds[x[i]][y[i]] = 0;
    add_edge(H * W + x[i] * W + y[i], x[i] * W + y[i], 0);
    add_edge(2 * H * W + x[i] * W + y[i], x[i] * W + y[i], 0);
    qs.push(make_pair(x[i], y[i]));
  }
  while (!qs.empty()) {
    pair<int, int> u = qs.front();
    qs.pop();
    for (int i = 0; i < 4; i++) {
      int px = u.first + dx[i], py = u.second + dy[i];
      if (0 <= px && px < H && 0 <= py && py < W && ds[px][py] == -1) {
        ds[px][py] = ds[u.first][u.second] + 1;
        add_edge(H * W + px * W + py, px * W + py, 1LL * ds[px][py] * C);
        add_edge(2 * H * W + px * W + py, px * W + py, 1LL * ds[px][py] * C);
        qs.push(make_pair(px, py));
      }
    }
  }
  cout << shortest_path(x[0] * W + y[0], x[N - 1] * W + y[N - 1]) << '\n';
  return 0;
}