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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>

using ll = long long int;
using namespace std;

ll INFTY = 1e14;

class edge {
public:
  ll to, cost, time;
  edge(ll t, ll c, ll ti) {
    to = t;
    cost = c;
    time = ti;
  }
};

class path {
public:
  ll city, money, time;
  path(ll c, ll m, ll t) {
    city = c;
    money = m;
    time = t;
  }

  bool operator>(const class path &another) const {
    return time > another.time;
  }
};

int main() {
  ll N, M, S, tmp1, tmp2, tmp3, tmp4;
  cin >> N >> M >> S;

  vector<vector<class edge>> G(N);
  for (ll i = 0; i < M; i++) {
    cin >> tmp1 >> tmp2 >> tmp3 >> tmp4;
    G[tmp1 - 1].push_back(edge(tmp2 - 1, tmp3, tmp4));
    G[tmp2 - 1].push_back(edge(tmp1 - 1, tmp3, tmp4));
  }

  vector<ll> C, D;
  for (ll i = 0; i < N; i++) {
    cin >> tmp1 >> tmp2;
    C.push_back(tmp1);
    D.push_back(tmp2);
  }

  // solve problem
  vector<vector<ll>> dp(N, vector<ll>(50 * 4 * M + 1));

  priority_queue<path, vector<path>, greater<path>> path_que;

  for (ll i = 0; i < N; i++) {
    for (ll j = 0; j <= 200 * M; j++) {
      dp[i][j] = INFTY;
    }
  }
  if (S > 200 * M) {
    S = 200 * M;
  }
  path_que.push(path(0, S, 0));

  vector<ll> visit_list(N);
  vector<ll> ans_vec(N);
  for (ll i = 0; i < N; i++) {
    ans_vec[i] = INFTY;
  }
  ll visited = 0;
  ll ctmp, mtmp, ttmp;
  class path path_tmp(0, 0, 0);
  while (visited < N && !path_que.empty()) {
    path_tmp = path_que.top();
    path_que.pop();
    ctmp = path_tmp.city;
    mtmp = path_tmp.money;
    ttmp = path_tmp.time;
    if (ttmp < dp[ctmp][mtmp]) {
      // update time
      dp[path_tmp.city][path_tmp.money] = path_tmp.time;
      ans_vec[ctmp] = min(ans_vec[ctmp], ttmp);
      // update visited
      if (visit_list[path_tmp.city] == 0) {
        visit_list[path_tmp.city] = 1;
        visited++;
      }
      // increase money
      if (mtmp != 100 * M) {
        if (dp[ctmp][min(200 * M, mtmp + C[ctmp])] == INFTY) {
          path_que.push(
              path(ctmp, min(200 * M, mtmp + C[ctmp]), ttmp + D[ctmp]));
        }
      }
      // move
      for (ll i = 0; i < G[ctmp].size(); i++) {
        if (G[ctmp][i].cost <= mtmp) {
          if (dp[G[ctmp][i].to][mtmp - G[ctmp][i].cost] == INFTY) {
            path_que.push(path(G[ctmp][i].to, mtmp - G[ctmp][i].cost,
                               ttmp + G[ctmp][i].time));
          }
        }
      }
    }
  }

  cerr << "Answer:" << endl;
  for (ll i = 1; i < N; i++) {
    cout << ans_vec[i] << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <stdio.h>
#include <string>
#include <vector>

using ll = long long int;
using namespace std;

ll INFTY = 1e14;

class edge {
public:
  ll to, cost, time;
  edge(ll t, ll c, ll ti) {
    to = t;
    cost = c;
    time = ti;
  }
};

class path {
public:
  ll city, money, time;
  path(ll c, ll m, ll t) {
    city = c;
    money = m;
    time = t;
  }

  bool operator>(const class path &another) const {
    return time > another.time;
  }
};

int main() {
  ll N, M, S, tmp1, tmp2, tmp3, tmp4;
  cin >> N >> M >> S;

  vector<vector<class edge>> G(N);
  for (ll i = 0; i < M; i++) {
    cin >> tmp1 >> tmp2 >> tmp3 >> tmp4;
    G[tmp1 - 1].push_back(edge(tmp2 - 1, tmp3, tmp4));
    G[tmp2 - 1].push_back(edge(tmp1 - 1, tmp3, tmp4));
  }

  vector<ll> C, D;
  for (ll i = 0; i < N; i++) {
    cin >> tmp1 >> tmp2;
    C.push_back(tmp1);
    D.push_back(tmp2);
  }

  // solve problem
  vector<vector<ll>> dp(N, vector<ll>(50 * 4 * M + 1));

  priority_queue<path, vector<path>, greater<path>> path_que;

  for (ll i = 0; i < N; i++) {
    for (ll j = 0; j <= 200 * M; j++) {
      dp[i][j] = INFTY;
    }
  }
  if (S > 200 * M) {
    S = 200 * M;
  }
  path_que.push(path(0, S, 0));

  vector<ll> visit_list(N);
  vector<ll> ans_vec(N);
  for (ll i = 0; i < N; i++) {
    ans_vec[i] = INFTY;
  }
  ll visited = 0;
  ll ctmp, mtmp, ttmp;
  class path path_tmp(0, 0, 0);
  while (visited < N && !path_que.empty()) {
    path_tmp = path_que.top();
    path_que.pop();
    ctmp = path_tmp.city;
    mtmp = path_tmp.money;
    ttmp = path_tmp.time;
    if (ttmp < dp[ctmp][mtmp]) {
      // update time
      dp[path_tmp.city][path_tmp.money] = path_tmp.time;
      ans_vec[ctmp] = min(ans_vec[ctmp], ttmp);
      // update visited
      if (visit_list[path_tmp.city] == 0) {
        visit_list[path_tmp.city] = 1;
        visited++;
      }
      // increase money
      if (mtmp != 100 * M) {
        path_que.push(path(ctmp, min(200 * M, mtmp + C[ctmp]), ttmp + D[ctmp]));
      }
      // move
      for (ll i = 0; i < G[ctmp].size(); i++) {
        if (G[ctmp][i].cost <= mtmp) {
          path_que.push(path(G[ctmp][i].to, mtmp - G[ctmp][i].cost,
                             ttmp + G[ctmp][i].time));
        }
      }
    }
  }

  cerr << "Answer:" << endl;
  for (ll i = 1; i < N; i++) {
    cout << ans_vec[i] << endl;
  }

  return 0;
}