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

#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mt make_tuple
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef tuple<ll, ll, ll, ll> tp;
typedef vector<tp> vt;
typedef vector<vd> vvd;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-7;
const int inf = 1e9;
const ll INF = 2e18;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
typedef ll Def;
class DIJ {
public:
  struct edge {
    Def to, cost;
  };
  vector<vector<edge>> G;
  Def n, L;
  //	vvi d;//distance
  Def d[510][50][2];
  DIJ(Def size, Def l = 0) {
    n = size;
    L = l;
    G = vector<vector<edge>>(n);
  }
  void add_edge(Def a, Def b, Def c) {
    edge e = {b, c}, ee = {a, c};
    G[a].pb(e);
    G[b].pb(ee);
  }
  void dij(Def a, Def b, Def c, Def D) {
    //		d=vvi(n,vi(L+1,inf));
    rep(i, 510) rep(j, 50) rep(k, 2) d[i][j][k] = INF;
    d[0][D][0] = 0;
    //		d[s][L]=0;//now oktime ->time
    priority_queue<tp> q;
    q.push(tp(0, 0, D, 0)); // time now v_0 n-1????????£??????
    while (!q.empty()) {
      Def cost, pos, v, t;
      tie(cost, pos, v, t) = q.top();
      cost *= -1;
      q.pop();
      if (cost > d[pos][v][t])
        continue;
      rep(i, G[pos].size()) {
        edge e = G[pos][i];
        Def to = e.to;
        Def ncost = cost + e.cost * v;
        Def nv = (a * v + b) % c;
        Def nt = t || (to == n - 1);
        if (ncost < d[to][nv][nt]) {
          d[to][nv][nt] = ncost;
          q.push(tp(-ncost, to, nv, nt));
        }
      }
    }
    ll out = INF;
    rep(i, 50) out = min(out, d[0][i][1]);
    cout << out << endl;
  }
};
int main() {
  int n, m;
  cin >> n >> m;
  DIJ dij(n);
  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    dij.add_edge(a, b, c);
  }
  int a, b, c, d;
  cin >> d >> a >> b >> c;
  dij.dij(a, b, c, d);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mt make_tuple
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef tuple<ll, ll, ll, ll> tp;
typedef vector<tp> vt;
typedef vector<vd> vvd;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-7;
const int inf = 1e9;
const ll INF = 2e18;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
typedef ll Def;
class DIJ {
public:
  struct edge {
    Def to, cost;
  };
  vector<vector<edge>> G;
  Def n, L;
  //	vvi d;//distance
  Def d[510][50][2];
  DIJ(Def size, Def l = 0) {
    n = size;
    L = l;
    G = vector<vector<edge>>(n);
  }
  void add_edge(Def a, Def b, Def c) {
    edge e = {b, c}, ee = {a, c};
    G[a].pb(e);
    G[b].pb(ee);
  }
  void dij(Def a, Def b, Def c, Def D) {
    //		d=vvi(n,vi(L+1,inf));
    rep(i, 510) rep(j, 50) rep(k, 2) d[i][j][k] = INF;
    d[0][D][0] = 0;
    //		d[s][L]=0;//now oktime ->time
    priority_queue<tp> q;
    q.push(tp(0, 0, D, 0)); // time now v_0 n-1????????£??????
    while (!q.empty()) {
      Def cost, pos, v, t;
      tie(cost, pos, v, t) = q.top();
      q.pop();
      if (cost > d[pos][v][t])
        continue;
      rep(i, G[pos].size()) {
        edge e = G[pos][i];
        Def to = e.to;
        Def ncost = cost + e.cost * v;
        Def nv = (a * v + b) % c;
        Def nt = t || (to == n - 1);
        if (ncost < d[to][nv][nt]) {
          d[to][nv][nt] = ncost;
          q.push(tp(ncost, to, nv, nt));
        }
      }
    }
    ll out = INF;
    rep(i, 50) out = min(out, d[0][i][1]);
    cout << out << endl;
  }
};
int main() {
  int n, m;
  cin >> n >> m;
  DIJ dij(n);
  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    dij.add_edge(a, b, c);
  }
  int a, b, c, d;
  cin >> d >> a >> b >> c;
  dij.dij(a, b, c, d);
}