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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1 << 30;
class DIJ {
public:
  vvi G;
  int n;
  vvi d; // distance
  vp in;
  DIJ(int size) {
    n = size;
    in = vp(n);
    G = vvi(n);
  }
  void add_ver(int a, int b, int c) { in[a] = pii(b, c); }
  void add_edge(int a, int b) {
    G[a].pb(b);
    G[b].pb(a);
  }
  void dij(int s) {
    d = vvi(n, vi(n + 1, inf));
    d[s][0] = 0; // now oktime ->time
    priority_queue<pip> q;
    q.push(pip(0, pii(s, 0))); // coat now last
    while (!q.empty()) {
      pip p = q.top();
      q.pop();
      int pos = p.second.first;
      int cost = -p.first;
      int nu = p.second.second;
      if (cost > d[pos][nu])
        continue;
      rep(i, G[pos].size()) {
        int to = G[pos][i];
        int nowcost = cost;
        int nownu = nu - 1;
        if (nownu >= 0 && nowcost < d[to][nownu]) {
          d[to][nownu] = nowcost;
          q.push(pip(-nowcost, pii(to, nownu)));
        }
        nowcost = in[pos].first + cost;
        nownu = in[pos].second - 1;
        if (nowcost < d[to][nownu]) {
          d[to][nownu] = nowcost;
          q.push(pip(-nowcost, pii(to, nownu)));
        }
      }
    }
  }
};
int main() {
  int n, m;
  cin >> n >> m;
  DIJ dij(n);
  rep(i, n) {
    int a, b;
    cin >> a >> b;
    dij.add_ver(i, a, b);
  }
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    dij.add_edge(a, b);
  }
  dij.dij(0);
  int out = inf;
  rep(i, n + 1) out = min(out, dij.d[n - 1][i]);
  cout << out << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1 << 30;
// aoj 0596
vi visit, d;
vvi G;
void dfs(int a, int b) {
  d[a] = b;
  rep(i, G[a].size()) {
    int to = G[a][i];
    if (visit[to] == false && d[to] > b + 1) {
      visit[a] = true;
      d[to] = b + 1;
      dfs(to, b + 1);
      visit[a] = false;
    }
  }
}
struct edge {
  int to, cost;
};
int main() {
  int n, m;
  cin >> n >> m;
  vp in(n);
  rep(i, n) cin >> in[i].first >> in[i].second;
  G = vvi(n);
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    G[a].pb(b);
    G[b].pb(a);
  }
  vector<vector<edge>> g(n);
  rep(i, n) {
    visit = vi(n);
    d = vi(n, inf);
    visit[i] = true;
    dfs(i, 0);
    rep(j, n) if (d[j] <= in[i].second) g[i].pb(edge{j, in[i].first});
  }
  d = vi(n, inf);
  d[0] = 0;
  priority_queue<pii> q;
  q.push(pii(0, 0));
  while (!q.empty()) {
    pii p = q.top();
    q.pop();
    int pos = p.second, cost = -p.first;
    if (cost > d[pos])
      continue;
    rep(i, g[pos].size()) {
      edge e = g[pos][i];
      int to = e.to;
      int nowcost = cost + e.cost;
      if (nowcost < d[to]) {
        d[to] = nowcost;
        q.push(pii(-d[to], to));
      }
    }
  }
  cout << d[n - 1] << endl;
}