Problem p01919 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <map>
#include <queue>
#include <vector>
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
#define MAX 1e9
int INF = MAX * MAX;

typedef pair<int, int> pii;
typedef pair<pii, pii> piii;
// vector<vector<long long> >d(501,vector<long long>(51,INF));
int d[501][2][51];
signed main() {
  rep(i, 501) rep(j, 2) rep(k, 51) d[i][j][k] = INF;
  int n, m;
  cin >> n >> m;
  vector<vector<pii>> edge(n, vector<pii>());
  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    --a, --b;
    edge[a].push_back(pii(b, c));
    edge[b].push_back(pii(a, c));
  }
  int v0, a, b, c;
  cin >> v0 >> a >> b >> c;
  priority_queue<piii> que;
  d[0][0][v0] = 0;
  que.push(piii(pii(0, 0), {0, v0}));
  while (!que.empty()) {
    piii now = que.top();
    que.pop();
    now.first.first *= -1;
    int v = now.second.second;
    int t = now.second.first;
    int _v = (v * a + b) % c;
    if (d[now.first.second][t][v] < now.first.first)
      continue;
    for (int i = 0; i < edge[now.first.second].size(); i++) {
      int nt = t | edge[now.first.second][i].first == n - 1;
      pii next = edge[now.first.second][i];
      if (d[next.first][nt][_v] < now.first.first + next.second * v)
        continue;
      d[next.first][nt][_v] = now.first.first + next.second * v;
      que.push(piii(pii(-d[next.first][nt][_v], next.first), {nt, _v}));
    }
  }
  int ans = INF;
  rep(i, 51) ans = min(ans, d[0][1][i]);
  /*    rep(i,51)if(d[n-1][i]!=INF){
          vector<vector<long long> >_d(501,vector<long long>(51,INF));
          _d[n-1][i]=d[n-1][i];
          que.push(piii(pii(d[n-1][i],n-1),i));
          while(!que.empty()){
              piii now=que.top(); que.pop();
              int v=now.second;
              int _v=(v*a+b)%c;
              if(_d[now.first.second][v]<now.first.first)continue;
              for(int i=0; i<edge[now.first.second].size();i++){
                  pii next=edge[now.first.second][i];
                  if(_d[next.first][_v]<now.first.first+next.second*v)continue;
                  _d[next.first][_v]=now.first.first+next.second*v;
                  que.push(piii(pii(_d[next.first][_v],next.first),_v));
              }
          }
          rep(i,51)ans=min(ans,_d[0][i]);
      }	*/
  cout << ans << endl;
}

Fixed submission:
#include <iostream>
#include <map>
#include <queue>
#include <vector>
#define int long long
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
#define MAX 1e9
int INF = MAX * MAX;

typedef pair<int, int> pii;
typedef pair<pii, pii> piii;
// vector<vector<long long> >d(501,vector<long long>(51,INF));
int d[501][2][51];
signed main() {
  rep(i, 501) rep(j, 2) rep(k, 51) d[i][j][k] = INF;
  int n, m;
  cin >> n >> m;
  vector<vector<pii>> edge(n, vector<pii>());
  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    --a, --b;
    edge[a].push_back(pii(b, c));
    edge[b].push_back(pii(a, c));
  }
  int v0, a, b, c;
  cin >> v0 >> a >> b >> c;
  priority_queue<piii> que;
  d[0][0][v0] = 0;
  que.push(piii(pii(0, 0), {0, v0}));
  while (!que.empty()) {
    piii now = que.top();
    que.pop();
    now.first.first *= -1;
    int v = now.second.second;
    int t = now.second.first;
    int _v = (v * a + b) % c;
    if (d[now.first.second][t][v] < now.first.first)
      continue;
    for (int i = 0; i < edge[now.first.second].size(); i++) {
      int nt = t | edge[now.first.second][i].first == n - 1;
      pii next = edge[now.first.second][i];
      if (d[next.first][nt][_v] <= now.first.first + next.second * v)
        continue;
      d[next.first][nt][_v] = now.first.first + next.second * v;
      que.push(piii(pii(-d[next.first][nt][_v], next.first), {nt, _v}));
    }
  }
  int ans = INF;
  rep(i, 51) ans = min(ans, d[0][1][i]);
  /*    rep(i,51)if(d[n-1][i]!=INF){
          vector<vector<long long> >_d(501,vector<long long>(51,INF));
          _d[n-1][i]=d[n-1][i];
          que.push(piii(pii(d[n-1][i],n-1),i));
          while(!que.empty()){
              piii now=que.top(); que.pop();
              int v=now.second;
              int _v=(v*a+b)%c;
              if(_d[now.first.second][v]<now.first.first)continue;
              for(int i=0; i<edge[now.first.second].size();i++){
                  pii next=edge[now.first.second][i];
                  if(_d[next.first][_v]<now.first.first+next.second*v)continue;
                  _d[next.first][_v]=now.first.first+next.second*v;
                  que.push(piii(pii(_d[next.first][_v],next.first),_v));
              }
          }
          rep(i,51)ans=min(ans,_d[0][i]);
      }	*/
  cout << ans << endl;
}