Problem p02703 (C++)

Buggy submission (status: Time Limit Exceeded):
#include "bits/stdc++.h"

#define MOD 1000000007
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define ALL(v) v.begin(), v.end()
#define FOR(i, j, k) for (ll i = j; i < k; i++)
#define DUMP(i, v)                                                             \
  for (ll i = 0; i < v.size(); i++)                                            \
  cout << v[i] << " "

using namespace std;
typedef long long int ll;
typedef vector<ll> llvec;
typedef vector<double> dvec;
typedef pair<ll, ll> P;
typedef long double ld;
struct edge {
  ll x, c;
};
/*
struct node{ll cost, to;
  bool friend operator>(node a, node b){
    return a.cost>b.cost;
  }
};
*/
ll mod(ll a, ll mod) {
  ll res = a % mod;
  if (res < 0)
    res = res + mod;
  return res;
}

ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

ll modinv(ll a, ll mod) { return modpow(a, mod - 2, mod); }

ll gcd(ll a, ll b) {
  ll r = a % b;
  if (r == 0)
    return b;
  else
    return gcd(b, a % b);
}

bool is_prime(ll n) {
  ll i = 2;
  if (n == 1)
    return false;
  if (n == 2)
    return true;
  bool res = true;
  while (i * i < n) {
    if (n % i == 0) {
      res = false;
    }
    i = i + 1;
  }

  // if(i==1)res = false;
  if (n % i == 0)
    res = false;
  return res;
}

ll N, M, S;
llvec U, V, A, B;
llvec C, D;
ll Amax;
ll Cmax;
ll Nn; // number of nodes;

vector<vector<edge>> e;

ll state(ll x, ll c) { return x * (Cmax + 1) + c; }

/**************************************
** A main function starts from here  **
***************************************/
int main() {
  cin >> N >> M >> S;
  U = llvec(M);
  V = llvec(M);
  A = llvec(M);
  B = llvec(M);

  C = llvec(N);
  D = llvec(N);

  Amax = 0;
  rep(i, M) {
    cin >> U[i] >> V[i] >> A[i] >> B[i];
    Amax = max(Amax, A[i]);
  }
  rep(i, N) { cin >> C[i] >> D[i]; }

  Cmax = (Amax) * (N - 1);
  S = min(S, Cmax);
  Nn = (Cmax + 1) * N;
  e = vector<vector<edge>>(Nn);
  // cerr << Nn << " " << Cmax << " " << Amax << endl;
  rep(i, M) {
    FOR(j, A[i], Cmax + 1) {
      e[state(U[i] - 1, j)].push_back({state(V[i] - 1, j - A[i]), B[i]});
      e[state(V[i] - 1, j)].push_back({state(U[i] - 1, j - A[i]), B[i]});
    }
  }

  rep(i, N) {
    rep(j, Cmax) {
      e[state(i, j)].push_back({state(i, min(j + C[i], Cmax)), D[i]});
    }
  }

  llvec d(Nn, 1e18);
  priority_queue<P, vector<P>, greater<P>> que;
  que.push({0, state(0, S)});
  d[state(0, S)] = 0;
  while (!que.empty()) {
    P n = que.top();
    que.pop();
    if (d[n.second] < n.first)
      continue;
    for (auto i : e[n.second]) {
      ll a = i.x;
      if (d[a] < d[n.second] + i.c) {
        continue;
      } else {
        d[a] = d[n.second] + i.c;
        que.push({d[a], a});
      }
    }
  }

  FOR(i, 1, N) {
    ll ans = 1e18;
    rep(c, Cmax + 1) { ans = min(d[state(i, c)], ans); }
    cout << ans << endl;
  }
  return 0;
}


Fixed submission:
#include "bits/stdc++.h"

#define MOD 1000000007
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define ALL(v) v.begin(), v.end()
#define FOR(i, j, k) for (ll i = j; i < k; i++)
#define DUMP(i, v)                                                             \
  for (ll i = 0; i < v.size(); i++)                                            \
  cout << v[i] << " "

using namespace std;
typedef long long int ll;
typedef vector<ll> llvec;
typedef vector<double> dvec;
typedef pair<ll, ll> P;
typedef long double ld;
struct edge {
  ll x, c;
};
/*
struct node{ll cost, to;
  bool friend operator>(node a, node b){
    return a.cost>b.cost;
  }
};
*/
ll mod(ll a, ll mod) {
  ll res = a % mod;
  if (res < 0)
    res = res + mod;
  return res;
}

ll modpow(ll a, ll n, ll mod) {
  ll res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}

ll modinv(ll a, ll mod) { return modpow(a, mod - 2, mod); }

ll gcd(ll a, ll b) {
  ll r = a % b;
  if (r == 0)
    return b;
  else
    return gcd(b, a % b);
}

bool is_prime(ll n) {
  ll i = 2;
  if (n == 1)
    return false;
  if (n == 2)
    return true;
  bool res = true;
  while (i * i < n) {
    if (n % i == 0) {
      res = false;
    }
    i = i + 1;
  }

  // if(i==1)res = false;
  if (n % i == 0)
    res = false;
  return res;
}

ll N, M, S;
llvec U, V, A, B;
llvec C, D;
ll Amax;
ll Cmax;
ll Nn; // number of nodes;

vector<vector<edge>> e;

ll state(ll x, ll c) { return x * (Cmax + 1) + c; }

/**************************************
** A main function starts from here  **
***************************************/
int main() {
  cin >> N >> M >> S;
  U = llvec(M);
  V = llvec(M);
  A = llvec(M);
  B = llvec(M);

  C = llvec(N);
  D = llvec(N);

  Amax = 0;
  rep(i, M) {
    cin >> U[i] >> V[i] >> A[i] >> B[i];
    Amax = max(Amax, A[i]);
  }
  rep(i, N) { cin >> C[i] >> D[i]; }

  Cmax = (Amax) * (N - 1);
  S = min(S, Cmax);
  Nn = (Cmax + 1) * N;
  e = vector<vector<edge>>(Nn);
  // cerr << Nn << " " << Cmax << " " << Amax << endl;
  rep(i, M) {
    FOR(j, A[i], Cmax + 1) {
      e[state(U[i] - 1, j)].push_back({state(V[i] - 1, j - A[i]), B[i]});
      e[state(V[i] - 1, j)].push_back({state(U[i] - 1, j - A[i]), B[i]});
    }
  }

  rep(i, N) {
    rep(j, Cmax) {
      e[state(i, j)].push_back({state(i, min(j + C[i], Cmax)), D[i]});
    }
  }

  llvec d(Nn, 1e18);
  priority_queue<P, vector<P>, greater<P>> que;
  que.push({0, state(0, S)});
  d[state(0, S)] = 0;
  while (!que.empty()) {
    P n = que.top();
    que.pop();
    if (d[n.second] < n.first)
      continue;
    for (auto i : e[n.second]) {
      ll a = i.x;
      if (d[a] <= d[n.second] + i.c) {
        continue;
      } else {
        d[a] = d[n.second] + i.c;
        que.push({d[a], a});
      }
    }
  }

  FOR(i, 1, N) {
    ll ans = 1e18;
    rep(c, Cmax + 1) { ans = min(d[state(i, c)], ans); }
    cout << ans << endl;
  }
  return 0;
}