alexjercan/bugnet huggingface dataset:
Problem p00519 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <queue>
#include <vector>
#define INF (1 << 25)
using namespace std;

typedef pair<int, int> P;

struct range {
  int reg, cost;
};

range co[5050];

map<int, vector<int>> es;
int n_map[5050][5050];

bool used[5050];
void bfs(int s) {

  queue<P> que;
  que.push(P(s, 0));
  while (que.size()) {
    P p = que.front();
    que.pop();
    if (n_map[s][p.first] != INF)
      continue;
    if (p.second > co[s].reg)
      break;
    n_map[s][p.first] = co[s].cost;
    used[p.first] = true;
    for (int i = 0; i < es[p.first].size(); i++) {
      que.push(P(es[p.first][i], p.second + 1));
    }
  }
}

int n, k;

int d[5050];
int dijkstra(int s) {
  fill(used, used + 5050, false);
  fill(d, d + 5050, INF);
  d[s] = 0;
  while (true) {
    int v = -1;
    for (int u = 1; u < 5050; u++) {
      if (!used[u] && (v == -1 || d[u] < d[v]))
        v = u;
    }
    if (v == -1)
      break;

    used[v] = true;
    for (int u = 1; u < 5050; u++) {
      d[u] = min(d[u], d[v] + n_map[v][u]);
    }
  }
  return d[n];
}
int main() {
  fill((int *)n_map, (int *)n_map + 5050 * 5050, INF);
  cin >> n >> k;

  for (int i = 1; i <= n; i++) {
    cin >> co[i].cost >> co[i].reg;
  }
  for (int i = 0; i < k; i++) {
    int a, b;
    cin >> a >> b;
    es[a].push_back(b);
    es[b].push_back(a);
  }

  for (int i = 1; i <= n; i++)
    bfs(i);

  cout << dijkstra(1) << endl;

  // for(int i=0;i<10;i++)cout<<d[i]<<endl;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <vector>
#define INF (1 << 25)
using namespace std;

typedef pair<int, int> P;

struct range {
  int reg, cost;
};

range co[5050];

map<int, vector<int>> es;
int n_map[5050][5050];

bool used[5050];
void bfs(int s) {

  fill(used, used + 5050, false);
  queue<P> que;
  que.push(P(s, 0));
  while (que.size()) {
    P p = que.front();
    que.pop();
    if (p.second > co[s].reg)
      break;
    n_map[s][p.first] = co[s].cost;
    used[p.first] = true;
    for (int i = 0; i < es[p.first].size(); i++) {
      if (!used[es[p.first][i]])
        que.push(P(es[p.first][i], p.second + 1));
    }
  }
}

int n, k;

int d[5050];
int dijkstra(int s) {
  fill(used, used + 5050, false);
  fill(d, d + 5050, INF);
  d[s] = 0;
  while (true) {
    int v = -1;
    for (int u = 1; u < 5050; u++) {
      if (!used[u] && (v == -1 || d[u] < d[v]))
        v = u;
    }
    if (v == -1)
      break;

    used[v] = true;
    for (int u = 1; u < 5050; u++) {
      d[u] = min(d[u], d[v] + n_map[v][u]);
    }
  }
  return d[n];
}
int main() {
  fill((int *)n_map, (int *)n_map + 5050 * 5050, INF);
  cin >> n >> k;

  for (int i = 1; i <= n; i++) {
    cin >> co[i].cost >> co[i].reg;
  }
  for (int i = 0; i < k; i++) {
    int a, b;
    cin >> a >> b;
    es[a].push_back(b);
    es[b].push_back(a);
  }

  for (int i = 1; i <= n; i++)
    bfs(i);

  cout << dijkstra(1) << endl;

  // for(int i=0;i<10;i++)cout<<d[i]<<endl;
}
Problem p00519 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef int ll;
typedef pair<ll, ll> P;
typedef double db;
const ll INF = 0xfffffff;
ll n, k;
ll c[5000], r[5000];
ll d[5000];
ll a[10000], b[10000];
ll tmax[5000];
vector<ll> G[5000];
priority_queue<P, vector<P>, greater<P>> q;
queue<P> q2;
int main() {
  scanf("%d%d", &n, &k);
  for (ll i = 0; i < n; i++)
    scanf("%d%d", &c[i], &r[i]);
  for (ll i = 0; i < k; i++) {
    scanf("%d%d", &a[i], &b[i]);
    a[i]--;
    b[i]--;
    G[a[i]].push_back(b[i]);
    G[b[i]].push_back(a[i]);
  }
  fill(d, d + n, INF);
  d[0] = 0;
  tmax[0] = 0;
  q.push(P(d[0] + c[0], 0));
  while (!q.empty()) {
    P p = q.top();
    q.pop();
    if (d[p.second] + c[p.second] < p.first)
      continue;
    q2.push(P(p.second, r[p.second]));
    while (!q2.empty()) {
      P t = q2.front();
      q2.pop();
      if (d[t.first] > p.first) {
        d[t.first] = p.first;
        tmax[t.first] = t.second;
        q.push(P(d[t.first] + c[t.first], t.first));
      }
      if (t.second)
        for (ll i = 0; i < G[t.first].size(); ++i)
          if (d[G[t.first][i]] > p.first || tmax[G[t.first][i]] < t.second - 1)
            q2.push(P(G[t.first][i], t.second - 1));
    }
  }
  printf("%d\\n", d[n - 1]);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
using namespace std;
typedef int ll;
typedef pair<ll, ll> P;
typedef double db;
const ll INF = 0xfffffff;
ll n, k;
ll c[5000], r[5000];
ll d[5000];
ll a[10000], b[10000];
ll tmax[5000];
vector<ll> G[5000];

int main() {
  scanf("%d%d", &n, &k);
  for (ll i = 0; i < n; i++)
    scanf("%d%d", &c[i], &r[i]);
  for (ll i = 0; i < k; i++) {
    scanf("%d%d", &a[i], &b[i]);
    a[i]--;
    b[i]--;
    G[a[i]].push_back(b[i]);
    G[b[i]].push_back(a[i]);
  }
  fill(d, d + n, INF);
  priority_queue<P, vector<P>, greater<P>> q;
  queue<P> q2;
  d[0] = 0;
  tmax[0] = 0;
  q.push(P(d[0] + c[0], 0));
  while (!q.empty()) {
    P p = q.top();
    q.pop();
    if (d[p.second] + c[p.second] < p.first)
      continue;
    q2.push(P(p.second, r[p.second]));
    while (!q2.empty()) {
      P t = q2.front();
      q2.pop();
      ll x = t.first;
      if (d[x] > p.first) {
        d[x] = p.first;
        tmax[x] = t.second;
        q.push(P(d[x] + c[x], x));
      }
      if (t.second)
        for (ll i = 0; i < G[x].size(); i++)
          if (d[G[x][i]] > p.first || tmax[G[x][i]] < t.second - 1)
            q2.push(P(G[x][i], t.second - 1));
    }
  }
  printf("%d\\n", d[n - 1]);
}
Problem p00519 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <cstring>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>
#define INF 900000000

using namespace std;

int n, k, c[5010], r[5010], cost[5010];
vector<int> road[5010];
vector<pair<int, int>> adj[5010];
bool done[5010];

void bfs(int num) {
  queue<pair<int, int>> que;
  que.push({num, 0});
  while (!que.empty()) {
    int a = que.front().first;
    int b = que.front().second;
    que.pop();
    if (done[a])
      continue;
    done[a] = true;
    for (auto u : road[a]) {
      if (!done[u] && b + 1 <= r[num]) {
        adj[num].push_back({u, c[num]});
        que.push({u, b + 1});
      }
    }
  }
}

int dijkstra() {
  for (int i = 0; i < 5010; i++)
    cost[i] = INF;
  cost[1] = 0;
  priority_queue<pair<int, int>> que;
  memset(done, false, sizeof(done));
  que.push({0, 1});
  while (!que.empty()) {
    int a = que.top().second;
    que.pop();
    if (done[a])
      continue;
    done[a] = true;
    for (auto u : adj[a]) {
      int b = u.first, w = u.second;
      if (cost[b] > cost[a] + w) {
        cost[b] = cost[a] + w;
        que.push({-cost[b], b});
      }
    }
  }
  return cost[n];
}

int main() {
  int a, b;
  cin >> n >> k;
  memset(done, false, sizeof(false));
  for (int i = 1; i <= n; i++)
    cin >> c[i] >> r[i];
  for (int i = 0; i < k; i++) {
    cin >> a >> b;
    road[a].push_back(b);
    road[b].push_back(a);
  }
  for (int i = 1; i <= n; i++) {
    memset(done, false, sizeof(done));
    bfs(i);
  }
  cout << dijkstra() << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

#include <cstring>
#include <iostream>
#include <queue>
#include <set>
#include <utility>
#include <vector>
#define INF 900000000

using namespace std;

int n, k, c[5010], r[5010], cost[5010];
vector<int> road[5010];
vector<pair<int, int>> adj[5010];
bool done[5010];
pair<int, bool> alr[5010];
set<int> st;

void dfs(int dep, int lim, int num) {
  if (dep > r[lim])
    return;
  alr[num].second = true;
  alr[num].first = dep;
  for (auto u : road[num]) {
    if (!st.count(u)) {
      st.insert(u);
      adj[lim].push_back({u, c[lim]});
    }
    if (alr[u].second = false || alr[u].first > dep)
      dfs(dep + 1, lim, u);
  }
}

int dijkstra() {
  for (int i = 0; i < 5010; i++)
    cost[i] = INF;
  cost[1] = 0;
  priority_queue<pair<int, int>> que;
  memset(done, false, sizeof(done));
  que.push({0, 1});
  while (!que.empty()) {
    int a = que.top().second;
    que.pop();
    if (done[a])
      continue;
    done[a] = true;
    for (auto u : adj[a]) {
      int b = u.first, w = u.second;
      if (cost[b] > cost[a] + w) {
        cost[b] = cost[a] + w;
        que.push({-cost[b], b});
      }
    }
  }
  return cost[n];
}

int main() {
  int a, b;
  cin >> n >> k;
  for (int i = 1; i <= n; i++)
    cin >> c[i] >> r[i];
  for (int i = 0; i < k; i++) {
    cin >> a >> b;
    road[a].push_back(b);
    road[b].push_back(a);
  }
  for (int i = 1; i <= n; i++) {
    for (int i = 0; i < 5010; i++) {
      alr[i].first = INF;
      alr[i].second = false;
    }
    st.clear();
    dfs(1, i, i);
  }
  cout << dijkstra() << '\\n';
  return 0;
}
Problem p00519 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <climits>
#include <cstdio>
#include <iostream>
#include <queue>
#include <set>
#include <vector>
using namespace std;
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  vector<int> c(n), r(n);
  for (int i = 0; i < n; i++)
    scanf("%d%d", &c[i], &r[i]);
  vector<vector<int>> path(n, vector<int>());
  for (int x, y, i = 0; i < k; i++) {
    scanf("%d%d", &x, &y);
    path[x - 1].push_back(y - 1);
    path[y - 1].push_back(x - 1);
  }
  vector<vector<int>> route(n, vector<int>());
  queue<pair<int, int>> q;
  for (int i = 0; i < n; i++) {
    vector<bool> visit(n, false);
    visit[i] = true;
    q.push(make_pair(i, r[i]));
    for (; q.size();) {
      int now = q.front().first;
      int rem = q.front().second;
      q.pop();
      for (int j = 0; j < path[now].size(); j++) {
        if (!visit[path[now][j]]) {
          visit[path[now][j]] = true;
          route[i].push_back(path[now][j]);
          if (rem > 1) {
            q.push(make_pair(path[now][j], rem - 1));
          }
        }
      }
    }
  }
  priority_queue<pair<int, int>> pq;
  vector<int> dist(n, INT_MAX);
  pq.push(make_pair(dist[0] = 0, 0));
  for (; pq.size();) {
    int cost = pq.top().first;
    int now = pq.top().second;
    pq.pop();
    for (int i = 0; i < route[now].size(); i++) {
      int to = route[now][i];
      if (dist[to] > c[now] + cost) {
        pq.push(make_pair(dist[to] = c[now] + cost, to));
      }
    }
  }
  cout << dist[n - 1] << endl;
}

Error: TLE

Fixed submission:

#include <climits>
#include <iostream>
#include <map>
#include <queue>
#include <vector>
using namespace std;

int main() {
  int n, k;
  cin >> n >> k;
  priority_queue<pair<int, int>, vector<pair<int, int>>, less<pair<int, int>>>
      q, q2;
  vector<int> c(n), r(n), ans(n, INT_MAX), ans2(n, INT_MAX);
  vector<bool> p(n * n, false);
  map<int, vector<int>> dist;
  for (int i = 0; i < n; i++) {
    cin >> c[i] >> r[i];
  }
  for (int i = 0; i < k; i++) {
    int a, b;
    cin >> a >> b;
    p[(a - 1) + n * (b - 1)] = p[(b - 1) + n * (a - 1)] = true;
  }
  q.push(make_pair(0, 0));
  for (; q.size();) {
    int cost = q.top().first;
    int now = q.top().second;
    q.pop();
    if (cost < ans[now]) {
      ans[now] = cost;
      if (dist.find(now) == dist.end()) {
        ans2.assign(n, INT_MAX);
        q2.push(make_pair(0, now));
        for (; q2.size();) {
          int cost2 = q2.top().first;
          int now2 = q2.top().second;
          q2.pop();
          if (cost2 < ans2[now2]) {
            ans2[now2] = cost2;
            for (int i = 0; i < n; i++) {
              if (p[i + n * now2] && 1 + cost2 < ans2[i]) {
                q2.push(make_pair(1 + cost2, i));
              }
            }
          }
        }
        dist[now] = ans2;
      }
      for (int i = 0; i < n; i++) {
        if (dist[now][i] <= r[now] && c[now] + cost < ans[i]) {
          q.push(make_pair(c[now] + cost, i));
        }
      }
    }
  }
  cout << ans[n - 1] << endl;
}
Problem p00519 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <cstdio>
#include <functional>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef pair<int, int> mp;
typedef pair<int, mp> mmp;

vector<int> ver[5005];
int rode[5005] = {};
int tdat[5005][2];
int n, k;
int main(void) {
  scanf("%d%d", &n, &k);
  rep(i, n) { scanf("%d%d", &(tdat[i + 1][0]), &(tdat[i + 1][1])); }
  rep(i, k) {
    int a, b;
    scanf("%d%d", &a, &b);
    ver[a].push_back(b);
    ver[b].push_back(a);
  }

  priority_queue<mmp, vector<mmp>, greater<mmp>> que;
  que.push(mmp(0, mp(1, 0)));
  while (!que.empty()) {
    mmp pa = que.top();
    que.pop();
    int co = pa.first, no = pa.second.first, nt = pa.second.second;
    if (no == n) {
      printf("%d\\n", co);
      return 0;
    }
    if (nt != 0)
      rep(i, ver[no].size()) que.push(mmp(co, mp(ver[no][i], nt - 1)));
    if (rode[no])
      continue;
    rode[no] = 1;
    que.push(mmp(co + tdat[no][0], mp(no, tdat[no][1])));
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <functional>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
typedef pair<int, int> mp;
typedef pair<int, mp> mmp;

vector<int> ver[5005];
int tdat[5005][2];
int n, k;
int main(void) {
  scanf("%d%d", &n, &k);
  rep(i, n) { scanf("%d%d", &(tdat[i + 1][0]), &(tdat[i + 1][1])); }
  rep(i, k) {
    int a, b;
    scanf("%d%d", &a, &b);
    ver[a].push_back(b);
    ver[b].push_back(a);
  }

  priority_queue<mmp, vector<mmp>, greater<mmp>> que;
  que.push(mmp(0, mp(1, 0)));
  while (!que.empty()) {
    mmp pa = que.top();
    que.pop();
    int co = pa.first, no = pa.second.first, nt = pa.second.second;
    if (no == n) {
      printf("%d\\n", co);
      return 0;
    }
    que.push(mmp(co + tdat[no][0], mp(no, tdat[no][1])));
    if (nt != 0)
      rep(i, ver[no].size()) que.push(mmp(co, mp(ver[no][i], nt - 1)));
  }
  return 0;
}
Problem p00519 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <deque>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORE(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(i, b) FOR(i, 0, b)
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pr;
typedef vector<pr> vpr;

const int INF = 1 << 30;
int longs[5001];
int price[5001];
vi edge[5001];
int cost[5001];
bool able[5001][5001];
priority_queue<pr, vpr, greater<pr>> pq;
void bfs(int i) {
  queue<pr> qu;
  able[i][i] = true;
  qu.push(pr(i, 0));
  while (!qu.empty()) {
    int f = qu.front().first, times = qu.front().second;
    if (times >= longs[i])
      break;
    qu.pop();
    REP(j, edge[f].size())
    if (!able[i][edge[f][j]]) {
      able[i][edge[f][j]] = true;
      qu.push(pr(edge[f][j], times + 1));
    }
  }
}
int main() {
  int N, K, a, b;
  REP(i, 5001)
  cost[i] = INF;
  cin >> N >> K;
  FORE(i, 1, N)
  cin >> price[i] >> longs[i];
  REP(i, K) {
    cin >> a >> b;
    edge[a].push_back(b);
    edge[b].push_back(a);
  }
  FORE(i, 1, N)
  bfs(i);
  pq.push(pr(0, 1));
  cost[1] = 0;
  while (!pq.empty()) {
    int need = pq.top().first, num = pq.top().second;
    pq.pop();
    if (cost[num] != need)
      continue;
    FORE(i, 1, N) {
      if (able[num][i])
        if (cost[i] > price[num] + need) {
          cost[i] = price[num] + need;
          pq.push(pr(cost[i], i));
        }
    }
  }
  cout << cost[N] << endl;
  // your code goes here
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <deque>
#include <iostream>
#include <queue>
#include <string>
#include <vector>
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define FORE(i, a, b) for (int i = (a); i <= (b); i++)
#define REP(i, b) FOR(i, 0, b)
using namespace std;
typedef vector<int> vi;
typedef pair<int, int> pr;
typedef vector<pr> vpr;

const int INF = 1 << 30;
int longs[5001];
int price[5001];
vi edge[5001];
int cost[5001];
bool able[5001][5001];
priority_queue<pr, vpr> pq;
void bfs(int i) {
  queue<pr> qu;
  able[i][i] = true;
  qu.push(pr(i, 0));
  while (!qu.empty()) {
    int f = qu.front().first, times = qu.front().second;
    if (times >= longs[i])
      break;
    qu.pop();
    REP(j, edge[f].size())
    if (!able[i][edge[f][j]]) {
      able[i][edge[f][j]] = true;
      qu.push(pr(edge[f][j], times + 1));
    }
  }
}
int main() {
  int N, K, a, b;
  REP(i, 5001)
  cost[i] = INF;
  cin >> N >> K;
  FORE(i, 1, N)
  cin >> price[i] >> longs[i];
  REP(i, K) {
    cin >> a >> b;
    edge[a].push_back(b);
    edge[b].push_back(a);
  }
  FOR(i, 1, N)
  bfs(i);
  pq.push(pr(0, 1));
  cost[1] = 0;
  while (!pq.empty()) {
    int need = pq.top().first, num = pq.top().second;
    pq.pop();
    if (cost[num] != need)
      break;
    FORE(i, 1, N) {
      if (able[num][i])
        if (cost[i] > price[num] + need) {
          cost[i] = price[num] + need;
          pq.push(pr(cost[i], i));
        }
    }
  }
  cout << cost[N] << endl;
  // your code goes here
  return 0;
}
Problem p00519 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
#define F first
#define S second
#define MP make_pair
struct edge {
  int to, cost, cnt;
  edge(int a, int b, int c) {
    to = a;
    cost = b;
    cnt = c;
  }
};
int N, K;
int c[5000], r[5000];
int flag2[5001];
vector<int> G[5001], G2[5001];
priority_queue<P, vector<P>, greater<P>> pq;
void bfs(int s) {
  queue<P> que;
  que.push(MP(s, 0));
  bool flag[5001] = {};
  flag[s] = true;
  while (!que.empty()) {
    P now = que.front();
    que.pop();
    if (now.S == r[s])
      continue;
    for (int i = 0; i < G[now.F].size(); ++i) {
      if (!flag[G[now.F][i]]) {
        flag[G[now.F][i]] = true;
        que.push(MP(G[now.F][i], now.S + 1));
        G2[s].push_back(G[now.F][i]);
      }
    }
  }
}
void dijkstra(int s) {
  pq.push(MP(0, 0));
  while (!pq.empty()) {
    P now = pq.top();
    pq.pop();
    for (int i = 0; i < G2[now.S].size(); ++i) {
      if (flag2[G2[now.S][i]] > now.F + c[now.S]) {
        flag2[G2[now.S][i]] = now.F + c[now.S];
        pq.push(MP(now.F + c[now.S], G2[now.S][i]));
      }
    }
  }
}
int main() {
  cin >> N >> K;
  for (int i = 0; i < N; ++i)
    cin >> c[i] >> r[i];
  for (int i = 0; i < K; ++i) {
    int in1, in2;
    cin >> in1 >> in2;
    --in1;
    --in2;
    G[in1].push_back(in2);
    G[in2].push_back(in1);
  }
  fill(flag2 + 1, flag2 + N, INT_MAX);
  for (int i = 0; i < N; ++i)
    bfs(i);
  dijkstra(0);
  cout << flag2[N - 1] << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
#define F first
#define S second
#define MP make_pair
struct edge {
  int to, cost, cnt;
  edge(int a, int b, int c) {
    to = a;
    cost = b;
    cnt = c;
  }
};
int N, K;
int c[5000], r[5000];
int flag2[5001];
vector<int> G[5001];
priority_queue<P> pq;
void bfs(int s) {
  queue<P> que;
  que.push(MP(s, 0));
  bool flag[5001] = {};
  flag[s] = true;
  while (!que.empty()) {
    P now = que.front();
    que.pop();
    if (now.S == r[s])
      continue;
    for (int i = 0; i < G[now.F].size(); ++i) {
      if (!flag[G[now.F][i]]) {
        flag[G[now.F][i]] = true;
        que.push(MP(G[now.F][i], now.S + 1));
        G[s].push_back(G[now.F][i]);
      }
    }
  }
}
void dijkstra(int s) {
  pq.push(MP(0, 0));
  while (!pq.empty()) {
    P now = pq.top();
    pq.pop();
    for (int i = 0; i < G[now.S].size(); ++i) {
      if (flag2[G[now.S][i]] > now.F + c[now.S]) {
        flag2[G[now.S][i]] = now.F + c[now.S];
        pq.push(MP(now.F + c[now.F], G[now.S][i]));
      }
    }
  }
}
int main() {
  cin >> N >> K;
  for (int i = 0; i < N; ++i)
    cin >> c[i] >> r[i];
  for (int i = 0; i < K; ++i) {
    int in1, in2;
    cin >> in1 >> in2;
    --in1;
    --in2;
    G[in1].push_back(in2);
    G[in2].push_back(in1);
  }
  fill(flag2 + 1, flag2 + N, INT_MAX);
  for (int i = 0; i < N; ++i)
    bfs(i);
  dijkstra(0);
  cout << flag2[N - 1] << endl;
}
Problem p00519 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int N, K;
int C[5555], R[5555];
vector<int> G[5555];
struct state {
  int id, c;
  bool operator<(const state &st) const { return c > st.c; }
};
vector<int> nG[5555];

void init() {
  for (int i = 0; i < N; i++) {
    G[i].clear();
    nG[i].clear();
  }
}

void bfs(int st) {
  queue<state> q;
  vector<int> h(N, -1);
  q.push((state){st, 0});
  h[st] = 0;
  while (!q.empty()) {
    state p = q.front();
    q.pop();
    if (p.c >= R[st])
      break;
    int nc = p.c + 1;
    for (int to : G[p.id]) {
      if (h[to] == -1) {
        h[to] = nc;
        q.push((state){to, nc});
        nG[st].push_back(to);
      }
    }
  }
}

const int INF = 1000000009;
int dijk(int st) {
  priority_queue<state> q;
  vector<int> h(N, INF);
  q.push((state){st, 0});
  h[st] = 0;
  while (!q.empty()) {
    state p = q.top();
    q.pop();
    if (h[p.id] < p.c)
      continue;
    if (p.id == N - 1)
      return h[p.id];
    int nc = p.c + C[p.id];
    for (int to : nG[p.id]) {
      if (h[to] > nc) {
        h[to] = nc;
        q.push((state){to, nc});
      }
    }
  }
  return h[N - 1];
}

int main() {
  while (cin >> N >> K && (N || K)) {
    init();
    for (int i = 0; i < N; i++) {
      cin >> C[i] >> R[i];
    }
    for (int i = 0; i < K; i++) {
      int a, b;
      cin >> a >> b;
      --a;
      --b;
      G[a].push_back(b);
      G[b].push_back(a);
    }
    for (int i = 0; i < N; i++)
      bfs(i);
    cout << dijk(0) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int N, K;
int C[5555], R[5555];
vector<int> G[5555];
struct state {
  int id, c;
  bool operator<(const state &st) const { return c > st.c; }
};
vector<int> nG[5555];

void bfs(int st) {
  queue<state> q;
  vector<int> h(N, -1);
  q.push((state){st, 0});
  h[st] = 0;
  while (!q.empty()) {
    state p = q.front();
    q.pop();
    if (p.c == R[st])
      continue;
    for (int to : G[p.id]) {
      if (h[to] == -1) {
        h[to] = p.c + 1;
        q.push((state){to, p.c + 1});
        nG[st].push_back(to);
      }
    }
  }
}

const int INF = 1000000009;
int dijk(int st) {
  priority_queue<state> q;
  vector<int> h(N, INF);
  q.push((state){st, 0});
  h[st] = 0;
  while (!q.empty()) {
    state p = q.top();
    q.pop();
    if (h[p.id] < p.c)
      continue;
    for (int to : nG[p.id]) {
      int nc = p.c + C[p.id];
      if (h[to] > nc) {
        h[to] = nc;
        q.push((state){to, nc});
      }
    }
  }
  return h[N - 1];
}

int main() {
  cin >> N >> K;
  for (int i = 0; i < N; i++) {
    cin >> C[i] >> R[i];
  }
  for (int i = 0; i < K; i++) {
    int a, b;
    cin >> a >> b;
    --a;
    --b;
    G[a].push_back(b);
    G[b].push_back(a);
  }

  for (int i = 0; i < N; i++)
    bfs(i);
  cout << dijk(0) << endl;
  return 0;
}