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

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
#define INF 1000000007
typedef pair<int, int> P;

struct edge {
  int to, cost;
};
int d[5005];
P cst[5005];
bool used[5005];

vector<edge> graf[5005];
vector<edge> G[5005];

int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    cst[i] = P(a, b);
  }
  for (int i = 0; i < k; i++) {
    int a, b;
    cin >> a >> b;
    graf[a - 1].push_back({b - 1, 1});
    graf[b - 1].push_back({a - 1, 1});
  }

  for (int ia = 0; ia < n; ia++) { // bfs
    queue<P> q;
    fill(d, d + n, INF);
    d[ia] = 0;
    q.push(P(0, ia));

    while (q.size()) {
      P p = q.front();
      q.pop();
      int v = p.second;
      if (d[v] < p.first)
        continue;
      for (int i = 0; i < graf[v].size(); i++) {
        edge e = graf[v][i];
        if (d[e.to] > d[v] + e.cost) {
          d[e.to] = d[v] + e.cost;
          q.push(P(d[e.to], e.to));
        }
      }
    }
    for (int i = 0; i < n; i++) {
      if (i == ia) {
        G[ia].push_back({i, 0});
        continue;
      }
      if (d[i] > cst[ia].second)
        continue;
      G[ia].push_back({i, cst[ia].first});
    }
  }

  // dijkstra ElogV=36989
  priority_queue<P, vector<P>, greater<P>> Q;
  fill(d, d + n, INF);
  d[0] = 0;
  Q.push(P(0, 0));

  while (Q.size()) {
    P p = Q.top();
    Q.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        Q.push(P(d[e.to], e.to));
      }
    }
  }

  cout << d[n - 1] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
#define INF 1000000007
typedef pair<int, int> P;

struct edge {
  int to, cost;
};
int graf[5005][5005];
int d[5005];
P cst[5005];
bool used[5005];

vector<edge> G[5005];

int main() {
  int n, k;
  cin >> n >> k;
  for (int i = 0; i < n; i++) {
    int a, b;
    cin >> a >> b;
    cst[i] = P(a, b);
  }
  for (int i = 0; i < k; i++) {
    int a, b;
    cin >> a >> b;
    graf[a - 1][b - 1] = 1;
    graf[b - 1][a - 1] = 1;
  }

  for (int ia = 0; ia < n; ia++) { // bfs
    fill(d, d + n, INF);
    queue<int> Q;
    Q.push(ia);
    d[ia] = 0;
    while (Q.size()) {
      int a = Q.front();
      Q.pop();
      for (int i = 0; i < n; i++) {
        if (!graf[a][i])
          continue;
        if (d[i] != INF)
          continue;
        d[i] = d[a] + 1;
        Q.push(i);
      }
    }

    for (int i = 0; i < n; i++) {
      if (ia == i) {
        G[ia].push_back({i, 0});
        continue;
      }
      if (d[i] > cst[ia].second)
        continue;
      G[ia].push_back({i, cst[ia].first});
    }
  }

  // dijkstra ElogV=36989
  priority_queue<P, vector<P>, greater<P>> Q;
  fill(d, d + n, INF);
  d[0] = 0;
  Q.push(P(0, 0));

  while (Q.size()) {
    P p = Q.top();
    Q.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        Q.push(P(d[e.to], e.to));
      }
    }
  }

  cout << d[n - 1] << endl;
  return 0;
}