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

#include <iostream>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

#define INF (1 << 30)

int main() {
  int N, K, A, B;

  cin >> N >> K;

  vector<int> C(N + 1);
  vector<int> R(N + 1);

  vector<bool> S(N + 1, false);
  vector<int> D(N + 1, INF);
  vector<int> P(N + 1, INF);

  vector<vector<int>> M1(N + 1, vector<int>());
  vector<vector<int>> M2(N + 1, vector<int>(N + 1, INF));

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

  for (int i = 1; i <= K; i++) {
    cin >> A >> B;

    M1[A].push_back(B);
    M1[B].push_back(A);
  }

  for (int i = 1; i <= N; i++) {
    if (R[i] < N - 1) {
      queue<pair<int, int>> que;

      pair<int, int> start;

      start.first = i;
      start.second = (short)0;

      que.push(start);

      while (!que.empty() && que.front().second < R[i]) {
        pair<int, int> p1 = que.front();
        que.pop();

        for (int j = 0; j < M1[p1.first].size(); j++) {
          if (M2[i][M1[p1.first][j]] == INF) {
            pair<int, int> p2;

            p2.first = M1[p1.first][j];
            p2.second = p1.second + 1;

            que.push(p2);

            M2[i][M1[p1.first][j]] = C[i];
          }
        }
      }
    } else {
      for (int j = 1; j <= N; j++) {
        if (i != j) {
          M2[i][j] = C[i];
        }
      }
    }
  }

  D[1] = 0;
  P[1] = -1;

  while (true) {
    int mincost = INF;

    int u = -1;

    for (int i = 1; i <= N; i++) {
      if (S[i] == false && D[i] < mincost) {
        mincost = D[i];

        u = i;
      }
    }

    if (mincost == INF) {
      break;
    }

    S[u] = true;

    for (int v = 1; v <= N; v++) {
      if (S[v] == false && M2[u][v] != INF) {
        if (D[u] + M2[u][v] < D[v]) {
          D[v] = D[u] + M2[u][v];
          P[v] = u;
        }
      }
    }
  }

  cout << D[N] << endl;

  return 0;
}

// Time : O ( N ^ 2 )

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

#define INF (1 << 30)

int main() {
  int N, K, A, B;

  cin >> N >> K;

  vector<int> C(N + 1);
  vector<int> R(N + 1);

  vector<bool> S(N + 1, false);
  vector<int> D(N + 1, INF);
  vector<int> P(N + 1, INF);

  vector<vector<bool>> M1(N + 1, vector<bool>(N + 1, false));
  vector<vector<int>> M2(N + 1, vector<int>(N + 1, INF));

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

  for (int i = 1; i <= K; i++) {
    cin >> A >> B;

    M1[A][B] = true;
    M1[B][A] = true;
  }

  for (int i = 1; i <= N; i++) {
    if (R[i] < N - 1) {
      queue<pair<int, int>> que;

      pair<int, int> start;

      start.first = i;
      start.second = (short)0;

      que.push(start);

      while (!que.empty() && que.front().second < R[i]) {
        pair<int, int> p1 = que.front();
        que.pop();

        for (int j = 1; j <= N; j++) {
          if (M1[p1.first][j] == true && M2[i][j] == INF) {
            pair<int, int> p2;

            p2.first = j;
            p2.second = p1.second + 1;

            que.push(p2);

            M2[i][j] = C[i];
          }
        }
      }
    } else {
      for (int j = 1; j <= N; j++) {
        if (i != j) {
          M2[i][j] = C[i];
        }
      }
    }
  }

  D[1] = 0;
  P[1] = -1;

  while (true) {
    int mincost = INF;

    int u = -1;

    for (int i = 1; i <= N; i++) {
      if (S[i] == false && D[i] < mincost) {
        mincost = D[i];

        u = i;
      }
    }

    if (mincost == INF) {
      break;
    }

    S[u] = true;

    for (int v = 1; v <= N; v++) {
      if (S[v] == false && M2[u][v] != INF) {
        if (D[u] + M2[u][v] < D[v]) {
          D[v] = D[u] + M2[u][v];
          P[v] = u;
        }
      }
    }
  }

  cout << D[N] << endl;

  return 0;
}

// Time : O ( N ^ 2 )