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

#include <iostream>
using namespace std;

#include <cmath>

struct Q {
  int x;
  int y;
};

int map[10002][10002] = {0};

int xm[] = {1, -1, 0, 0, -1, 1};
int ym[] = {1, -1, -1, 1, 0, 0};

int main() {
  int w, h, n;
  int cnt = 0;
  Q tage;
  cin >> w >> h >> n >> tage.x >> tage.y;
  Q q;
  q.x = tage.x;
  q.y = tage.y;
  for (int i = 1; i < n; ++i) {
    cin >> tage.x >> tage.y;
    while (!(q.x == tage.x && q.y == tage.y)) {
      if (q.x < tage.x && q.y < tage.y) {
        cnt++;
        q.x++;
        q.y++;
      } else if (q.x > tage.x && q.y > tage.y) {
        cnt++;
        q.x--;
        q.y--;
      } else {
        if (q.x != tage.x) {
          cnt += abs(q.x - tage.x);
          q.x = tage.x;
        }
        if (q.y != tage.y) {
          cnt += abs(q.y - tage.y);
          q.y = tage.y;
        }
      }
    }
  }

  cout << cnt << endl;

  return 0;
}

Error: MLE

Fixed submission:

#include <iostream>
using namespace std;

#include <cmath>
#include <cstring>
#include <queue>

struct Q {
  int x;
  int y;
};

int map[10002][10002] = {0};

int xm[] = {1, -1, 0, 0, -1, 1};
int ym[] = {1, -1, -1, 1, 0, 0};

int main() {
  int w, h, n;
  int cnt = 0;
  cin >> w >> h >> n;
  Q q;
  q.x = 1;
  q.y = 1;
  for (int i = 0; i < n; ++i) {
    Q tage;
    cin >> tage.x >> tage.y;
    memset(map, 0, sizeof(map));
    map[q.y][q.x] = cnt;
    queue<Q> que;
    que.push(q);
    while (!que.empty()) {
      Q p;
      p = que.front();
      que.pop();
      if (p.x == tage.x && p.y == tage.y) {
        cnt = map[p.y][p.x];
        q = p;
        break;
      }
      for (int j = 0; j < 6; ++j) {
        Q move;
        move.x = p.x + xm[j];
        move.y = p.y + ym[j];
        if (move.x > 0 && move.x <= w && move.y > 0 && move.y <= h) {
          if (map[move.y][move.x] == 0) {
            map[move.y][move.x] = map[p.y][p.x] + 1;
            que.push(move);
          }
        }
      }
    }
    /*while (!(x == tagex && y == tagey)) {
            if (x < tagex && y < tagey) {
                    cnt++;
                    x++; y++;
            }
            else if (x > tagex && y > tagey) {
                    cnt++;
                    x--; y--;
            }
            else {
                    if (x != tagex) {
                            cnt += abs(x - tagex);
                            x = tagex;
                    }
                    if (y != tagey) {
                            cnt += abs(y - tagey);
                            y = tagey;
                    }
            }
    }*/
  }

  cout << cnt << endl;

  return 0;
}
Problem p00517 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
int main() {
  int i, w, h, n;
  int sum = 0;
  scanf("%d%d%d", &w, &h, &n);
  int x[n + 1];
  int y[n + 1];
  for (i = 1; i <= n; i++) {
    scanf("%d%d", &x[i], &y[i]);
  }
  for (int i = 1; i < n; i++) {
    if (x[i + 1] == x[i]) { /*????????´???*/
      sum += abs(y[i + 1] - y[i]);
    } else if (y[i + 1] == y[i]) { /*???????¨?????????´??????*/
      sum += abs(x[i + 1] - x[i]);
    } else if ((x[i + 1] - x[i]) * (y[i + 1] - y[i]) > 0) {
      /*?????±???????????¨???,????\\????????????¨???*/
      sum += max(abs(x[i + 1] - x[i]), abs(y[i + 1] - y[i]));
    } else { /*?????±,????\\????????????¨???*/
      sum += abs(x[i + 1] - x[i]) + abs(y[i + 1] - y[i]);
    }
  }
  printf("%d\\n", sum);
  return 0;
}

Error: WA

Fixed submission:

#include <stdio.h>
int main() {
  int i, w, h, n, a;
  int sum = 0;
  int x[w];
  int y[h];
  scanf("%d%d%d", &w, &h, &n);
  for (i = 1; i <= n; i++) {
    scanf("%d%d", &x[i], &y[i]);
  }
  for (int i = 1; i < n; i++) {
    if (x[i + 1] == x[i]) {
      sum += y[i + 1] - y[i];
    } else if (y[i + 1] == y[i]) {
      sum += x[i + 1] - x[i];
    } else if (x[i + 1] > x[i]) {
      if (y[i + 1] > y[i]) {
        a = y[i + 1] - y[i];
        sum += a + (x[i + 1] - a - 1);
      } else {
        sum += (x[i + 1] - x[i]) + (y[i + 1] - y[i]);
      }
    } else if (x[i + 1] < x[i]) {
      if (y[i + 1] > y[i]) {
        a = y[i + 1] - y[i];
        sum += a + (x[i] - a - 1);
      } else {
        sum += (x[i] - x[i + 1]) + (y[i] - y[i + 1]);
      }
    }
  }
  printf("%d\\n", sum);
  return 0;
}
Problem p00518 (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;
const int MOD = 10007;

int main() {
  int N;
  cin >> N;
  string s;
  cin >> s;
  int dp[1010][8] = {};
  dp[0][1] = 1;
  map<char, int> dic;
  for (int i = 0; i < 3; i++)
    dic["JOI"[i]] = i;

  for (int i = 0; i < N; i++) {
    for (int s1 = 0; s1 < 8; s1++) {
      for (int s2 = 0; s2 < 8; s2++) {
        if ((s2 >> dic[s[i]] & 1) && (s2 & s1) != 0) {
          dp[i + 1][s2] += dp[i][s1];
          dp[i + 1][s2] %= MOD;
        }
      }
    }
  }
  int ans = 0;
  for (int s = 0; s < 8; s++) {
    ans += dp[N][s];
    ans %= MOD;
  }
  cout << ans << 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;
const int MOD = 10007;

int main() {
  int N;
  cin >> N;
  string s;
  cin >> s;
  int dp[1000][8] = {};
  dp[0][1] = 1;
  map<char, int> dic;
  for (int i = 0; i < 3; i++)
    dic["JOI"[i]] = i;

  for (int i = 0; i < N; i++) {
    for (int s1 = 0; s1 < 8; s1++) {
      for (int s2 = 0; s2 < 8; s2++) {
        if ((s2 >> dic[s[i]] & 1) && (s2 & s1) != 0) {
          dp[i + 1][s2] += dp[i][s1];
          dp[i + 1][s2] %= MOD;
        }
      }
    }
  }
  int ans = 0;
  for (int s = 0; s < 8; s++) {
    ans += dp[N][s];
    ans %= MOD;
  }
  cout << ans << endl;
  return 0;
}
Problem p00518 (C++) — buggy submission, verdict: Runtime Error

// メモリ数確認したか?
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;

int main() {
  int n;
  string ss;
  cin >> n >> ss;
  const int MAX_N = 1000;
  const int mod = 10007;
  int dp[MAX_N + 1][7];
  for (int j = 0; j < 7; j++) {
    dp[0][j] = 0;
    if (j == 0)
      dp[0][j] = 1;
  }
  int ans[MAX_N + 1];
  ans[0] = 1;
  for (int i = 1; i <= n; i++) {
    ans[i] = 0;
    if (ss[i - 1] == 'J') {
      dp[i][0] = dp[i - 1][0] + dp[i - 1][3] + dp[i - 1][4] + dp[i - 1][6];
      dp[i][1] = 0;
      dp[i][2] = 0;
      dp[i][3] = ans[i - 1] - dp[i - 1][2];
      dp[i][4] = ans[i - 1] - dp[i - 1][1];
      dp[i][5] = 0;
      dp[i][6] = ans[i - 1];
    }
    if (ss[i - 1] == 'O') {
      dp[i][0] = 0;
      dp[i][1] = dp[i - 1][1] + dp[i - 1][3] + dp[i - 1][5] + dp[i - 1][6];
      dp[i][2] = 0;
      dp[i][3] = ans[i - 1] - dp[i - 1][2];
      dp[i][4] = 0;
      dp[i][5] = ans[i - 1] - dp[i - 1][0];
      dp[i][6] = ans[i - 1];
    }
    if (ss[i - 1] == 'I') {
      dp[i][0] = 0;
      dp[i][1] = 0;
      dp[i][2] = dp[i - 1][2] + dp[i - 1][4] + dp[i - 1][5] + dp[i - 1][6];
      dp[i][3] = 0;
      dp[i][4] = ans[i - 1] - dp[i - 1][1];
      dp[i][5] = ans[i - 1] - dp[i - 1][0];
      dp[i][6] = ans[i - 1];
    }
    for (int j = 0; j < 7; j++) {
      ans[i] += dp[i][j];
      dp[i][j] += mod;
      dp[i][j] %= mod;
    }
    ans[i] %= mod;
  }
  printf("%d\\n", ans[n]);
  return 0;
}
// メモリ数確認したか?

Error: 0

Fixed submission:

// メモリ数確認したか?
#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
typedef long long int ll;

int main() {
  int n;
  string ss;
  cin >> n >> ss;
  const int MAX_N = 1000;
  const int mod = 10007;
  int dp[MAX_N + 1][7];
  for (int j = 0; j < 7; j++) {
    dp[0][j] = 0;
    if (j == 0)
      dp[0][j] = 1;
  }
  int ans[MAX_N];
  ans[0] = 1;
  for (int i = 1; i <= n; i++) {
    ans[i] = 0;
    if (ss[i - 1] == 'J') {
      dp[i][0] = dp[i - 1][0] + dp[i - 1][3] + dp[i - 1][4] + dp[i - 1][6];
      dp[i][1] = 0;
      dp[i][2] = 0;
      dp[i][3] = ans[i - 1] - dp[i - 1][2];
      dp[i][4] = ans[i - 1] - dp[i - 1][1];
      dp[i][5] = 0;
      dp[i][6] = ans[i - 1];
    }
    if (ss[i - 1] == 'O') {
      dp[i][0] = 0;
      dp[i][1] = dp[i - 1][1] + dp[i - 1][3] + dp[i - 1][5] + dp[i - 1][6];
      dp[i][2] = 0;
      dp[i][3] = ans[i - 1] - dp[i - 1][2];
      dp[i][4] = 0;
      dp[i][5] = ans[i - 1] - dp[i - 1][0];
      dp[i][6] = ans[i - 1];
    }
    if (ss[i - 1] == 'I') {
      dp[i][0] = 0;
      dp[i][1] = 0;
      dp[i][2] = dp[i - 1][2] + dp[i - 1][4] + dp[i - 1][5] + dp[i - 1][6];
      dp[i][3] = 0;
      dp[i][4] = ans[i - 1] - dp[i - 1][1];
      dp[i][5] = ans[i - 1] - dp[i - 1][0];
      dp[i][6] = ans[i - 1];
    }
    for (int j = 0; j < 7; j++) {
      ans[i] += dp[i][j];
      dp[i][j] += mod;
      dp[i][j] %= mod;
    }
    ans[i] %= mod;
  }
  printf("%d\\n", ans[n]);
  return 0;
}
// メモリ数確認したか?
Problem p00518 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <map>
#include <string>
#define MOD 10007
using namespace std;

int n;
long long dp[1111][8];
string s;
map<char, int> shift;

long long rec(int idx, int prev) {

  if (idx == n)
    return 1;
  if (dp[idx][prev])
    return dp[idx][prev];

  long long ret = 0;
  int sft = shift[s[idx]];
  for (int i = 1; i < 8; i++) {
    if ((i >> sft & 1) && (i & prev)) {
      ret += rec(idx + 1, i) % MOD;
    }
  }
  return dp[idx][prev] = ret;
}

int main() {

  shift['I'] = 0, shift['O'] = 1, shift['J'] = 2;
  cin >> n >> s;
  cout << rec(0, 4) % MOD << endl;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <string>
#define MOD 10007
using namespace std;

int n;
long long dp[1111][8];
string s;
map<char, int> shift;

long long rec(int idx, int prev) {

  if (idx == n)
    return 1;
  if (dp[idx][prev])
    return dp[idx][prev];

  long long ret = 0;
  int sft = shift[s[idx]];
  for (int i = 1; i < 8; i++) {
    if ((i >> sft & 1) && (i & prev)) {
      (ret += rec(idx + 1, i)) % MOD;
    }
  }
  return dp[idx][prev] = ret;
}

int main() {

  shift['I'] = 0, shift['O'] = 1, shift['J'] = 2;
  cin >> n >> s;
  cout << rec(0, 4) % MOD << endl;
}
Problem p00518 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
using namespace std;
int n;
string ta;
int memo[20000][10];
int solve(int day, int f) {
  if (memo[day][f] != 0)
    return memo[day][f];
  if (day == n) {
    return memo[day][f] = 1;
  }

  int tmp, sum = 0;
  ;
  for (int i = 0; i < 8; i++) {
    if (ta[day] == 'J')
      tmp = 4;
    else if (ta[day] == 'O')
      tmp = 2;
    else if (ta[day] == 'I')
      tmp = 1;
    if ((i & tmp) != 0 && (i & f) != 0)
      sum += solve(day + 1, i);
  }

  return memo[day][f] = sum % 10007;
}

int main() {
  cin >> n;
  cin >> ta;
  cout << solve(0, 4) << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
using namespace std;
int n;
string ta;
int memo[200][200];
int solve(int day, int f) {
  if (memo[day][f] != 0)
    return memo[day][f];
  if (day == n) {
    return memo[day][f] = 1;
  }

  int tmp, sum = 0;
  ;
  for (int i = 0; i < 8; i++) {
    if (ta[day] == 'J')
      tmp = 4;
    else if (ta[day] == 'O')
      tmp = 2;
    else if (ta[day] == 'I')
      tmp = 1;
    if ((i & tmp) != 0 && (i & f) != 0)
      sum += solve(day + 1, i);
  }

  return memo[day][f] = sum % 10007;
}

int main() {
  cin >> n;
  cin >> ta;
  cout << solve(0, 4) << endl;
  return 0;
}
Problem p00519 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define F first
#define S second
const int INF = 1 << 31, MAX_N = 5000;
typedef pair<int, int> P;

void dijkstra();
void bfs(int s, int dis, int cost);
int n, k;
vector<int> road[MAX_N + 1];
int taxi[MAX_N + 1][2];
int reach[MAX_N + 1];

int main() {
  fill(reach + 2, reach + MAX_N + 1, INF);
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    scanf("%d%d", &taxi[i][0], &taxi[i][1]);
  }
  for (int i = 0; i < k; i++) {
    int a, b;
    scanf("%d%d", &a, &b);
    road[a].push_back(b);
    road[b].push_back(a);
  }
  dijkstra();
  cout << -reach[n] << endl;
  return 0;
}

priority_queue<P> pq;

void dijkstra() {
  bfs(1, taxi[1][1], -taxi[1][0]);
  while (!pq.empty()) {
    P p = pq.top();
    pq.pop();
    if (reach[p.S] < p.F) {
      reach[p.S] = p.F;
      bfs(p.S, taxi[p.S][1], p.F - taxi[p.S][0]);
    }
  }
}

void bfs(int s, int dis, int cost) {
  queue<P> que;
  bool rea[5001] = {};
  que.push(P(s, dis));
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    if (p.S) {
      for (int i = 0; i < road[p.F].size(); i++) {
        if (!rea[road[p.F][i]]) {
          rea[road[p.F][i]] = 1;
          que.push(P(road[p.F][i], p.S - 1));
          if (reach[road[p.F][i]] < cost) {
            pq.push(P(cost, road[p.F][i]));
          }
        }
      }
    }
  }
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define F first
#define S second
const int INF = 1 << 31, MAX_N = 5000;
typedef pair<int, int> P;

void dijkstra();
void bfs(int s, int dis, int cost);
int n, k;
vector<int> road[MAX_N + 1];
int taxi[MAX_N + 1][2];
int reach[MAX_N + 1];

int main() {
  fill(reach + 2, reach + MAX_N + 1, INF);
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    scanf("%d,%d", &taxi[i][0], &taxi[i][1]);
  }
  for (int i = 0; i < k; i++) {
    int a, b;
    scanf("%d,%d", &a, &b);
    road[a].push_back(b);
    road[b].push_back(a);
  }
  dijkstra();
  cout << -reach[n] << endl;
  return 0;
}

priority_queue<P> pq;

void dijkstra() {
  bfs(1, taxi[1][1], -taxi[1][0]);
  while (!pq.empty()) {
    P p = pq.top();
    pq.pop();
    if (reach[p.S] < p.F) {
      reach[p.S] = p.F;
      bfs(p.S, taxi[p.S][1], p.F - taxi[p.S][0]);
    }
  }
}

void bfs(int s, int dis, int cost) {
  queue<P> que;
  bool rea[5001] = {};
  que.push(P(s, dis));
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    if (p.S) {
      for (int i = 0; i < road[p.F].size(); i++) {
        if (!rea[road[p.F][i]]) {
          rea[road[p.F][i]] = 1;
          que.push(P(road[p.F][i], p.S - 1));
          if (reach[road[p.F][i]] < cost) {
            pq.push(P(cost, road[p.F][i]));
          }
        }
      }
    }
  }
}
Problem p00519 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <iostream>
#include <queue>
#include <set>
#include <vector>

using namespace std;

#define INF (1000000000)

typedef pair<int, int> P;

int main() {
  int N, K;
  cin >> N >> K;

  vector<int> C;
  vector<int> R;
  for (int i = 0; i < N; ++i) {
    int a, b;
    cin >> a >> b;
    C.push_back(a);
    R.push_back(b);
  }

  vector<int> PA[N];
  for (int i = 0; i < K; ++i) {
    int a, b;
    cin >> a >> b;
    PA[a - 1].push_back(b - 1);
    PA[b - 1].push_back(a - 1);
  }

  priority_queue<P, vector<P>, greater<P>> q;
  set<int> check;
  vector<int> cst(N, INF);

  q.push(P(0, 0));
  while (!q.empty()) {
    P p = q.top();
    q.pop();
    if (check.count(p.second) != 0)
      continue;
    cst[p.second] = p.first;
    check.insert(p.second);
    if (p.second == N - 1)
      break;

    queue<P> qtemp;
    set<int> stemp;
    qtemp.push(P(p.second, R[p.second]));
    while (!qtemp.empty()) {
      P ptemp = qtemp.front();
      qtemp.pop();
      for (auto x : PA[ptemp.first]) {
        if (stemp.count(x) != 0)
          continue;
        stemp.insert(x);
        if (ptemp.second > 1)
          qtemp.push(P(x, ptemp.second - 1));
      }
    }
    for (auto x : stemp) {
      if (check.count(x) != 0 || cst[x] < p.first + C[p.second])
        continue;
      cst[x] = p.first + C[p.second];
      q.push(P(p.first + C[p.second], x));
    }
  }

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

  return 0;
}


Error: MLE

Fixed submission:

#include <iostream>
#include <queue>
#include <set>
#include <vector>

using namespace std;

#define INF (1000000000)

typedef pair<int, int> P;

int main() {
  int N, K;
  cin >> N >> K;

  vector<int> C;
  vector<int> R;
  for (int i = 0; i < N; ++i) {
    int a, b;
    cin >> a >> b;
    C.push_back(a);
    R.push_back(b);
  }

  vector<int> PA[N];
  for (int i = 0; i < K; ++i) {
    int a, b;
    cin >> a >> b;
    PA[a - 1].push_back(b - 1);
    PA[b - 1].push_back(a - 1);
  }

  set<int> tnet[N];
  for (int i = 0; i < N - 1; ++i) { // check all taxi's reachable city
    // search all reachable town ( number i )
    queue<P> qtemp;
    set<int> t;
    qtemp.push(P(i, R[i]));
    while (!qtemp.empty()) {
      P p = qtemp.front();
      qtemp.pop();
      for (auto x : PA[p.first]) {
        if (t.count(x) != 0)
          continue;
        t.insert(x);
        if (p.second > 1)
          qtemp.push(P(x, p.second - 1));
      }
    }
    // make new network tnet[from]={to,to,...}
    for (auto x : t) {
      tnet[i].insert(x);
    }
    t.clear();
  }

  priority_queue<P, vector<P>, greater<P>> q;
  set<int> check;
  vector<int> cst(N, INF);

  q.push(P(0, 0));
  while (!q.empty()) {
    P p = q.top();
    q.pop();
    if (check.count(p.second) != 0)
      continue;
    cst[p.second] = p.first;
    check.insert(p.second);
    if (p.second == N - 1)
      break;
    for (auto x : tnet[p.second]) {
      if (check.count(x) != 0 || cst[x] < p.first + C[p.second])
        continue;
      cst[x] = p.first + C[p.second];
      q.push(P(p.first + C[p.second], x));
    }
  }

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

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

#include <bits/stdc++.h>
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define RREP(i, n) for (ll i = n - 1; i >= 0; i--)

#define PB push_back
#define INF (1 << 29)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))

typedef long long int ll;

using namespace std;

int d[5001];
int n, k;
vector<int> v[5000];
int t[5001];
int r[5001];
int dp[5000][5000];
ll c = 0;

int main() {
  memset(d, 100000001, sizeof(d));
  memset(dp, d[0], sizeof(dp));
  cin >> n >> k;
  REP(i, n) {
    cin >> t[i];
    cin >> r[i];
  }
  REP(i, k) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].PB(b);
    v[b].PB(a);
  }
  queue<int> qx;
  queue<int> qr;
  dp[r[0]][0] = t[0];
  qx.push(0);
  qr.push(r[0]);
  while (!qx.empty()) {
    int x = qx.front();
    int tr = qr.front();
    qx.pop();
    qr.pop();
    if (tr == 0)
      continue;
    REP(i, v[x].size()) {
      int nx = v[x][i];
      if (dp[tr - 1][nx] > dp[tr][x]) {
        dp[tr - 1][nx] = dp[tr][x];
        qx.push(nx);
        qr.push(tr - 1);
      }
      if (dp[r[nx]][nx] > dp[tr][x] + t[nx]) {
        dp[r[nx]][nx] = dp[tr][x] + t[nx];
        qx.push(nx);
        qr.push(r[nx]);
      }
    }
  }
  int ans = d[0];
  REP(i, 5000) ans = min(ans, dp[i][n - 1]);
  cout << ans << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define REP(i, n) for (ll i = 0; i < (n); i++)
#define RREP(i, n) for (ll i = n - 1; i >= 0; i--)

#define PB push_back
#define INF (1 << 29)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define CLR(a) memset(a, 0, sizeof(a))

typedef long long int ll;

using namespace std;

int d[5001];
int n, k;
vector<int> v[5000];
int t[5001];
int r[5001];
int dp[5000][5000];
ll c = 0;
void saiki(int m, int p, int rr) {
  if (dp[rr][p] <= m)
    return;
  else {
    /*
    RREP(i,rr+1) {
        if(dp[i][p] > m) dp[i][p] = m;
        else break;
    }
    */
    if (dp[rr + 1][p] <= m)
      return;
    if (dp[rr + 2][p] <= m)
      return;
    dp[rr][p] = m;
  }
  if (dp[rr][n - 1] <= m)
    return;
  if (p == n - 1) {
    return;
  }
  if (rr == 0)
    return;

  REP(i, v[p].size()) {
    int pos = v[p][i];
    saiki(m, pos, rr - 1);
    if (m + t[pos] < dp[r[pos]][pos]) {
      saiki(m + t[pos], pos, r[pos]);
    }
  }
}
int main() {
  memset(d, 100000001, sizeof(d));
  memset(dp, d[0], sizeof(dp));
  cin >> n >> k;
  REP(i, n) {
    cin >> t[i];
    cin >> r[i];
  }
  REP(i, k) {
    ll a, b;
    cin >> a >> b;
    a--;
    b--;
    v[a].PB(b);
    v[b].PB(a);
  }
  saiki(t[0], 0, r[0]);
  int ans = d[0];
  REP(i, 5000) ans = min(dp[i][n - 1], ans);
  // cout << dp[0][n-1] << endl;
  cout << ans << endl;
  return 0;
}
Problem p00519 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define F first
#define S second
const int INF = 1 << 31, MAX_N = 5000;
typedef pair<int, int> P;

void dijkstra();
void bfs(int s, int dis, int cost);
int n, k;
vector<int> road[MAX_N + 1];
int taxi[MAX_N + 1][2];
int reach[MAX_N + 1];

int main() {
  fill(reach + 2, reach + MAX_N + 1, INF);
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> taxi[i][0] >> taxi[i][1];
  }
  for (int i = 0; i < k; i++) {
    int a, b;
    cin >> a >> b;
    road[a].push_back(b);
    road[b].push_back(a);
  }
  dijkstra();
  cout << -reach[n] << endl;
  return 0;
}

priority_queue<P> pq;

void dijkstra() {
  bfs(1, taxi[1][1], -taxi[1][0]);
  while (!pq.empty()) {
    P p = pq.top();
    pq.pop();
    if (reach[p.S] < p.F) {
      reach[p.S] = p.F;
      bfs(p.S, taxi[p.S][1], p.F - taxi[p.S][0]);
    }
  }
}

void bfs(int s, int dis, int cost) {
  queue<P> que;
  bool rea[5001] = {};
  que.push(P(s, dis));
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    if (p.S) {
      for (int i = 0; i < road[p.F].size(); i++) {
        if (!rea[road[p.F][i]]) {
          rea[road[p.F][i]] = 1;
          que.push(P(road[p.F][i], p.S - 1));
          pq.push(P(cost, road[p.F][i]));
        }
      }
    }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <vector>
using namespace std;
#define F first
#define S second
const int INF = 1 << 31, MAX_N = 50000;
typedef pair<int, int> P;

void dijkstra();
void bfs(int s, int dis, int cost);
int n, k;
vector<int> road[MAX_N + 1];
int taxi[MAX_N + 1][2];
int reach[MAX_N + 1];

int main() {
  fill(reach + 2, reach + MAX_N + 1, INF);
  cin >> n >> k;
  for (int i = 1; i <= n; i++) {
    cin >> taxi[i][0] >> taxi[i][1];
  }
  for (int i = 0; i < k; i++) {
    int a, b;
    cin >> a >> b;
    road[a].push_back(b);
    road[b].push_back(a);
  }
  dijkstra();
  cout << -reach[n] << endl;
  return 0;
}

priority_queue<P> pq;

void dijkstra() {
  bfs(1, taxi[1][1], -taxi[1][0]);
  while (!pq.empty()) {
    P p = pq.top();
    pq.pop();
    if (reach[p.S] < p.F) {
      reach[p.S] = p.F;
      bfs(p.S, taxi[p.S][1], p.F - taxi[p.S][0]);
    }
  }
}

void bfs(int s, int dis, int cost) {
  queue<P> que;
  que.push(P(s, dis));
  while (!que.empty()) {
    P p = que.front();
    que.pop();
    if (p.S) {
      for (int i = 0; i < road[p.F].size(); i++) {
        que.push(P(road[p.F][i], p.S - 1));
        pq.push(P(cost, road[p.F][i]));
      }
    }
  }
}