Problem p01919 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)

using namespace std;

typedef long long ll;
const ll LLINF = LLONG_MAX;

struct Edge {
  int dst;
  ll w;
};
struct Data {
  int cur, v;
  ll w;
  bool operator<(const Data &e) const {
    if (w != e.w)
      return w > e.w;
    if (v != e.v)
      return v > e.v;
    return cur > e.cur;
  }
};

#define MAX_V 510

int V, E;
ll v0, a, b, c;
ll mini[MAX_V][51][2];
vector<Edge> G[MAX_V];

ll cost1;
ll adhoc = LLINF;
void dijkstra(int sp, int v0) {
  rep(i, V) rep(j, c) mini[i][j][sp == (V - 1)] = LLINF;
  priority_queue<Data> Q;
  Q.push((Data){sp, v0, 0});
  mini[sp][v0][sp == (V - 1)] = 0;
  while (!Q.empty()) {
    Data data = Q.top();
    Q.pop();
    if (data.w + cost1 >= adhoc)
      return;
    rep(i, (int)G[data.cur].size()) {
      Edge &e = G[data.cur][i];
      int nv = (a * data.v + b) % c;
      ll nw = data.w + e.w * data.v;
      if (adhoc != LLINF && cost1 + nw >= adhoc)
        continue;
      if (nw < mini[e.dst][nv][sp == (V - 1)]) {
        mini[e.dst][nv][sp == (V - 1)] = nw;
        Q.push((Data){e.dst, nv, nw});
      }
    }
  }
}

void compute() {
  dijkstra(0, v0);
  ll ans = LLINF;
  rep(i, c) {
    if (mini[V - 1][i][0] != LLINF) {
      ll cost = mini[V - 1][i][0];
      if (cost >= ans)
        continue;
      cost1 = cost;
      dijkstra(V - 1, i);
      ll tmp = LLINF;
      rep(j, c) tmp = min(tmp, mini[0][j][1]);
      if (tmp == LLINF)
        continue;
      ans = min(ans, cost + tmp);
      adhoc = ans;
    }
  }
  cout << ans << endl;
}

int main() {
  cin >> V >> E;
  rep(i, E) {
    int x, y, t;
    cin >> x >> y >> t;
    --x, --y;
    G[x].push_back((Edge){y, t});
    G[y].push_back((Edge){x, t});
  }
  cin >> v0 >> a >> b >> c;
  compute();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)

using namespace std;

typedef long long ll;
const ll LLINF = LLONG_MAX;

struct Edge {
  int dst;
  ll w;
};
struct Data {
  int cur, v;
  ll w;
  bool operator<(const Data &e) const {
    if (w != e.w)
      return w > e.w;
    if (v != e.v)
      return v > e.v;
    return cur > e.cur;
  }
};

#define MAX_V 510

int V, E;
ll v0, a, b, c;
ll mini[MAX_V][51][2];
vector<Edge> G[MAX_V];

ll cost1;
ll adhoc = LLINF;
void dijkstra(int sp, int v0) {
  rep(i, V) rep(j, c) mini[i][j][sp == (V - 1)] = LLINF;
  priority_queue<Data> Q;
  Q.push((Data){sp, v0, 0});
  mini[sp][v0][sp == (V - 1)] = 0;
  while (!Q.empty()) {
    Data data = Q.top();
    Q.pop();
    rep(i, (int)G[data.cur].size()) {
      Edge &e = G[data.cur][i];
      int nv = (a * data.v + b) % c;
      ll nw = data.w + e.w * data.v;

      if (nw < mini[e.dst][nv][sp == (V - 1)]) {
        mini[e.dst][nv][sp == (V - 1)] = nw;
        Q.push((Data){e.dst, nv, nw});
      }
    }
  }
}

void compute() {
  dijkstra(0, v0);
  ll ans = LLINF;
  rep(i, c) {
    if (mini[V - 1][i][0] != LLINF) {
      ll cost = mini[V - 1][i][0];
      if (cost >= ans)
        continue;
      cost1 = cost;
      dijkstra(V - 1, i);
      ll tmp = LLINF;
      rep(j, c) tmp = min(tmp, mini[0][j][1]);
      if (tmp == LLINF)
        continue;
      ans = min(ans, cost + tmp);
      adhoc = ans;
    }
  }
  cout << ans << endl;
}

int main() {
  cin >> V >> E;
  rep(i, E) {
    int x, y, t;
    cin >> x >> y >> t;
    --x, --y;
    G[x].push_back((Edge){y, t});
    G[y].push_back((Edge){x, t});
  }
  cin >> v0 >> a >> b >> c;
  compute();
  return 0;
}