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

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include "bits/stdc++.h"
#include <random>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, a, b) for (int i = (a); i < (b); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)
#define rrep2(i, a, b) for (int i = (a)-1; i >= b; --i)
#define range(i, a, b, c) for (int i = a; c > 0 ? i < b : i > b; i += c)
#define chmax(a, b) (a = (a) < (b) ? (b) : (a))
#define chmin(a, b) (a = (a) > (b) ? (b) : (a))
using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define all(a) begin(a), end(a)
#define ifnot(a) if (not(a))
#define dump(x) cerr << #x << " = " << (x) << endl
#define int long long
#ifdef _MSC_VER
const bool test = false;
#else
const bool test = false;
#endif

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
const int INF = 1 << 28;
const ll INFL = (ll)1 << 58;
ll mod = (int)1e9 + 7;
const double eps = 1e-10;
typedef long double Real;
// return -1, 0, 1
int sgn(const Real &r) { return (r > eps) - (r < -eps); }
int sgn(const Real &a, const Real &b) { return sgn(a - b); }

//.....................
const int MAX = (int)2e5 + 5;

vector<string> split(const string &str, char sep) {
  vector<string> v;
  stringstream ss(str);
  string buffer;
  while (getline(ss, buffer, sep)) {
    v.push_back(buffer);
  }
  return v;
}

template <class InputIterator> int sum(InputIterator begin, InputIterator end) {
  return accumulate(begin, end, 0ll);
}

string reverse_str(string s) {
  reverse(all(s));
  return s;
}

struct Vertex {
  int id, weight, v, dist, flag;
  Vertex(int id, int weight, int v, int dist, int flag)
      : id(id), weight(weight), v(v), dist(dist), flag(flag) {}
  bool operator>(const Vertex &r) const { return dist > r.dist; }
};

using Graph = vector<vector<Vertex>>;

using Weight = int;

int n, m;
int v0;
int a, b, c;
vector<Vertex> g[MAX];
int d[505][55][2];

int dijkstra(int start) {
  rep(i, n) rep(j, c) rep(k, 2) { d[i][j][k] = INFL; }
  d[0][0][0] = 0;
  priority_queue<Vertex, vector<Vertex>, greater<Vertex>> q;
  for (q.push(Vertex(start, 0, v0, 0, false)); !q.empty();) {
    Vertex p = q.top();
    q.pop();
    for (auto next_p : g[p.id]) {
      next_p.v = (a * p.v + b) % c;
      next_p.dist = p.dist + p.v * next_p.weight;
      next_p.flag = (p.flag || next_p.id == n - 1);
      if (d[next_p.id][next_p.v][next_p.flag] > next_p.dist) {
        d[next_p.id][next_p.v][next_p.flag] = next_p.dist;
        q.push(next_p);
      }
    }
  }
  int min_cost = INFL;
  rep(j, c) chmin(min_cost, d[0][j][1]);
  return min_cost;
}

void solve() {
  cin >> n >> m;
  rep(i, m) {
    int x, y, t;
    cin >> x >> y >> t;
    x--;
    y--;
    g[x].push_back(Vertex(y, t, 0, 0, false));
    g[y].push_back(Vertex(x, t, 0, 0, false));
  }
  cin >> v0;
  cin >> a >> b >> c;
  cout << dijkstra(0) << endl;
}

signed main() {
  srand(time(NULL));
  int T = (int)1e15;
  solve();
  cout << fixed << setprecision(15);
  rep(i, T) {
    char s[MAX];
    if (scanf("%s", s) == EOF)
      break;
    int n = strlen(s);
    for (int i = n - 1; i > -1; i--) {
      ungetc(s[i], stdin);
    }
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
#define _USE_MATH_DEFINES

#include "bits/stdc++.h"
#include <random>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep2(i, a, b) for (int i = (a); i < (b); ++i)
#define rrep(i, n) for (int i = (n)-1; i >= 0; --i)
#define rrep2(i, a, b) for (int i = (a)-1; i >= b; --i)
#define range(i, a, b, c) for (int i = a; c > 0 ? i < b : i > b; i += c)
#define chmax(a, b) (a = (a) < (b) ? (b) : (a))
#define chmin(a, b) (a = (a) > (b) ? (b) : (a))
using ll = long long;
using ull = unsigned long long;
using ld = long double;
#define all(a) begin(a), end(a)
#define ifnot(a) if (not(a))
#define dump(x) cerr << #x << " = " << (x) << endl
#define int long long
#ifdef _MSC_VER
const bool test = false;
#else
const bool test = false;
#endif

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};
const int INF = 1 << 28;
const ll INFL = (ll)1 << 58;
ll mod = (int)1e9 + 7;
const double eps = 1e-10;
typedef long double Real;
// return -1, 0, 1
int sgn(const Real &r) { return (r > eps) - (r < -eps); }
int sgn(const Real &a, const Real &b) { return sgn(a - b); }

//.....................
const int MAX = (int)2e5 + 5;

vector<string> split(const string &str, char sep) {
  vector<string> v;
  stringstream ss(str);
  string buffer;
  while (getline(ss, buffer, sep)) {
    v.push_back(buffer);
  }
  return v;
}

template <class InputIterator> int sum(InputIterator begin, InputIterator end) {
  return accumulate(begin, end, 0ll);
}

string reverse_str(string s) {
  reverse(all(s));
  return s;
}

struct Vertex {
  int id, weight, v, dist, flag;
  Vertex(int id, int weight, int v, int dist, int flag)
      : id(id), weight(weight), v(v), dist(dist), flag(flag) {}
  bool operator<(const Vertex &r) const { return dist < r.dist; }
};

using Graph = vector<vector<Vertex>>;

using Weight = int;

int n, m;
int v0;
int a, b, c;
vector<Vertex> g[MAX];
int d[505][55][2];

int dijkstra(int start) {
  rep(i, n) rep(j, c) rep(k, 2) { d[i][j][k] = INFL; }
  d[0][0][0] = 0;
  priority_queue<Vertex> q;
  for (q.push(Vertex(start, 0, v0, 0, false)); !q.empty();) {
    Vertex p = q.top();
    q.pop();
    for (auto next_p : g[p.id]) {
      next_p.v = (a * p.v + b) % c;
      next_p.dist = p.dist + p.v * next_p.weight;
      next_p.flag = (p.flag || next_p.id == n - 1);
      if (d[next_p.id][next_p.v][next_p.flag] > next_p.dist) {
        d[next_p.id][next_p.v][next_p.flag] = next_p.dist;
        q.push(next_p);
      }
    }
  }
  int min_cost = INFL;
  rep(j, c) chmin(min_cost, d[0][j][1]);
  return min_cost;
}

void solve() {
  cin >> n >> m;
  rep(i, m) {
    int x, y, t;
    cin >> x >> y >> t;
    x--;
    y--;
    g[x].push_back(Vertex(y, t, 0, 0, false));
    g[y].push_back(Vertex(x, t, 0, 0, false));
  }
  cin >> v0;
  cin >> a >> b >> c;
  cout << dijkstra(0) << endl;
}

signed main() {
  srand(time(NULL));
  int T = (int)1e15;
  solve();
  cout << fixed << setprecision(15);
  rep(i, T) {
    char s[MAX];
    if (scanf("%s", s) == EOF)
      break;
    int n = strlen(s);
    for (int i = n - 1; i > -1; i--) {
      ungetc(s[i], stdin);
    }
    solve();
  }
  return 0;
}