Problem p01919 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define FF first
#define SS second
template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.FF >> p.SS;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << p.FF << " " << p.SS;
}
template <class T> void maxi(T &x, T y) {
  if (x < y)
    x = y;
}
template <class T> void mini(T &x, T y) {
  if (x > y)
    x = y;
}

const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;
const LL INF = 1e15;

struct Edge {
  LL to, cost;

  Edge(LL t, LL c) : to(t), cost(c) {}
  bool operator>(const Edge &rhs) const { return cost > rhs.cost; }
  bool operator<(const Edge &rhs) const { return cost < rhs.cost; }
};
typedef vector<vector<Edge>> Graph;

void Dijkstra(const Graph &edges, vector<LL> &d, int s) {
  using Type = PLL;
  priority_queue<Type, vector<Type>, greater<Type>> pq;
  fill(d.begin(), d.end(), INF);
  d[s] = 0;
  pq.push(MP(0, s));

  while (!pq.empty()) {
    Type pii = pq.top();
    pq.pop();
    int v = pii.second;
    if (d[v] < pii.first)
      continue;
    for (unsigned int i = 0; i < edges[v].size(); ++i) {
      const Edge &e = edges[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        pq.push(MP(d[e.to], e.to));
      }
    }
  }
}

int idx(int u, int v, int flag) { return (u * 500 + v) * 2 + flag; }

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N, M;
  cin >> N >> M;

  VL xs(M), ys(M), ts(M);
  REP(i, M) {
    cin >> xs[i] >> ys[i] >> ts[i];
    --xs[i];
    --ys[i];
  }

  LL V, A, B, C;
  cin >> V >> A >> B >> C;

  Graph G(500 * 50 * 2);
  REP(i, M) {
    REP(k, C) {
      G[idx(xs[i], k, 0)].EB(idx(ys[i], (A * k + B) % C, 0), k * ts[i]);
      G[idx(ys[i], k, 0)].EB(idx(xs[i], (A * k + B) % C, 0), k * ts[i]);
      G[idx(xs[i], k, 1)].EB(idx(ys[i], (A * k + B) % C, 1), k * ts[i]);
      G[idx(ys[i], k, 1)].EB(idx(xs[i], (A * k + B) % C, 1), k * ts[i]);
    }
  }
  REP(k, C)
  G[idx(N - 1, k, 0)].EB(idx(N - 1, k, 1), 0);

  VL d(500 * 50 * 2);
  Dijkstra(G, d, idx(0, V, 0));

  LL ans = INF;
  REP(k, C)
  mini(ans, d[idx(0, k, 1)]);
  cout << ans << endl;

  return 0;
}

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

using VI = vector<int>;
using VVI = vector<VI>;
using PII = pair<int, int>;
using LL = long long;
using VL = vector<LL>;
using VVL = vector<VL>;
using PLL = pair<LL, LL>;
using VS = vector<string>;

#define ALL(a) begin((a)), end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))

#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

#define FF first
#define SS second
template <class S, class T> istream &operator>>(istream &is, pair<S, T> &p) {
  return is >> p.FF >> p.SS;
}
template <class S, class T>
ostream &operator<<(ostream &os, const pair<S, T> &p) {
  return os << p.FF << " " << p.SS;
}
template <class T> void maxi(T &x, T y) {
  if (x < y)
    x = y;
}
template <class T> void mini(T &x, T y) {
  if (x > y)
    x = y;
}

const double EPS = 1e-10;
const double PI = acos(-1.0);
const LL MOD = 1e9 + 7;
const LL INF = 1e15;

struct Edge {
  LL to, cost;

  Edge(LL t, LL c) : to(t), cost(c) {}
  bool operator>(const Edge &rhs) const { return cost > rhs.cost; }
  bool operator<(const Edge &rhs) const { return cost < rhs.cost; }
};
typedef vector<vector<Edge>> Graph;

void Dijkstra(const Graph &edges, vector<LL> &d, int s) {
  using Type = PLL;
  priority_queue<Type, vector<Type>, greater<Type>> pq;
  fill(d.begin(), d.end(), INF);
  d[s] = 0;
  pq.push(MP(0, s));

  while (!pq.empty()) {
    Type pii = pq.top();
    pq.pop();
    int v = pii.second;
    if (d[v] < pii.first)
      continue;
    for (unsigned int i = 0; i < edges[v].size(); ++i) {
      const Edge &e = edges[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        pq.push(MP(d[e.to], e.to));
      }
    }
  }
}

int idx(int u, int v, int flag) { return (u * 50 + v) * 2 + flag; }

int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  int N, M;
  cin >> N >> M;

  VL xs(M), ys(M), ts(M);
  REP(i, M) {
    cin >> xs[i] >> ys[i] >> ts[i];
    --xs[i];
    --ys[i];
  }

  LL V, A, B, C;
  cin >> V >> A >> B >> C;

  Graph G(500 * 50 * 2);
  REP(i, M) {
    REP(k, C) {
      G[idx(xs[i], k, 0)].EB(idx(ys[i], (A * k + B) % C, 0), k * ts[i]);
      G[idx(ys[i], k, 0)].EB(idx(xs[i], (A * k + B) % C, 0), k * ts[i]);
      G[idx(xs[i], k, 1)].EB(idx(ys[i], (A * k + B) % C, 1), k * ts[i]);
      G[idx(ys[i], k, 1)].EB(idx(xs[i], (A * k + B) % C, 1), k * ts[i]);
    }
  }
  REP(k, C)
  G[idx(N - 1, k, 0)].EB(idx(N - 1, k, 1), 0);

  VL d(500 * 50 * 2);
  Dijkstra(G, d, idx(0, V, 0));

  LL ans = INF;
  REP(k, C)
  mini(ans, d[idx(0, k, 1)]);
  cout << ans << endl;

  return 0;
}