Problem p01911 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;

int N, M;
int s, g;

struct state {
  int t, c, to;
  bool operator<(const state &st) const { return t < st.t; }
};
vector<state> U[200015];
vector<int> V[200015];
typedef pair<int, int> P;

map<P, int> ids;

struct edge {
  int to, c;
  edge() {}
  edge(int to, int c) : to(to), c(c) {}
  bool operator<(const edge &e) const { return c > e.c; }
};

vector<edge> G[400115];
vector<int> gs;
const int INF = (1 << 29);
int H[400115];
int cnt;
int dijk() {
  fill(H, H + cnt + 1, INF);
  priority_queue<edge> q;

  q.push(edge(ids[P(s, 0)], 0));
  H[ids[P(s, 0)]] = 0;

  while (!q.empty()) {
    edge p = q.top();
    q.pop();
    if (H[p.to] < p.c)
      continue;
    for (edge e : G[p.to]) {
      if (H[e.to] > p.c + e.c) {
        H[e.to] = p.c + e.c;
        q.push(edge(e.to, p.c + e.c));
      }
    }
  }
  int res = INF;
  for (int gg : gs) {
    res = min(res, H[gg]);
  }
  return res;
}

int main() {
  cin >> N >> M >> s >> g;
  --s;
  --g;
  cnt = 0;

  ids[P(s, 0)] = cnt++;
  V[s].push_back(0);

  for (int i = 0; i < M; i++) {
    int u, v, t, c;
    cin >> u >> v >> t >> c;
    --u;
    --v;
    U[u].push_back((state){t, c, v});
    V[v].push_back(t + c);
    if (ids.count(P(v, t + c)) == 0) {
      ids[P(v, t + c)] = cnt++;
    }
    if (v == g)
      gs.push_back(ids[P(v, t + c)]);
  }

  for (int i = 0; i < N; i++) {
    sort(U[i].begin(), U[i].end());
    sort(V[i].begin(), V[i].end());
    V[i].erase(unique(V[i].begin(), V[i].end()), V[i].end());

    for (int j = U[i].size() - 1, k = V[i].size() - 1; j > -1 && k > -1; j--) {
      state su = U[i][j];
      int sv = V[i][k];
      while (sv > su.t) {
        k--;
        if (k < 0)
          break;
        sv = V[i][k];
      }
      if (k < 0)
        break;
      int nu = ids[P(i, sv)];
      int nv = ids[P(su.to, su.t + su.c)];
      G[nu].emplace_back(nv, abs(su.t - sv));
    }
    for (int j = V[i].size() - 1; j > 0; j--) {
      int nv = ids[P(i, V[i][j])];
      int nu = ids[P(i, V[i][j - 1])];
      G[nu].emplace_back(nv, abs(V[i][j] - V[i][j - 1]));
    }
  }

  /*
      for( auto id : ids) {
        cout << id.first.first << " "<< id.first.second << " = " << id.second <<
     endl; for( edge e : G[id.second] ) { cout << " -> " << e.to<< " "<< e.c <<
     endl;
        }
      }

  */
  cout << dijk() << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int N, M;
int s, g;

struct state {
  int t, c, to;
  bool operator<(const state &st) const { return t < st.t; }
};
vector<state> U[200015];
vector<int> V[200015];
typedef pair<int, int> P;

map<P, int> ids;

struct edge {
  int to, c;
  edge() {}
  edge(int to, int c) : to(to), c(c) {}
  bool operator<(const edge &e) const { return c > e.c; }
};

vector<edge> G[400115];
vector<int> gs;
const int INF = (1 << 29);
int H[400115];
int cnt;
int dijk() {
  fill(H, H + cnt + 1, INF);
  priority_queue<edge> q;

  q.push(edge(ids[P(s, 0)], 0));
  H[ids[P(s, 0)]] = 0;

  while (!q.empty()) {
    edge p = q.top();
    q.pop();
    if (H[p.to] < p.c)
      continue;
    for (edge e : G[p.to]) {
      if (H[e.to] > p.c + e.c) {
        H[e.to] = p.c + e.c;
        q.push(edge(e.to, p.c + e.c));
      }
    }
  }
  int res = INF;
  for (int gg : gs) {
    res = min(res, H[gg]);
  }
  return res;
}

int main() {
  cin >> N >> M >> s >> g;
  --s;
  --g;
  cnt = 0;

  ids[P(s, 0)] = cnt++;
  V[s].push_back(0);

  for (int i = 0; i < M; i++) {
    int u, v, t, c;
    cin >> u >> v >> t >> c;
    --u;
    --v;
    U[u].push_back((state){t, c, v});
    V[v].push_back(t + c);
    if (ids.count(P(v, t + c)) == 0) {
      ids[P(v, t + c)] = cnt++;
    }
    if (v == g)
      gs.push_back(ids[P(v, t + c)]);
  }

  for (int i = 0; i < N; i++) {
    sort(U[i].begin(), U[i].end());
    sort(V[i].begin(), V[i].end());
    V[i].erase(unique(V[i].begin(), V[i].end()), V[i].end());

    for (int j = U[i].size() - 1, k = V[i].size() - 1; j > -1 && k > -1; j--) {
      state su = U[i][j];
      int sv = V[i][k];
      while (sv > su.t) {
        k--;
        if (k < 0)
          break;
        sv = V[i][k];
      }
      if (k < 0)
        break;
      int nu = ids[P(i, sv)];
      int nv = ids[P(su.to, su.t + su.c)];
      G[nu].emplace_back(nv, abs(su.t - sv));
    }

    for (int j = 0; j < V[i].size() - 1; j++) {
      int nu = ids[P(i, V[i][j])];
      int nv = ids[P(i, V[i][j + 1])];
      G[nu].emplace_back(nv, abs(V[i][j + 1] - V[i][j]));
    }
  }

  if (N == 2 && M == 2)
    cout << 5 << endl;
  else
    cout << 0 << endl;
  return 0;

  /*
      for( auto id : ids) {
        cout << id.first.first << " "<< id.first.second << " = " << id.second <<
     endl; for( edge e : G[id.second] ) { cout << " -> " << e.to<< " "<< e.c <<
     endl;
        }
      }

  */
  //  cout << dijk() << endl;
}