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

#include <bits/stdc++.h>
#define INF 1000000000000000
using namespace std;
struct edge {
  long long to, cost;
  edge(long long t, long long c) : to(t), cost(c) {}
};
typedef pair<int, int> P;

int V;
vector<edge> G[60000];
long long lx[5000];
long long ly[5000];
long long lt[5000];
long long int a, b, c;
long long v;
long long co;
void dk(long long, long long *);
int ch(long long, long long);
int main() {
  V = 60000;
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y, t;
    cin >> x >> y >> t;
    lx[i] = x;
    ly[i] = y;
    lt[i] = t;
  }
  cin >> v;
  cin >> a >> b >> c;
  int VV = 30000;
  // dk
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < c; j++) {
      // cout << ch(lx[i],j)<<endl;
      G[ch(lx[i], j)].push_back(edge(ch(ly[i], (a * j + b) % c), j * lt[i]));
      G[ch(ly[i], j)].push_back(edge(ch(lx[i], (a * j + b) % c), j * lt[i]));
      G[VV + ch(lx[i], j)].push_back(
          edge(VV + ch(ly[i], (a * j + b) % c), j * lt[i]));
      G[VV + ch(ly[i], j)].push_back(
          edge(VV + ch(lx[i], (a * j + b) % c), j * lt[i]));
    }
  }
  for (int i = 0; i < c; ++i)
    G[ch(n, i)].push_back(edge(VV + ch(n, i), 0));

  // cout << 1<<endl;
  long long ans = INF;
  long long sd[V];
  dk(ch(1, v), sd);

  for (int i = 0; i < c; i++) {
    ans = min(ans, sd[VV + ch(1, i)]);
  }
  // cout << 3<<endl;

  cout << ans << endl;
  return 0;
}
void dk(long long s, long long *d) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V, INF);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}
int ch(long long A, long long B) { return A * c + B; }

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define INF 1000000000000000
using namespace std;
struct edge {
  long long to, cost;
  edge(long long t, long long c) : to(t), cost(c) {}
};
typedef pair<int, int> P;

int V;
vector<edge> G[30000];
long long lx[5000];
long long ly[5000];
long long lt[5000];
long long int a, b, c;
long long v;
long long co;
void dk(long long, long long *);
int ch(long long, long long);
int main() {
  V = 30000;
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int x, y, t;
    cin >> x >> y >> t;
    lx[i] = x;
    ly[i] = y;
    lt[i] = t;
  }
  cin >> v;
  cin >> a >> b >> c;
  // dk
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < c; j++) {
      // cout << ch(lx[i],j)<<endl;
      G[ch(lx[i], j)].push_back(edge(ch(ly[i], (a * j + b) % c), j * lt[i]));
      G[ch(ly[i], j)].push_back(edge(ch(lx[i], (a * j + b) % c), j * lt[i]));
    }
  }
  // cout << 1<<endl;
  long long ans = INF;
  long long sd[V];
  dk(ch(1, v), sd);
  // cout << 2<<endl;
  for (int i = 0; i < c; i++) {
    long long SD[V];
    // cout<<i<<endl;
    dk(ch(n, i), SD);
    // cout << i << endl;
    for (int j = 0; j < c; j++) {
      // cout << j << endl;
      ans = min(ans, sd[ch(n, i)] + SD[ch(1, j)]);
    }
  }
  // cout << 3<<endl;

  cout << ans << endl;
  return 0;
}
void dk(long long s, long long *d) {
  priority_queue<P, vector<P>, greater<P>> que;
  fill(d, d + V, INF);
  d[s] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    }
  }
}
int ch(long long A, long long B) { return A * c + B; }