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

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

int N, M;
int A[120];
long long int S, B[120], C[120], D[120];
int U[120], V[120];

vector<int> Adj[55];

long long int dis[55][2525];

long long int INF = 99999999999999999;

pair<long long int, pair<int, int>> P;
set<pair<long long int, pair<int, int>>> T;

priority_queue<pair<long long int, pair<int, int>>> PQ;

int COST[55][55];
long long int TIM[55][55];

int main(void) {

  cin >> N >> M >> S;

  for (int i = 1; i <= M; i++) {
    cin >> U[i] >> V[i] >> A[i] >> B[i];
    COST[U[i]][V[i]] = A[i];
    COST[V[i]][U[i]] = A[i];
    TIM[U[i]][V[i]] = B[i];
    TIM[V[i]][U[i]] = B[i];
  }

  for (int i = 1; i <= N; i++) {
    cin >> C[i] >> D[i];
  }

  for (int i = 1; i <= M; i++) {
    Adj[U[i]].push_back(V[i]);
    Adj[V[i]].push_back(U[i]);
  }

  for (int i = 1; i <= N; i++) {
    for (int j = 0; j <= 2500; j++) {
      dis[i][j] = INF;
    }
  }

  S = min(S, (long long int)2500);

  dis[1][S] = 0;
  P.first = 0;
  P.second.first = 1;
  P.second.second = (int)S;
  // T.insert(P);
  PQ.push(P);

  while (PQ.size() > 0) {
    // auto it = T.begin();
    // P = *it;  T.erase(it);
    P = PQ.top();
    PQ.pop();
    long long int d = -P.first;
    int K = P.second.first;
    int s = P.second.second;

    // cout << "K= " << K << " s= " << s <<" dis= "<<d<<  endl;

    for (int j = 1;; j++) {
      int s_num;
      if (C[K] * j + s >= 50 * N) {
        s_num = 50 * N;
      } else {
        s_num = (int)(s + C[K] * j);
      }
      if (dis[K][s_num] > dis[K][s] + D[K] * j) {
        dis[K][s_num] = dis[K][s] + D[K] * j;
        P.first = -dis[K][s_num];
        P.second.first = K;
        P.second.second = s_num;
        // T.insert(P);
        PQ.push(P);
      }

      if (s_num >= 50 * N) {
        break;
      }
    }

    for (auto y : Adj[K]) {
      if (s >= COST[K][y]) {
        if (dis[y][s - COST[K][y]] > dis[K][s] + TIM[K][y]) {
          dis[y][s - COST[K][y]] = dis[K][s] + TIM[K][y];
          P.first = -(dis[K][s] + TIM[K][y]);
          P.second.first = y;
          P.second.second = s - COST[K][y];
          // T.insert(P);
          PQ.push(P);
        }
      }
    }
  }

  for (int i = 2; i <= N; i++) {
    long long int ans = INF;
    for (int j = 0; j <= 2500; j++) {
      ans = min(ans, dis[i][j]);
    }
    cout << ans << endl;
  }

  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <iomanip>
#include <iostream>
#include <set>
#include <string>
#include <vector>

using namespace std;

int N, M;
int A[120];
long long int S, B[120], C[120], D[120];
int U[120], V[120];

vector<int> Adj[55];

long long int dis[55][2525];

long long int INF = 99999999999999999;

pair<long long int, pair<int, int>> P;
set<pair<long long int, pair<int, int>>> T;

int COST[55][55];
long long int TIM[55][55];

int main(void) {

  cin >> N >> M >> S;

  for (int i = 1; i <= M; i++) {
    cin >> U[i] >> V[i] >> A[i] >> B[i];
    COST[U[i]][V[i]] = A[i];
    COST[V[i]][U[i]] = A[i];
    TIM[U[i]][V[i]] = B[i];
    TIM[V[i]][U[i]] = B[i];
  }

  for (int i = 1; i <= N; i++) {
    cin >> C[i] >> D[i];
  }

  for (int i = 1; i <= M; i++) {
    Adj[U[i]].push_back(V[i]);
    Adj[V[i]].push_back(U[i]);
  }

  for (int i = 1; i <= N; i++) {
    for (int j = 0; j <= 2500; j++) {
      dis[i][j] = INF;
    }
  }

  S = min(S, (long long int)2500);

  dis[1][S] = 0;
  P.first = 0;
  P.second.first = 1;
  P.second.second = (int)S;
  T.insert(P);

  while (T.size() > 0) {
    auto it = T.begin();
    P = *it;
    T.erase(it);
    long long int d = P.first;
    int K = P.second.first;
    int s = P.second.second;

    // cout << "K= " << K << " s= " << s <<" dis= "<<d<<  endl;

    for (int j = 1;; j++) {
      int s_num;
      if (C[K] * j + s >= 50 * N) {
        s_num = 50 * N;
      } else {
        s_num = (int)(s + C[K] * j);
      }
      if (dis[K][s_num] > dis[K][s] + D[K] * j) {
        dis[K][s_num] = dis[K][s] + D[K] * j;
        P.first = dis[K][s_num];
        P.second.first = K;
        P.second.second = s_num;
        T.insert(P);
      }

      if (s_num >= 50 * N) {
        break;
      }
    }

    for (auto y : Adj[K]) {
      if (s >= COST[K][y]) {
        if (dis[y][s - COST[K][y]] > dis[K][s] + TIM[K][y]) {
          dis[y][s - COST[K][y]] = dis[K][s] + TIM[K][y];
          P.first = dis[K][s] + TIM[K][y];
          P.second.first = y;
          P.second.second = s - COST[K][y];
          T.insert(P);
        }
      }
    }
  }

  for (int i = 2; i <= N; i++) {
    long long int ans = INF;
    for (int j = 0; j <= 2500; j++) {
      ans = min(ans, dis[i][j]);
    }
    cout << ans << endl;
  }

  return 0;
}