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

#include <bits/stdc++.h>
using namespace std;

const int inf = 1e9 + 9;
const int max_n = 5000;
typedef pair<int, int> P;

struct edge {
  int to, cost;
};

int n, k;
int V;
vector<edge> G[max_n];
vector<int> g[max_n];

int d[max_n];
int c[max_n], r[max_n];
bool f[max_n];

void dijkstra() {
  priority_queue<P, vector<P>, greater<P>> q;
  fill(d, d + n, inf);
  d[0] = 0;
  q.push(P(0, 0));

  while (!q.empty()) {
    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));
      }
    }
  }
}

void bfs(int R, int v) {
  if (R == 0)
    return;
  if (f[v] >= R)
    return;
  f[v] = R;

  edge e;
  e.to = v;
  e.cost = c[V];
  G[V].push_back(e);

  for (int i = 0; i < g[v].size(); i++) {
    bfs(R - 1, g[v][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 a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  for (int i = 0; i < n; i++) {
    memset(f, false, sizeof(f));

    queue<P> que;
    que.push(P(i, r[i] + 1));
    while (!que.empty()) {
      P p = que.front();
      que.pop();
      int v = p.first;
      if (p.second == 0)
        continue;
      if (f[v])
        continue;
      f[v] = true;

      edge e;
      e.to = v;
      e.cost = c[i];
      if (i != v)
        G[i].push_back(e);
      for (int j = 0; j < g[v].size(); j++) {
        que.push(P(g[v][j], p.second - 1));
      }
    }
  }

  dijkstra();

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

Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

const int inf = 1e9 + 9;
const int max_n = 5000;
typedef pair<int, int> P;

struct edge {
  int to, cost;
};

int n, k;
int V;
vector<edge> G[max_n];
vector<int> g[max_n];

int d[max_n];
int c[max_n], r[max_n];
int f[max_n];

void dijkstra() {
  priority_queue<P, vector<P>, greater<P>> q;
  fill(d, d + n, inf);
  d[0] = 0;
  q.push(P(0, 0));

  while (!q.empty()) {
    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));
      }
    }
  }
}

void bfs(int R, int v) {
  if (R == 0)
    return;
  if (f[v] >= R)
    return;
  f[v] = R;

  edge e;
  e.to = v;
  e.cost = c[V];
  G[V].push_back(e);

  for (int i = 0; i < g[v].size(); i++) {
    bfs(R - 1, g[v][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 a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }

  for (int i = 0; i < n; i++) {
    memset(f, false, sizeof(f));
    V = i;
    f[i] = r[i] + 1;
    for (int j = 0; j < g[i].size(); j++) {
      bfs(r[i], g[i][j]);
    }
  }

  dijkstra();

  cout << d[n - 1] << endl;
}
Problem p00519 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;
typedef vector<int> Node;
typedef vector<Node> Graph;
typedef pair<int, int> P;

void bfs(int s, int MAX, const Graph &G, vector<int> &res) {
  vector<int> dist(G.size(), -1);
  queue<int> que;
  que.push(s);
  dist[s] = 0;
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    if (dist[u] + 1 > MAX)
      break;
    for (int v : G[u]) {
      if (dist[v] == -1) {
        dist[v] = dist[u] + 1;
        que.push(v);
        res.push_back(v);
      }
    }
  }
}

int main() {
  int N, K;
  while (cin >> N >> K) {
    vector<int> C(N), R(N);
    REP(i, N) cin >> C[i] >> R[i];
    Graph G(N);
    REP(i, K) {
      int a, b;
      cin >> a >> b;
      G[a - 1].push_back(b - 1);
      G[b - 1].push_back(a - 1);
    }
    vector<int> dist(N, INT_MAX);
    priority_queue<P, vector<P>, greater<P>> que;
    que.push(P(C[0], 0));
    dist[0] = 0;
    while (!que.empty()) {
      int u = que.top().second;
      int c = que.top().first;
      que.pop();
      if (u == N - 1)
        break;
      if (dist[u] < c - C[u])
        continue;

      vector<int> next;
      bfs(u, R[u], G, next);
      for (int v : next) {
        if (dist[v] > c) {
          dist[v] = c;
          que.push(P(c + C[v], v));
        }
      }
    }
    cout << dist[N - 1] << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)

using namespace std;
typedef vector<int> Node;
typedef vector<Node> Graph;
typedef pair<int, int> P;

void bfs(int s, int MAX, const Graph &G, vector<int> &res) {
  vector<int> dist(G.size(), -1);
  queue<int> que;
  que.push(s);
  dist[s] = 0;
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    if (dist[u] + 1 > MAX)
      break;
    for (int v : G[u]) {
      if (dist[v] == -1) {
        dist[v] = dist[u] + 1;
        que.push(v);
        res.push_back(v);
      }
    }
  }
}

int main() {
  int N, K;
  while (cin >> N >> K) {
    vector<int> C(N), R(N);
    REP(i, N) cin >> C[i] >> R[i];
    Graph G(N);
    REP(i, N) {
      int a, b;
      cin >> a >> b;
      G[a - 1].push_back(b - 1);
      G[b - 1].push_back(a - 1);
    }
    vector<int> dist(N, INT_MAX);
    priority_queue<P, vector<P>, greater<P>> que;
    que.push(P(C[0], 0));
    dist[0] = 0;
    while (!que.empty()) {
      int u = que.top().second;
      int c = que.top().first;
      que.pop();
      if (u == N - 1)
        break;
      if (dist[u] < c - C[u])
        continue;

      vector<int> next;
      bfs(u, R[u], G, next);
      for (int v : next) {
        if (dist[v] > c) {
          dist[v] = c;
          que.push(P(c + C[v], v));
        }
      }
    }
    cout << dist[N - 1] << endl;
  }
  return 0;
}
Problem p00519 (C++) — buggy submission, verdict: Memory Limit Exceeded

// AOJ 0596
#include <cstdio>
#include <queue>
#include <vector>
#define rep(i, a) for (int i = 0; i != (a); ++i)

const int INF = 1 << 30;

struct State {
  int v, cost, rest;

  State() {}

  State(int v, int c, int r) : v(v), cost(c), rest(r) {}

  bool operator<(const State &st) const { return cost > st.cost; }
};

int N, K;
int C[5000], R[5000];
int d[5000][5001];
std::vector<int> G[5000];

int dijkstra() {
  rep(i, N) rep(j, N + 1) d[i][j] = INF;

  std::priority_queue<State> pque;
  pque.push(State(0, C[0], R[0]));
  while (!pque.empty()) {
    State st = pque.top();
    pque.pop();

    if (st.v == N - 1)
      return st.cost;

    if (st.rest) {
      rep(i, G[st.v].size()) {
        int to = G[st.v][i];

        if (d[to][st.rest - 1] > st.cost) {
          d[to][st.rest - 1] = st.cost;
          pque.push(State(to, st.cost, st.rest - 1));
        }
      }
    }

    if (d[st.v][R[st.v]] > st.cost + C[st.v]) {
      d[st.v][R[st.v]] = st.cost + C[st.v];
      pque.push(State(st.v, st.cost + C[st.v], R[st.v]));
    }
  }

  return -1;
}

int main() {
  scanf("%d%d", &N, &K);
  rep(i, N) scanf("%d%d", C + i, R + i);
  rep(j, K) {
    int A, B;
    scanf("%d%d", &A, &B);
    --A;
    --B;
    G[A].push_back(B);
    G[B].push_back(A);
  }

  printf("%d\\n", dijkstra());

  return 0;
}

Error: MLE

Fixed submission:

// AOJ 0596
#include <cstdio>
#include <functional>
#include <queue>
#include <vector>
#define rep(i, a) for (int i = 0; i != (a); ++i)

const int INF = 1 << 30;

typedef std::pair<int, int> P;

struct edge {
  int from, to, cost;

  edge(int from, int to, int cost) : from(from), to(to), cost(cost) {}
};

int N, K;
std::vector<int> C, R;
std::vector<int> G[5000];
std::vector<edge> G2[5000];
std::vector<bool> used(5000, false);
std::priority_queue<P, std::vector<P>, std::greater<P>> pque;
std::vector<int> d(5000, INF);

int main() {
  scanf("%d%d", &N, &K);
  rep(i, N) {
    int c, r;
    scanf("%d%d", &c, &r);
    C.push_back(c);
    R.push_back(r);
  }
  rep(i, K) {
    int A, B;
    scanf("%d%d", &A, &B);
    --A;
    --B;
    G[A].push_back(B);
    G[B].push_back(A);
  }

  rep(i, N) {
    std::queue<P> que;
    std::fill(used.begin(), used.end(), false);

    for (que.push(P(i, 0)); !que.empty();) {
      P p = que.front();
      que.pop();
      int v = p.first, c = p.second;

      if (c >= R[i])
        break;

      used[v] = true;

      rep(j, G[v].size()) {
        if (!used[G[v][j]]) {
          que.push(P(G[v][j], c + 1));
          G2[i].push_back(edge(i, G[v][j], C[i]));
        }
      }
    }
  }

  d[0] = 0;
  for (pque.push(P(0, 0)); !pque.empty();) {
    P p = pque.top();
    pque.pop();
    int dist = p.first, v = p.second;

    if (d[v] < dist)
      continue;

    rep(i, G2[v].size()) {
      edge &e = G2[v][i];

      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        pque.push(P(d[e.to], e.to));
      }
    }
  }

  printf("%d\\n", d[N - 1]);

  return 0;
}
Problem p00519 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF 999999999

vector<int> pass[6000];
int n, k;
int c[6000];
int r[6000];

int d[6000];
bool used[6000];

void dfs(int v, int u, int f, int end, bool visited[6000]) {
  if (visited[u])
    return;
  visited[u] = true;
  d[u] = min(d[u], d[v] + c[v]);
  if (f >= end)
    return;
  rep(i, pass[u].size()) { dfs(v, pass[u][i], f + 1, end, visited); }
}

int dijk(int s, int g) {
  rep(i, 6000) d[i] = INF;
  rep(i, 6000) used[i] = false;

  d[s] = 0;
  while (1) {
    int v = -1;
    rep(u, n) {
      if (!used[u] && (v == -1 || d[u] < d[v]))
        v = u;
    }
    if (v == -1)
      break;
    used[v] = true;
    rep(i, pass[v].size()) {
      bool visited[6000] = {};
      dfs(v, pass[v][i], 1, r[v], visited);
    }
  }
  return d[g];
}

int main() {
  cin >> n >> k;
  rep(i, n) cin >> c[i] >> r[i];
  rep(i, k) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    pass[a].pb(b);
    pass[b].pb(a);
  }

  cout << dijk(0, n - 1) << endl;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef long long ll;

#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(a) (a).begin(), (a).end()
#define pb push_back
#define INF 999999999

vector<int> pass[6000];
int n, k;
int c[6000];
int r[6000];

int d[6000];
bool used[6000];

void dfs(int v, int u, int f, int end) {
  d[u] = min(d[u], d[v] + c[v]);
  if (f >= end)
    return;
  rep(i, pass[u].size()) { dfs(v, pass[u][i], f + 1, end); }
}

int dijk(int s, int g) {
  rep(i, 6000) d[i] = INF;
  rep(i, 6000) used[i] = false;

  d[s] = 0;
  while (1) {
    int v = -1;
    rep(u, n) {
      if (!used[u] && (v == -1 || d[u] < d[v]))
        v = u;
    }
    if (v == -1)
      break;
    used[v] = true;
    rep(i, pass[v].size()) { dfs(v, pass[v][i], 1, r[v]); }
  }
  return d[g];
}

int main() {
  cin >> n >> k;
  rep(i, n) cin >> c[i] >> r[i];
  rep(i, k) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    pass[a].pb(b);
    pass[b].pb(a);
  }

  cout << dijk(0, n - 1) << endl;
}
Problem p00519 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <utility>
#include <vector>
#define INF 1000000000
using namespace std;
typedef pair<int, int> pa;

int pri[5010], god[5010], dp[5010];
vector<int> pat[5010];

void bfs(int n, set<int> &can) {
  queue<pa> que;
  pa tmp;
  can.clear();
  que.push(pa(0, n));
  while (!que.empty()) {
    tmp = que.front();
    que.pop();
    if (god[n] < tmp.first || can.count(tmp.second))
      continue;
    can.insert(tmp.second);
    for (auto it = pat[tmp.second].begin(); it != pat[tmp.second].end(); it++) {
      que.push(pa(tmp.first + 1, *it));
    }
  }
}

int main() {
  int N, K;
  cin >> N >> K;
  for (int i = 1; i <= N; i++) {
    cin >> pri[i] >> god[i];
  }
  int a, b;
  for (int j = 1; j <= K; j++) {
    cin >> a >> b;
    pat[a].push_back(b);
    pat[b].push_back(a);
  }
  fill(dp, dp + N + 1, INF);

  pa tmp;
  set<int> stmp;
  priority_queue<pa, vector<pa>, greater<pa>> pque;
  pque.push(pa(0, 1));
  while (!pque.empty()) {
    tmp = pque.top();
    pque.pop();
    if (dp[tmp.second] == INF) {
      dp[tmp.second] = tmp.first;
      if (tmp.second == N)
        break;
      bfs(tmp.second, stmp);
      for (auto it = stmp.begin(); it != stmp.end(); it++) {
        if (dp[*it] == INF)
          pque.push(pa(pri[tmp.second] + tmp.first, *it));
      }
    }
  }

  cout << dp[N] << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <set>
#include <utility>
#include <vector>
#define INF 1000000000
using namespace std;
typedef pair<int, short> pa;

int pri[5010], god[5010], dp[5010];
vector<int> pat[5010];
set<int> can[5010];

int main() {
  int N, K;
  cin >> N >> K;
  for (int i = 1; i <= N; i++) {
    cin >> pri[i] >> god[i];
  }
  int a, b;
  for (int j = 1; j <= K; j++) {
    cin >> a >> b;
    pat[a].push_back(b);
    pat[b].push_back(a);
  }
  fill(dp, dp + N + 1, INF);

  queue<pa> que;
  pa tmp;
  for (int i = 1; i <= N; i++) {
    que.push(pa(0, i));
    while (!que.empty()) {
      tmp = que.front();
      que.pop();
      if (god[i] < tmp.first || can[i].count(tmp.second))
        continue;
      can[i].insert(tmp.second);
      for (auto it = pat[tmp.second].begin(); it != pat[tmp.second].end();
           it++) {
        que.push(pa(tmp.first + 1, *it));
      }
    }
  }

  priority_queue<pa, vector<pa>, greater<pa>> pque;
  pque.push(pa(0, 1));
  while (!pque.empty()) {
    tmp = pque.top();
    pque.pop();
    if (dp[tmp.second] == INF) {
      dp[tmp.second] = tmp.first;
      if (tmp.second == N)
        break;
      for (auto it = can[tmp.second].begin(); it != can[tmp.second].end();
           it++) {
        if (dp[*it] == INF)
          pque.push(pa(pri[tmp.second] + tmp.first, *it));
      }
    }
  }

  cout << dp[N] << endl;
  return 0;
}
Problem p00519 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> P;
vector<int> v[5010];
int d[5010], n, m, c[5010], r[5010], vis[5010];
priority_queue<P, vector<P>, greater<P>> Q;
bool done[5010];

void DFS(int x, int cnt, int len, int lenmax) {
  vis[x] = len;
  if (cnt == d[x])
    Q.push(make_pair(cnt, x));
  if (len == lenmax)
    return;
  for (int i = 0; i < v[x].size(); i++) {
    d[v[x][i]] = min(cnt, d[v[x][i]]);
    if (vis[v[x][i]] > len + 1)
      DFS(v[x][i], cnt, len + 1, lenmax);
  }
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> c[i] >> r[i];
    d[i] = 1145141919;
    done[i] = false;
  }
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  Q.push(make_pair(0, 0));
  d[0] = 0;
  while (!Q.empty()) {
    int x = Q.top().first, y = Q.top().second;
    Q.pop(); // y???x??§
    if (done[y])
      continue;
    done[y] = true;
    for (int i = 0; i < n; i++)
      vis[i] = 1145141919;
    DFS(y, x + c[y], 0, r[y]);
  }
  cout << d[n - 1] << endl;
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
typedef pair<int, int> P;
vector<int> v[5010];
int d[5010], n, m, c[5010], r[5010];
priority_queue<P, vector<P>, greater<P>> Q;
bool vis[5010], done[5010];

void DFS(int x, int cnt, int len, int lenmax) {
  vis[x] = true;
  if (len && cnt == d[x])
    Q.push(make_pair(cnt, x));
  if (len == lenmax)
    return;
  for (int i = 0; i < v[x].size(); i++) {
    d[v[x][i]] = min(cnt, d[v[x][i]]);
    DFS(v[x][i], cnt, len + 1, lenmax);
  }
}

int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> c[i] >> r[i];
    d[i] = 1145141919;
    done[i] = false;
  }
  for (int i = 0; i < m; i++) {
    int a, b;
    cin >> a >> b;
    a--, b--;
    v[a].push_back(b);
    v[b].push_back(a);
  }
  Q.push(make_pair(0, 0));
  d[0] = 0;
  while (!Q.empty()) {
    int x = Q.top().first, y = Q.top().second;
    Q.pop(); // y???x??§
    if (done[y])
      continue;
    done[y] = true;
    for (int i = 0; i < n; i++)
      vis[i] = false;
    DFS(y, x + c[y], 0, r[y]);
  }
  cout << d[n - 1] << endl;
  return 0;
}
Problem p00519 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>

#define INF (1 << 28)

using namespace std;

#define MAX_V 12000

struct edge {
  int to;
  int cost;
};

typedef pair<int, int> P; // first??????????????¢???second??????????????????

bool used[MAX_V];
vector<edge> G[MAX_V];
int d[MAX_V];

void shortest_path1(int s, int V) { // V???????????°

  priority_queue<P, vector<P>, greater<P>> que;

  for (int i = 0; i < V; i++) {
    d[i] = INF;
  }
  d[s] = 0;

  que.push(P(0, s));

  while (!que.empty()) {

    P p = que.top();
    que.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] > e.cost + d[v]) {
        d[e.to] = d[v] + e.cost;
        que.push(P(d[e.to], e.to));
      }
    } // ????????????????°???????????????´??°
  }
}

int cost[5001][5001];

int fare[5001];
int range[5001];

void shortest_path2(int s, int V) {
  for (int i = 0; i < V; i++) {
    d[i] = INF;
    used[i] = false;
  }
  d[s] = 0;
  while (true) {

    int add_v = -1;

    for (int u = 0; u < V; u++) {
      if (!used[u] && (add_v == -1 || d[u] < d[add_v])) {
        add_v = u;
      }
    } // ??´??°????????????????±????

    if (add_v == -1) {
      break;
    } // ??´??°???????????????????????´????????????
    used[add_v] = true;

    for (int u = 0; u < V; u++) {
      d[u] = min(d[u], d[add_v] + cost[add_v][u]);
    } // ???????????????????????????????°???????????????´??°
  }
}

int main() {

  int N, K;

  cin >> N >> K;

  for (int i = 0; i < N; i++) {
    cin >> fare[i] >> range[i];
  }

  for (int i = 0; i < K; i++) {
    int num1, num2;
    cin >> num1 >> num2;
    num1--;
    num2--;

    edge e1, e2;
    e1.to = num2;
    e2.to = num1;
    e1.cost = 1;
    e2.cost = 1;
    G[num1].push_back(e1);
    G[num2].push_back(e2);
  }

  for (int i = 0; i < N; i++) {
    shortest_path1(i, N);
    for (int j = 0; j < N; j++) {
      if (d[j] <= range[i]) {
        cost[i][j] = fare[i];
      } else {
        cost[i][j] = INF;
      }
      // cout << cost[i][j] << " ";
    }
    // cout << endl;
  }

  shortest_path2(0, N);

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

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <queue>
#include <vector>

using namespace std;

int a[100000000];

int main() {

  for (int i = 0; i < 100000000; i++) {
    a[i] = i;
  }

  cout << "piyo" << endl;

  return 0;
}
Problem p00519 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
const int inf = INT_MAX;
int dp[5000];
vector<int> ls[5000];
int c[5000], r[5000];
int main() {
  int n, k;
  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--;
    ls[a].push_back(b);
    ls[b].push_back(a);
  }
  for (int i = 0; i < n; i++)
    dp[i] = inf;
  dp[0] = 0;
  priority_queue<pair<int, int>> pq;
  pq.push(make_pair(0, 0));
  while (!pq.empty()) {
    auto tp = pq.top();
    pq.pop();
    int cost = -tp.first, cur = tp.second;
    // cout<<cur<<" "<<cost<<endl;
    if (cost > dp[cur])
      continue;
    if (cur == n - 1) {
      cout << cost << endl;
      return 0;
    }
    cost += c[cur];
    set<int> visited;
    visited.insert(cur);
    set<int> now, next;
    now.insert(cur);
    for (int i = 0; i < r[cur]; i++) {
      for (auto it = now.begin(); it != now.end(); it++) {
        for (int j = 0; j < (int)ls[*it].size(); j++) {
          if (visited.find(ls[*it][j]) == visited.end()) {
            visited.insert(ls[*it][j]);
            next.insert(ls[*it][j]);
            if (dp[ls[*it][j]] > cost) {
              dp[ls[*it][j]] = cost;
              pq.push(make_pair(-cost, ls[*it][j]));
            }
          }
        }
      }
      now = next;
    }
  }
  return 0;
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int inf = INT_MAX;
int dp[5000];
set<int> to[5000];
vector<int> ls[5000];
int c[5000], r[5000];
int main() {
  int n, k;
  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--;
    ls[a].push_back(b);
    ls[b].push_back(a);
  }
  for (int i = 0; i < n; i++) {
    set<int> cur, next;
    cur.insert(i);
    for (int j = 0; j < r[i]; j++) {
      for (auto it = cur.begin(); it != cur.end(); it++) {
        for (int l = 0; l < (int)ls[*it].size(); l++) {
          if (to[i].find(ls[*it][l]) == to[i].end()) {
            to[i].insert(ls[*it][l]);
            next.insert(ls[*it][l]);
          }
        }
      }
      cur = next;
    }
    to[i].erase(i);
  }
  /*for(int i=0;i<n;i++){
          for(auto it=to[i].begin();it!=to[i].end();it++){
                  cout<<*it<<" ";
          }
          cout<<endl;
  }*/
  for (int i = 0; i < n; i++)
    dp[i] = inf;
  dp[0] = 0;
  priority_queue<pair<int, int>> pq;
  pq.push(make_pair(0, 0));
  while (!pq.empty()) {
    auto now = pq.top();
    pq.pop();
    int cost = -now.first, cur = now.second;
    // cout<<cur<<" "<<cost<<endl;
    if (cost > dp[cur])
      continue;
    if (cur == n - 1) {
      cout << cost << endl;
      return 0;
    }
    cost += c[cur];
    for (auto it = to[cur].begin(); it != to[cur].end(); it++) {
      if (dp[*it] > cost) {
        dp[*it] = cost;
        pq.push(make_pair(-cost, *it));
      }
    }
  }
  return 0;
}