prithivMLmods/Coder-Stat huggingface dataset:
Problem p00207 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int F[105][105];
int xg, yg, w, h;

void DFS(int Y, int X, int c) {
  if (F[Y][X] != c) {
    return;
  }

  F[Y][X] = -1;
  DFS(Y + 1, X, c);
  DFS(Y - 1, X, c);
  DFS(Y, X + 1, c);
  DFS(Y, X - 1, c);
}

int main() {
  int xs, ys, n;
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    for (int i = 0; i < 105; i++) {
      for (int j = 0; j < 105; j++) {
        F[i][j] = 10;
      }
    }

    cin >> xs >> ys >> xg >> yg >> n;

    for (int i = 0; i < n; i++) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;

      if (d == 0) {
        for (int j = 0; j < 4; j++) {
          F[x + j][y] = c;
          F[x + j][y + 1] = c;
        }
      } else {
        for (int j = 0; j < 4; j++) {
          F[x][y + j] = c;
          F[x + 1][y + j] = c;
        }
      }
    }
    DFS(xs, ys, F[xs][ys]);

    if (F[xg][yg] == -1) {
      cout << "OK" << endl;
    } else {
      cout << "NG" << endl;
    }
  }
}


Fixed submission:
#include <iostream>
using namespace std;

int F[105][105];
int xg, yg, w, h;

void DFS(int Y, int X, int c) {
  if (F[Y][X] != c) {
    return;
  }

  F[Y][X] = -1;
  DFS(Y + 1, X, c);
  DFS(Y - 1, X, c);
  DFS(Y, X + 1, c);
  DFS(Y, X - 1, c);
}

int main() {
  int xs, ys, n;
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    for (int i = 0; i < 102; i++) {
      for (int j = 0; j < 102; j++) {
        F[i][j] = 10;
      }
    }

    cin >> xs >> ys >> xg >> yg >> n;

    for (int i = 0; i < n; i++) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;

      if (d == 0) {
        for (int j = 0; j < 4; j++) {
          F[x + j][y] = c;
          F[x + j][y + 1] = c;
        }
      } else {
        for (int j = 0; j < 4; j++) {
          F[x][y + j] = c;
          F[x + 1][y + j] = c;
        }
      }
    }
    DFS(xs, ys, F[xs][ys]);

    if (F[xg][yg] == -1) {
      cout << "OK" << endl;
    } else {
      cout << "NG" << endl;
    }
  }
}
Problem p00207 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int F[105][105];
int xg, yg, w, h;

void DFS(int Y, int X, int c) {
  if (F[Y][X] != c) {
    return;
  }

  F[Y][X] = -1;
  DFS(Y + 1, X, c);
  DFS(Y - 1, X, c);
  DFS(Y, X + 1, c);
  DFS(Y, X - 1, c);
}

int main() {
  int xs, ys, n;
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    for (int i = 0; i < 105; i++) {
      for (int j = 0; j < 105; j++) {
        F[i][j] = 10;
      }
    }

    cin >> xs >> ys >> xg >> yg >> n;

    for (int i = 0; i < n; i++) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;

      if (d == 0) {
        for (int j = 0; j < 4; j++) {
          F[x + j][y] = c;
          F[x + j][y + 1] = c;
        }
      } else {
        for (int j = 0; j < 4; j++) {
          F[x][y + j] = c;
          F[x + 1][y + j] = c;
        }
      }
    }

    DFS(xs, ys, F[xs][ys]);

    if (F[xg][yg] == -1) {
      cout << "OK" << endl;
    } else {
      cout << "NG" << endl;
    }
  }
}


Fixed submission:
#include <iostream>
using namespace std;

int F[105][105];
int xg, yg, w, h;

void DFS(int Y, int X, int c) {
  if (F[Y][X] != c) {
    return;
  }

  F[Y][X] = -1;
  DFS(Y + 1, X, c);
  DFS(Y - 1, X, c);
  DFS(Y, X + 1, c);
  DFS(Y, X - 1, c);
}

int main() {
  int xs, ys, n;
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    for (int i = 0; i < 105; i++) {
      for (int j = 0; j < 105; j++) {
        F[i][j] = 10;
      }
    }

    cin >> xs >> ys >> xg >> yg >> n;

    for (int i = 0; i < n; i++) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;

      if (d == 0) {
        for (int j = 0; j < 4; j++) {
          F[x + j][y] = c;
          F[x + j][y + 1] = c;
        }
      } else {
        for (int j = 0; j < 4; j++) {
          F[x][y + j] = c;
          F[x + 1][y + j] = c;
        }
      }
    }
    if (F[xs][ys] == 10) {
      cout << "NG" << endl;
      continue;
    }
    DFS(xs, ys, F[xs][ys]);

    if (F[xg][yg] == -1) {
      cout << "OK" << endl;
    } else {
      cout << "NG" << endl;
    }
  }
}
Problem p00207 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int F[105][105];
int xg, yg;

void DFS(int Y, int X, int c) {
  if (F[Y][X] != c) {
    return;
  }

  F[Y][X] = -2000;
  DFS(Y - 1, X, c);
  DFS(Y, X + 1, c);
  DFS(Y + 1, X, c);
  DFS(Y, X - 1, c);
}

int main() {
  int w, h, xs, ys, n;
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;

    cin >> xs >> ys >> xg >> yg >> n;
    int c[n], d[n], x[n], y[n], ca;
    for (int i = 0; i < n; i++) {
      cin >> c[i] >> d[i] >> x[i] >> y[i];
    }
    for (int i = 0; i < n; i++) {
      if (c[i] == 0) {
        ca = 1000;
        break;
      } else if (i == n - 1) {
        ca = 0;
      }
    }
    for (int i = 0; i < 102; i++) {
      for (int j = 0; j < 102; j++) {
        F[i][j] = ca;
      }
    }
    for (int i = 0; i < n; i++) {
      if (d[i] == 0) {
        for (int j = 0; j < 4; j++) {
          F[x[i] + j][y[i]] = c[i];
          F[x[i] + j][y[i] + 1] = c[i];
        }
      } else {
        for (int j = 0; j < 4; j++) {
          F[x[i]][y[i] + j] = c[i];
          F[x[i] + 1][y[i] + j] = c[i];
        }
      }
    }

    DFS(xs, ys, F[xs][ys]);

    if (F[xg][yg] == -2000) {
      cout << "OK" << endl;
    } else {
      cout << "NG" << endl;
    }
  }
}


Fixed submission:
#include <iostream>
using namespace std;

int F[105][105];
int xg, yg;

void DFS(int Y, int X, int c) {
  if (F[Y][X] != c) {
    return;
  }

  F[Y][X] = -2000;
  DFS(Y - 1, X, c);
  DFS(Y, X + 1, c);
  DFS(Y + 1, X, c);
  DFS(Y, X - 1, c);
}

int main() {
  int w, h, xs, ys, n;
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;

    cin >> xs >> ys >> xg >> yg >> n;
    int c[n], d[n], x[n], y[n], ca;
    for (int i = 0; i < n; i++) {
      cin >> c[i] >> d[i] >> x[i] >> y[i];
    }
    for (int i = 0; i < n; i++) {
      if (c[i] == 0) {
        ca = 1000;
        break;
      } else if (i == n - 1) {
        ca = 1000;
      }
    }
    for (int i = 0; i < 102; i++) {
      for (int j = 0; j < 102; j++) {
        F[i][j] = ca;
      }
    }
    for (int i = 0; i < n; i++) {
      if (d[i] == 0) {
        for (int j = 0; j < 4; j++) {
          F[x[i] + j][y[i]] = c[i];
          F[x[i] + j][y[i] + 1] = c[i];
        }
      } else {
        for (int j = 0; j < 4; j++) {
          F[x[i]][y[i] + j] = c[i];
          F[x[i] + 1][y[i] + j] = c[i];
        }
      }
    }

    DFS(xs, ys, F[xs][ys]);

    if (F[xg][yg] == -2000) {
      cout << "OK" << endl;
    } else {
      cout << "NG" << endl;
    }
  }
}
Problem p00207 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

int block[100][100];
int xs, ys, xg, yg, n, w, h;

bool dfs(int x, int y, int c) {
  if (block[x][y] != c || block[x][y] == 0)
    return false;
  if (x == xg && y == yg)
    return true;
  bool b = false;
  block[x][y] = 0;
  if (x < w - 1)
    b |= dfs(x + 1, y, c);
  if (x > 0 && !b)
    b |= dfs(x - 1, y, c);
  if (y < h - 1 && !b)
    b |= dfs(x, y + 1, c);
  if (y > 0 && !b)
    b |= dfs(x, y - 1, c);
  block[x][y] = c;
  return b;
}

int main() {
  while (true) {
    cin >> w >> h;
    if (!w && !h)
      break;
    cin >> xs >> ys >> xg >> yg >> n;
    xs--;
    ys--;
    xg--;
    yg--;

    for (int i = 0; i < n; i++) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      x--;
      y--;
      block[x][y] = c;
      block[x + 1][y] = c;
      block[x][y + 1] = c;
      block[x + 1][y + 1] = c;
      if (d == 0)
        x += 2;
      else
        y += 2;
      block[x][y] = c;
      block[x + 1][y] = c;
      block[x][y + 1] = c;
      block[x + 1][y + 1] = c;
    }
    if (dfs(xs, ys, block[xs][ys]))
      cout << "OK\n";
    else
      cout << "NG\n";
  }
}

Fixed submission:
#include <iostream>
using namespace std;

int block[100][100];
int xs, ys, xg, yg, n, w, h;

bool dfs(int x, int y, int c) {
  if (block[x][y] != c || block[x][y] == 0)
    return false;
  if (x == xg && y == yg)
    return true;
  bool b = false;
  block[x][y] = 0;
  if (x < w - 1)
    b |= dfs(x + 1, y, c);
  if (x > 0 && !b)
    b |= dfs(x - 1, y, c);
  if (y < h - 1 && !b)
    b |= dfs(x, y + 1, c);
  if (y > 0 && !b)
    b |= dfs(x, y - 1, c);
  return b;
}

int main() {
  while (true) {
    cin >> w >> h;
    if (!w && !h)
      break;
    cin >> xs >> ys >> xg >> yg >> n;
    xs--;
    ys--;
    xg--;
    yg--;

    for (int i = 0; i < n; i++) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      x--;
      y--;
      block[x][y] = c;
      block[x + 1][y] = c;
      block[x][y + 1] = c;
      block[x + 1][y + 1] = c;
      if (d == 0)
        x += 2;
      else
        y += 2;
      block[x][y] = c;
      block[x + 1][y] = c;
      block[x][y + 1] = c;
      block[x + 1][y + 1] = c;
    }
    if (dfs(xs, ys, block[xs][ys]))
      cout << "OK\n";
    else
      cout << "NG\n";
  }
}
Problem p00207 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int F[105][105];
int xg, yg, w, h;

void DFS(int Y, int X, int c) {
  if (F[Y][X] != c || Y > w || X > h) {
    return;
  }

  F[Y][X] = -1;
  DFS(Y + 1, X, c);
  DFS(Y - 1, X, c);
  DFS(Y, X + 1, c);
  DFS(Y, X - 1, c);
}

int main() {
  int xs, ys, n;
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    for (int i = 0; i < 105; i++) {
      for (int j = 0; j < 105; j++) {
        F[i][j] = 0;
      }
    }

    cin >> xs >> ys >> xg >> yg >> n;

    for (int i = 0; i < n; i++) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      if (d == 0) {
        for (int j = 0; j < 4; j++) {
          F[x + j][y] = c;
          F[x + j][y + 1] = c;
        }
      } else {
        for (int j = 0; j < 4; j++) {
          F[x][y + j] = c;
          F[x + 1][y + j] = c;
        }
      }
    }

    DFS(xs, ys, F[xs][ys]);

    if (F[xg][yg] == -1) {
      cout << "OK" << endl;
    } else {
      cout << "NG" << endl;
    }
  }
}


Fixed submission:
#include <iostream>
using namespace std;

int F[105][105];
int xg, yg, w, h;

void DFS(int Y, int X, int c) {
  if (F[Y][X] != c || Y > w || X > h) {
    return;
  }

  F[Y][X] = -1;
  DFS(Y + 1, X, c);
  DFS(Y - 1, X, c);
  DFS(Y, X + 1, c);
  DFS(Y, X - 1, c);
}

int main() {
  int xs, ys, n;
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;
    for (int i = 0; i < 105; i++) {
      for (int j = 0; j < 105; j++) {
        F[i][j] = 10;
      }
    }

    cin >> xs >> ys >> xg >> yg >> n;

    for (int i = 0; i < n; i++) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      if (d == 0) {
        for (int j = 0; j < 4; j++) {
          F[x + j][y] = c;
          F[x + j][y + 1] = c;
        }
      } else {
        for (int j = 0; j < 4; j++) {
          F[x][y + j] = c;
          F[x + 1][y + j] = c;
        }
      }
    }

    DFS(xs, ys, F[xs][ys]);

    if (F[xg][yg] == -1) {
      cout << "OK" << endl;
    } else {
      cout << "NG" << endl;
    }
  }
}
Problem p00207 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int F[105][105];
int xg, yg;

void DFS(int Y, int X, int c) {
  if (F[Y][X] != c) {
    return;
  }

  F[Y][X] = -2000;
  DFS(Y - 1, X, c);
  DFS(Y, X + 1, c);
  DFS(Y + 1, X, c);
  DFS(Y, X - 1, c);
}

int main() {
  int w, h, xs, ys, n;
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;

    cin >> xs >> ys >> xg >> yg >> n;
    int c[n], d[n], x[n], y[n], ca;
    for (int i = 0; i < n; i++) {
      cin >> c[i] >> d[i] >> x[i] >> y[i];
    }
    for (int i = 0; i < n; i++) {
      if (c[i] == 0)
        ca = 10;
      else if (i == n - 1)
        ca = 0;
    }
    for (int i = 0; i < 102; i++) {
      for (int j = 0; j < 102; j++) {
        F[i][j] = ca;
      }
    }
    for (int i = 0; i < n; i++) {
      if (d[i] == 0) {
        for (int j = 0; j < 4; j++) {
          F[x[i] + j][y[i]] = c[i];
          F[x[i] + j][y[i] + 1] = c[i];
        }
      } else {
        for (int j = 0; j < 4; j++) {
          F[x[i]][y[i] + j] = c[i];
          F[x[i] + 1][y[i] + j] = c[i];
        }
      }
    }

    DFS(xs, ys, F[xs][ys]);

    if (F[xg][yg] == -2000) {
      cout << "OK" << endl;
    } else {
      cout << "NG" << endl;
    }
  }
}


Fixed submission:
#include <iostream>
using namespace std;

int F[105][105];
int xg, yg;

void DFS(int Y, int X, int c) {
  if (F[Y][X] != c) {
    return;
  }

  F[Y][X] = -2000;
  DFS(Y - 1, X, c);
  DFS(Y, X + 1, c);
  DFS(Y + 1, X, c);
  DFS(Y, X - 1, c);
}

int main() {
  int w, h, xs, ys, n;
  while (1) {
    cin >> w >> h;
    if (w == 0 && h == 0)
      break;

    cin >> xs >> ys >> xg >> yg >> n;
    int c[n], d[n], x[n], y[n], ca;
    for (int i = 0; i < n; i++) {
      cin >> c[i] >> d[i] >> x[i] >> y[i];
    }
    for (int i = 0; i < n; i++) {
      if (c[i] == 0)
        ca = 10;
      else if (i == n - 1)
        ca = 0;
    }
    for (int i = 0; i < 102; i++) {
      for (int j = 0; j < 102; j++) {
        F[i][j] = 10;
      }
    }
    for (int i = 0; i < n; i++) {
      if (d[i] == 0) {
        for (int j = 0; j < 4; j++) {
          F[x[i] + j][y[i]] = c[i];
          F[x[i] + j][y[i] + 1] = c[i];
        }
      } else {
        for (int j = 0; j < 4; j++) {
          F[x[i]][y[i] + j] = c[i];
          F[x[i] + 1][y[i] + j] = c[i];
        }
      }
    }

    DFS(xs, ys, F[xs][ys]);

    if (F[xg][yg] == -2000) {
      cout << "OK" << endl;
    } else {
      cout << "NG" << endl;
    }
  }
}
Problem p00207 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;

int F[102][102];

bool DFS(int X, int Y, int Xg, int Yg, int c) {
  if (F[Y][X] != c) {
    return false;
  } else if (F[Y][X] == c && X == Xg && Y == Yg) {
    return true;
  }
  F[Y][X] = -1;
  if (DFS(X, Y - 1, Xg, Yg, c)) {
    return true;
  } else if (DFS(X + 1, Y, Xg, Yg, c)) {
    return true;
  } else if (DFS(X, Y + 1, Xg, Yg, c)) {
    return true;
  } else if (DFS(X - 1, Y, Xg, Yg, c)) {
    return true;
  }

  return false;
}

int main() {
  while (true) {
    int w, h;
    cin >> w >> h;
    if (w == 0 && h == 0) {
      break;
    }
    int xs, ys, xg, yg;
    cin >> xs >> ys >> xg >> yg;
    int n;
    cin >> n;
    for (int y = 0; y < 102; y++) {
      for (int x = 0; x < 102; x++) {
        F[y][x] = -1;
      }
    }
    for (int i = 0; i < n; i++) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      if (d == 0) {
        for (int X = x; X < x + 4; X++) {
          for (int Y = y; Y < y + 2; Y++) {
            F[Y][X] = c;
          }
        }
      } else if (d == 1) {
        for (int X = x; X < x + 2; X++) {
          for (int Y = y; Y < y + 4; Y++) {
            F[Y][X] = c;
          }
        }
      }
    }
    if (DFS(xs, ys, xg, yg, F[xs][ys])) {
      cout << "OK" << endl;
    } else {
      cout << "NG" << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;

int F[102][102];

bool DFS(int X, int Y, int Xg, int Yg, int c) {
  if (F[Y][X] != c) {
    return false;
  } else if (F[Y][X] == c && X == Xg && Y == Yg) {
    return true;
  }
  F[Y][X] = -1;
  if (DFS(X, Y - 1, Xg, Yg, c)) {
    return true;
  } else if (DFS(X + 1, Y, Xg, Yg, c)) {
    return true;
  } else if (DFS(X, Y + 1, Xg, Yg, c)) {
    return true;
  } else if (DFS(X - 1, Y, Xg, Yg, c)) {
    return true;
  }

  return false;
}

int main() {
  while (true) {
    int w, h;
    cin >> w >> h;
    if (w == 0 && h == 0) {
      break;
    }
    int xs, ys, xg, yg;
    cin >> xs >> ys >> xg >> yg;
    int n;
    cin >> n;
    for (int y = 0; y < 102; y++) {
      for (int x = 0; x < 102; x++) {
        F[y][x] = -1;
      }
    }
    for (int i = 0; i < n; i++) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      if (d == 0) {
        for (int X = x; X < x + 4; X++) {
          for (int Y = y; Y < y + 2; Y++) {
            F[Y][X] = c;
          }
        }
      } else if (d == 1) {
        for (int X = x; X < x + 2; X++) {
          for (int Y = y; Y < y + 4; Y++) {
            F[Y][X] = c;
          }
        }
      }
    }
    if (F[ys][xs] != -1) {
      if (DFS(xs, ys, xg, yg, F[ys][xs])) {
        cout << "OK" << endl;
      } else {
        cout << "NG" << endl;
      }
    } else {
      cout << "NG" << endl;
    }
  }
  return 0;
}
Problem p00207 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
#define MAX_H 100
#define MAX_W 100
#define INF 1000;
int h, w;
int field[MAX_H][MAX_W];
int dx[4] = {0, -1, 0, 1}, dy[4] = {-1, 0, 1, 0};
void dfs(int y, int x, const int sc) {
  if (y <= 0 || x <= 0 || y > h || x > w)
    return;
  if (field[y][x] == sc) {
    field[y][x] = 0;
    for (int i = 0; i < 4; i++)
      dfs(y + dy[i], x + dx[i], sc);
  }
  return;
}
int main() {
  int sx, sy, gx, gy, n;
  while (cin >> w >> h) {
    if (!w && !h)
      break;
    cin >> sx >> sy >> gx >> gy >> n;
    for (int i = 0; i < MAX_H; i++) {
      for (int j = 0; j < MAX_W; j++) {
        field[i][j] = INF;
      }
    }
    while (n--) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 4; j++) {
          if (d)
            field[y + j][x + i] = c;
          else
            field[y + i][x + j] = c;
        }
      }
    }
    dfs(sy, sx, field[sy][sx]);
    if (field[gy][gx])
      cout << "NG" << endl;
    else
      cout << "OK" << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
#define MAX_H 100
#define MAX_W 100
#define INF 1000;
int h, w;
int field[MAX_H + 1][MAX_W + 1];
int dx[4] = {0, -1, 0, 1}, dy[4] = {-1, 0, 1, 0};
void dfs(int y, int x, const int sc) {
  if (y <= 0 || x <= 0 || y > h || x > w)
    return;
  if (field[y][x] == sc) {
    field[y][x] = 0;
    for (int i = 0; i < 4; i++)
      dfs(y + dy[i], x + dx[i], sc);
  }
  return;
}
int main() {
  int sx, sy, gx, gy, n;
  while (cin >> w >> h) {
    if (!w && !h)
      break;
    cin >> sx >> sy >> gx >> gy >> n;
    for (int i = 0; i < MAX_H; i++) {
      for (int j = 0; j < MAX_W; j++) {
        field[i][j] = INF;
      }
    }
    while (n--) {
      int c, d, x, y;
      cin >> c >> d >> x >> y;
      for (int i = 0; i < 2; i++) {
        for (int j = 0; j < 4; j++) {
          if (d)
            field[y + j][x + i] = c;
          else
            field[y + i][x + j] = c;
        }
      }
    }
    dfs(sy, sx, field[sy][sx]);
    if (field[gy][gx])
      cout << "NG" << endl;
    else
      cout << "OK" << endl;
  }
  return 0;
}
Problem p00207 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cstring>
#include <iostream>
using namespace std;
int map[110][110];
int com[110][110];
int lx, ly, sx, sy, gx, gy;
int py[4] = {1, 0, -1, 0};
int px[4] = {0, 1, 0, -1};
int fl;
int mei(int zx, int zy, int cei[][110]) {
  int i;
  if (zx == gx && zy == gy) {
    fl = 1;
    return 0;
  }
  cei[zy][zx] = 1;
  for (i = 0; i < 4; i++) {
    if (cei[zy + py[i]][zx + px[i]] == 0 &&
        map[sy][sx] == map[zy + py[i]][zx + px[i]]) {
      mei(zx + px[i], zy + py[i], cei);
      if (fl)
        return 0;
    }
  }
  cei[zy][zx] = 0;
  return 0;
}
int main() {
  int n, col, muk, vx, vy;
  int i, j, t;
  while (cin >> lx >> ly, lx != 0 && ly != 0) {
    fl = 0;
    cin >> sx >> sy >> gx >> gy >> n;
    memset(map, 0, sizeof(map));
    memset(com, 0, sizeof(com));
    for (i = 0; i < n; i++) {
      cin >> col >> muk >> vx >> vy;
      if (muk == 0) {
        for (j = vy; j < 2 + vy; j++) {
          for (t = vx; t < 4 + vx; t++) {
            map[j][t] = col;
          }
        }
      } else {
        for (j = vy; j < 4 + vy; j++) {
          for (t = vx; t < 2 + vx; t++) {
            map[j][t] = col;
          }
        }
      }
    }
    if (map[sy][sx] != map[gy][gx]) {
      cout << "NG" << endl;
      continue;
    }
    mei(sx, sy, com);
    if (fl)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
  return 0;
}

Fixed submission:
#include <cstring>
#include <iostream>
using namespace std;
int map[110][110];
int com[110][110];
int lx, ly, sx, sy, gx, gy;
int py[4] = {1, 0, -1, 0};
int px[4] = {0, 1, 0, -1};
int fl;
int mei(int zx, int zy, int cei[][110]) {
  int i;
  if (zx == gx && zy == gy) {
    fl = 1;
    return 0;
  }
  cei[zy][zx] = 1;
  for (i = 0; i < 4; i++) {
    if (cei[zy + py[i]][zx + px[i]] == 0 &&
        map[sy][sx] == map[zy + py[i]][zx + px[i]]) {
      mei(zx + px[i], zy + py[i], cei);
      if (fl)
        return 0;
    }
  }
  return 0;
}
int main() {
  int n, col, muk, vx, vy;
  int i, j, t;
  while (cin >> lx >> ly, lx != 0 && ly != 0) {
    fl = 0;
    cin >> sx >> sy >> gx >> gy >> n;
    memset(map, 0, sizeof(map));
    memset(com, 0, sizeof(com));
    for (i = 0; i < n; i++) {
      cin >> col >> muk >> vx >> vy;
      if (muk == 0) {
        for (j = vy; j < 2 + vy; j++) {
          for (t = vx; t < 4 + vx; t++) {
            map[j][t] = col;
          }
        }
      } else {
        for (j = vy; j < 4 + vy; j++) {
          for (t = vx; t < 2 + vx; t++) {
            map[j][t] = col;
          }
        }
      }
    }
    if (map[sy][sx] != map[gy][gx]) {
      cout << "NG" << endl;
      continue;
    }
    mei(sx, sy, com);
    if (fl)
      cout << "OK" << endl;
    else
      cout << "NG" << endl;
  }
  return 0;
}
Problem p00208 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
using namespace std;

int main() {
  while (1) {
    int a, j = 0;
    cin >> a;
    j = a;
    string s = "";
    int b = 0;
    for (b = 1; b * 8 <= a; b *= 8) {
    }
    for (int c = b; c != 1; c /= 8) {
      int o = a / c;
      switch (a / c) {
      case 0:
      case 1:
      case 2:
      case 3:
        cout << a / c;
        break;
      case 4:
        cout << 5;
        break;
      default:
        cout << a / c + 2;
        break;
      }

      a -= c * o;
    }
    switch (j % 8) {
    case 0:
    case 1:
    case 2:
    case 3:
      cout << j % 8;
      break;
    case 4:
      cout << 5;
      break;
    default:
      cout << j % 8 + 2;
    }

    cout << endl;
  }
}

Fixed submission:
#include <iostream>
using namespace std;

int main() {
  while (1) {
    int a, j = 0;
    cin >> a;
    if (a == 0)
      break;
    j = a;
    string s = "";
    int b = 0;
    for (b = 1; b * 8 <= a; b *= 8) {
    }
    for (int c = b; c != 1; c /= 8) {
      int o = a / c;
      switch (a / c) {
      case 0:
      case 1:
      case 2:
      case 3:
        cout << a / c;
        break;
      case 4:
        cout << 5;
        break;
      default:
        cout << a / c + 2;
        break;
      }

      a -= c * o;
    }
    switch (j % 8) {
    case 0:
    case 1:
    case 2:
    case 3:
      cout << j % 8;
      break;
    case 4:
      cout << 5;
      break;
    default:
      cout << j % 8 + 2;
    }

    cout << endl;
  }
}
Problem p00208 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <cmath>
#include <iostream>
using namespace std;
int x[8] = {0, 1, 2, 3, 5, 7, 8, 9};
int n, ok, k;
int main() {
  while (true) {
    ok = 0;
    cin >> n;
    if (!n) {
      break;
    }
    for (int i = 10; i >= 0; i++) {
      k = (n / (int)pow(8, i)) % 8;
      if (k > 0) {
        ok = 1;
      }
      if (ok == 1) {
        cout << x[k];
      }
    }
    cout << endl;
  }
}

Fixed submission:
#include <cmath>
#include <iostream>
using namespace std;
int x[8] = {0, 1, 2, 3, 5, 7, 8, 9};
int n, ok, k;
int main() {
  while (true) {
    ok = 0;
    cin >> n;
    if (!n) {
      break;
    }
    for (int i = 10; i >= 0; i--) {
      k = (n / (int)pow(8, i)) % 8;
      if (k > 0) {
        ok = 1;
      }
      if (ok == 1) {
        cout << x[k];
      }
    }
    cout << endl;
  }
}
Problem p00208 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

using namespace std;

int main() {
  int n, num[100];
  while (cin >> n, n) {
    int k = -1;
    do {
      num[++k] = n % 8;
      if (num[k] >= 5)
        num[k] += 2;
      else if (num[k] >= 4)
        ++num[k];
    } while (n /= 8);
    for (int i = k; i >= 0; --i)
      cout << num[i];
    cout << endl;
  }
  return 1;
}

Fixed submission:
#include <iostream>

using namespace std;

int main() {
  int n, num[100];
  while (cin >> n, n) {
    int k = -1;
    do {
      num[++k] = n % 8;
      if (num[k] >= 5)
        num[k] += 2;
      else if (num[k] >= 4)
        ++num[k];
    } while (n /= 8);
    for (int i = k; i >= 0; --i)
      cout << num[i];
    cout << endl;
  }
  return 0;
}
Problem p00208 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <stdio.h>

int main() {
  int n, i, c;
  char s[16];
  while (scanf("%d", &n)) {
    sprintf(s, "%o", n);
    for (i = 0; c = s[i]; i++) {
      c -= 48;
      printf("%d", c + c / 4 + c / 5);
    }
    printf("\n");
  }
}

Fixed submission:
#include <stdio.h>

int main() {
  int n, i, c;
  char s[16];
  while (scanf("%d", &n), n) {
    sprintf(s, "%o", n);
    for (i = 0; c = s[i]; i++) {
      c -= 48;
      printf("%d", c + c / 4 + c / 5);
    }
    printf("\n");
  }
}
Problem p00209 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#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()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e9;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
int main() {
  int n, m;
  while (scanf("%d %d", n, m), n) {
    vvi in(n, vi(n));
    vvi pic(m, vi(m));
    rep(i, n) rep(j, n) scanf("%d", &in[i][j]);
    rep(i, m) rep(j, m) scanf("%d", &pic[i][j]);
    pii out(inf, inf);
    rep(e, 4) {
      rep(i, n - m + 1) rep(j, n - m + 1) {
        pii t(inf, inf);
        rep(k, m) rep(l, m) if (pic[k][l] != -1 &&
                                in[i + k][j + l] != pic[k][l]) goto en;
        else if (pic[k][l] != -1) t = min(t, pii(i + k, j + l));
        out = min(out, t);
        goto end;
      en:;
      }
    end:;
      vvi tmp = pic;
      rep(i, m) rep(j, m) pic[i][j] = tmp[j][m - i - 1];
    }
    if (out.first == inf)
      cout << "NA" << endl;
    else
      cout << out.second + 1 << " " << out.first + 1 << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#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()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e9;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
int main() {
  int n, m;
  while (scanf("%d %d", &n, &m), n) {
    vvi in(n, vi(n));
    vvi pic(m, vi(m));
    rep(i, n) rep(j, n) scanf("%d", &in[i][j]);
    rep(i, m) rep(j, m) scanf("%d", &pic[i][j]);
    pii out(inf, inf);
    rep(e, 4) {
      rep(i, n - m + 1) rep(j, n - m + 1) {
        pii t(inf, inf);
        rep(k, m) rep(l, m) if (pic[k][l] != -1 &&
                                in[i + k][j + l] != pic[k][l]) goto en;
        else if (pic[k][l] != -1) t = min(t, pii(i + k, j + l));
        out = min(out, t);
        goto end;
      en:;
      }
    end:;
      vvi tmp = pic;
      rep(i, m) rep(j, m) pic[i][j] = tmp[j][m - i - 1];
    }
    if (out.first == inf)
      cout << "NA" << endl;
    else
      cout << out.second + 1 << " " << out.first + 1 << endl;
  }
}
Problem p00209 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
using namespace std;
int n, m, a[111][111], b[55][55];
void rotate() {
  int z[55][55];
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < m; j++) {
      z[i][j] = b[m - j - 1][i];
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      b[i][j] = z[i][j];
    }
  }
}
int main() {
  while (cin >> n >> m, n | m) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cin >> a[i][j];
      }
    }
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < m; j++) {
        cin >> b[i][j];
      }
    }
    int rx = n, ry = n;
    for (int i = 0; i < 4; i++) {
      int sx = m;
      for (int j = m - 1; j >= 0; j--) {
        if (b[0][j] != -1)
          sx = j;
      }
      for (int j = 0; j <= n - m; j++) {
        for (int k = 0; k <= n - m; k++) {
          bool flag = true;
          for (int ey = j; ey < j + m; ey++) {
            for (int ex = k; ex < k + m; ex++) {
              int v = b[ey - j][ex - k];
              if (v != -1 && a[ey][ex] != v)
                flag = false;
            }
          }
          if (flag && (ry > j || (ry == j && rx > k + sx))) {
            rx = k + sx, ry = j;
          }
        }
      }
      rotate();
    }
    if (rx == n && ry == n)
      cout << "NA" << endl;
    else
      cout << rx + 1 << ' ' << ry + 1 << endl;
  }
  return 0;
}

Fixed submission:
#include <iostream>
using namespace std;
int n, m, a[111][111], b[55][55];
void rotate() {
  int z[55][55];
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < m; j++) {
      z[i][j] = b[m - j - 1][i];
    }
  }
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < m; j++) {
      b[i][j] = z[i][j];
    }
  }
}
int main() {
  while (cin >> n >> m, n | m) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        cin >> a[i][j];
      }
    }
    for (int i = 0; i < m; i++) {
      for (int j = 0; j < m; j++) {
        cin >> b[i][j];
      }
    }
    int rx = n, ry = n;
    for (int i = 0; i < 4; i++) {
      int sx = m;
      for (int j = m - 1; j >= 0; j--) {
        if (b[0][j] != -1)
          sx = j;
      }
      for (int j = 0; j <= n - m; j++) {
        for (int k = 0; k <= n - m; k++) {
          bool flag = true;
          for (int ey = j; ey < j + m; ey++) {
            for (int ex = k; ex < k + m; ex++) {
              int v = b[ey - j][ex - k];
              if (v != -1 && a[ey][ex] != v)
                flag = false;
            }
          }
          if (flag && (ry > j || (ry == j && rx > k + sx))) {
            rx = k + sx, ry = j;
          }
        }
      }
      rotate();
    }
    if (rx == n && ry == n)
      cout << "NA" << endl;
    else
      cout << rx + 1 << ' ' << ry + 1 << endl;
  }
  return 0;
}
Problem p00211 (C++)

Buggy submission (status: Time Limit Exceeded):
// 26
#include <iostream>

using namespace std;

long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }

int main() {
  for (int n; cin >> n, n;) {
    int d[10], v[10];
    for (int i = 0; i < n; i++) {
      cin >> d[i] >> v[i];
    }
    int m[9];
    for (int i = 0; i < n - 1; i++) {
      for (long long j = 1;; j++) {
        if (j * d[0] * v[i + 1] % (v[0] * d[i + 1]) == 0) {
          m[i] = j;
          break;
        }
      }
    }
    long long f = 1;
    for (int i = 0; i < n - 1; i++) {
      f = f * m[i] / gcd(f, m[i]);
    }
    cout << f << endl;
    for (int i = 1; i < n; i++) {
      cout << f * d[0] * v[i] / v[0] / d[i] << endl;
    }
  }
  return 0;
}


Fixed submission:
// 26
#include <iostream>

using namespace std;

long long gcd(long long a, long long b) { return b ? gcd(b, a % b) : a; }

int main() {
  for (int n; cin >> n, n;) {
    int d[10], v[10];
    for (int i = 0; i < n; i++) {
      cin >> d[i] >> v[i];
    }
    int m[9];
    for (int i = 0; i < n - 1; i++) {
      m[i] = d[i + 1] * v[0] / gcd(d[0] * v[i + 1], d[i + 1] * v[0]);
    }
    long long f = 1;
    for (int i = 0; i < n - 1; i++) {
      f = f * m[i] / gcd(f, m[i]);
    }
    cout << f << endl;
    for (int i = 1; i < n; i++) {
      cout << f * d[0] * v[i] / v[0] / d[i] << endl;
    }
  }
  return 0;
}
Problem p00211 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;
typedef long long ll;

ll gcd(ll a, ll b) {
  if (a == 0 || b == 0)
    return 0;

  while (a != b) {
    if (a > b)
      a -= b;
    else
      b -= a;
  }
  return a;
}

ll lcm(ll a, ll b) {
  if (a == 0 || b == 0)
    return 0;
  return ((a / gcd(a, b) * b));
}

int main() {
  int n;
  while (cin >> n && n) {
    vector<pair<ll, ll>> v;

    rep(i, n) {
      ll a, b;
      cin >> a >> b;
      v.push_back(make_pair(a, b));
    }

    rep(i, n) {
      ll temp = gcd(v[i].first, v[i].second);
      v[i].first /= temp;
      v[i].second /= temp;
    }

    ll t, t2;
    rep(i, n) {
      if (i == 0) {
        t = v[i].first;
        t2 = v[i].second;
      } else {
        t = lcm(t, v[i].first);
        t2 = gcd(t2, v[i].second);
      }
    }

    rep(i, n) {
      ll ans = t * v[i].second;
      ans /= t2;
      ans /= v[i].first;
      cout << ans << endl;
    }
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;
typedef long long ll;

ll gcd(ll a, ll b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}

ll lcm(ll a, ll b) {
  if (a == 0 || b == 0)
    return 0;
  return ((a / gcd(a, b) * b));
}

int main() {
  int n;
  while (cin >> n && n) {
    vector<pair<ll, ll>> v;

    rep(i, n) {
      ll a, b;
      cin >> a >> b;
      v.push_back(make_pair(a, b));
    }

    rep(i, n) {
      ll temp = gcd(v[i].first, v[i].second);
      v[i].first /= temp;
      v[i].second /= temp;
    }

    ll t, t2;
    rep(i, n) {
      if (i == 0) {
        t = v[i].first;
        t2 = v[i].second;
      } else {
        t = lcm(t, v[i].first);
        t2 = gcd(t2, v[i].second);
      }
    }

    rep(i, n) {
      ll ans = t * v[i].second;
      ans /= t2;
      ans /= v[i].first;
      cout << ans << endl;
    }
  }
  return 0;
}
Problem p00212 (C++)

Buggy submission (status: Memory Limit Exceeded):
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

struct node {
  int cost;
  int c;
  int now;
  node(int C, int cc, int n) : cost(C), c(cc), now(n) {}
  bool operator>(const node &e) const { return cost > e.cost; }
};

int main() {
  int c, n, m, s, d;

  for (; cin >> c >> n >> m >> s >> d, c;) {
    int edge[n][n];
    bool close[c + 1][n];
    memset(edge, -1, sizeof(edge));
    memset(close, false, sizeof(close));
    for (int i = 0; i < m; i++) {
      int a, b, f;
      cin >> a >> b >> f;
      a--;
      b--;
      edge[a][b] = f;
      edge[b][a] = f;
    }
    s--;
    d--;
    priority_queue<node, vector<node>, greater<node>> qu;
    qu.push(node(0, c, s));
    while (!qu.empty()) {
      node tmp = qu.top();
      //	  cout<<tmp.now<<" "<<tmp.cost<<" "<<tmp.c<<endl;
      qu.pop();
      close[tmp.c][tmp.now] = true;
      if (tmp.now == d) {
        cout << tmp.cost << endl;
        break;
      }
      for (int i = 0; i < n; i++) {
        if (edge[tmp.now][i] != -1) {
          if (!close[tmp.c][i])
            qu.push(node(edge[tmp.now][i] + tmp.cost, tmp.c, i));
          if (tmp.c > 0 && !close[tmp.c - 1][i])
            qu.push(node(edge[tmp.now][i] / 2 + tmp.cost, tmp.c - 1, i));
        }
      }
    }
  }
}

Fixed submission:
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

struct node {
  int cost;
  int c;
  int now;
  node(int C, int cc, int n) : cost(C), c(cc), now(n) {}
  bool operator>(const node &e) const { return cost > e.cost; }
};

int main() {
  int c, n, m, s, d;

  for (; cin >> c >> n >> m >> s >> d, c;) {
    int edge[n][n];
    bool close[c + 1][n];
    memset(edge, -1, sizeof(edge));
    memset(close, false, sizeof(close));
    for (int i = 0; i < m; i++) {
      int a, b, f;
      cin >> a >> b >> f;
      a--;
      b--;
      edge[a][b] = f;
      edge[b][a] = f;
    }
    s--;
    d--;
    priority_queue<node, vector<node>, greater<node>> qu;
    qu.push(node(0, c, s));
    while (!qu.empty()) {
      node tmp = qu.top();
      //	  cout<<tmp.now<<" "<<tmp.cost<<" "<<tmp.c<<endl;
      qu.pop();
      if (close[tmp.c][tmp.now])
        continue;
      close[tmp.c][tmp.now] = true;
      if (tmp.now == d) {
        cout << tmp.cost << endl;
        break;
      }
      for (int i = 0; i < n; i++) {
        if (edge[tmp.now][i] != -1) {
          if (!close[tmp.c][i])
            qu.push(node(edge[tmp.now][i] + tmp.cost, tmp.c, i));
          if (tmp.c > 0 && !close[tmp.c - 1][i])
            qu.push(node(edge[tmp.now][i] / 2 + tmp.cost, tmp.c - 1, i));
        }
      }
    }
  }
}
Problem p00212 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 28

using namespace std;
typedef pair<int, int> P;

struct edge {
  int to;
  int cost;

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

int d[101][11];
vector<edge> G[101];

void dijkstra(int s, int n, int ticket) {
  priority_queue<P, vector<P>, greater<P>> que;
  rep(i, n) d[i][ticket] = INF;
  d[s][ticket] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();

    int v = p.second;
    if (d[v][ticket] < p.first)
      continue;

    rep(i, G[v].size()) {
      edge e = G[v][i];
      int temp;

      if (ticket == 0) {
        temp = d[v][ticket] + e.cost;
      } else {
        temp = min(d[v][ticket] + e.cost, d[v][ticket - 1] + e.cost / 2);
      }

      if (d[e.to][ticket] > temp) {
        d[e.to][ticket] = temp;
        que.push(P(d[e.to][ticket], e.to));
      }
    }
  }
}

int main() {
  int c, n, m, s, t;
  while (cin >> c >> n >> m >> s >> t) {
    if (c == 0 && n == 0 && m == 0 && s == 0 && t == 0)
      break;

    s--;
    t--;
    rep(i, 100) G[i].clear();

    rep(i, m) {
      int a, b, cost;
      cin >> a >> b >> cost;
      a--;
      b--;

      edge e(b, cost);
      G[a].push_back(e);

      edge e2(a, cost);
      G[b].push_back(e2);
    }

    rep(i, n) dijkstra(s, n, i);

    cout << d[t][c] << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1 << 28

using namespace std;
typedef pair<int, int> P;

struct edge {
  int to;
  int cost;

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

int d[101][11];
vector<edge> G[101];

void dijkstra(int s, int n, int ticket) {
  priority_queue<P, vector<P>, greater<P>> que;
  rep(i, n) d[i][ticket] = INF;
  d[s][ticket] = 0;
  que.push(P(0, s));

  while (!que.empty()) {
    P p = que.top();
    que.pop();

    int v = p.second;
    if (d[v][ticket] < p.first)
      continue;

    rep(i, G[v].size()) {
      edge e = G[v][i];
      int temp;

      if (ticket == 0) {
        temp = d[v][ticket] + e.cost;
      } else {
        temp = min(d[v][ticket] + e.cost, d[v][ticket - 1] + e.cost / 2);
      }

      if (d[e.to][ticket] > temp) {
        d[e.to][ticket] = temp;
        que.push(P(d[e.to][ticket], e.to));
      }
    }
  }
}

int main() {
  int c, n, m, s, t;
  while (cin >> c >> n >> m >> s >> t) {
    if (c == 0 && n == 0 && m == 0 && s == 0 && t == 0)
      break;

    s--;
    t--;
    rep(i, 100) G[i].clear();

    rep(i, m) {
      int a, b, cost;
      cin >> a >> b >> cost;
      a--;
      b--;

      edge e(b, cost);
      G[a].push_back(e);

      edge e2(a, cost);
      G[b].push_back(e2);
    }

    rep(i, c + 1) dijkstra(s, n, i);

    cout << d[t][c] << endl;
  }

  return 0;
}
Problem p00212 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

struct state {
  int node;
  int ticket;
  int cost;
  state(int n, int t, int c) : node(n), ticket(t), cost(c) {}
  bool operator<(const state &s) const { return cost > s.cost; }
};

int main() {
  int c, n, m, s, d;
  while (cin >> c >> n >> m >> s >> d && c != 0) {
    --s;
    --d;
    vector<vector<pair<int, int>>> g(n);
    for (int i = 0; i < m; i++) {
      int a, b, f;
      cin >> a >> b >> f;
      --a;
      --b;
      g[a].push_back(make_pair(b, f));
      g[b].push_back(make_pair(a, f));
    }

    priority_queue<state> q;
    q.push(state(s, c, 0));
    vector<vector<int>> city(n, vector<int>(c, 1000000));
    city[s][c] = 0;
    while (!q.empty()) {
      const state s = q.top();
      q.pop();
      if (s.node == d) {
        cout << s.cost << endl;
        break;
      }

      city[s.node][s.ticket] = s.cost;
      for (vector<pair<int, int>>::const_iterator it(g[s.node].begin());
           it != g[s.node].end(); ++it) {
        const int next = it->first;
        const int cost = s.cost + it->second;
        if (cost < city[next][s.ticket]) {
          city[next][s.ticket] = cost;
          q.push(state(next, s.ticket, cost));
        }
        if (s.ticket > 0) {
          const int cost2 = s.cost + it->second / 2;
          if (cost2 < city[next][s.ticket - 1]) {
            city[next][s.ticket - 1] = cost2;
            q.push(state(next, s.ticket - 1, cost2));
          }
        }
      }
    }
  }
  return 0;
}

Fixed submission:
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

struct state {
  int node;
  int ticket;
  int cost;
  state(int n, int t, int c) : node(n), ticket(t), cost(c) {}
  bool operator<(const state &s) const { return cost > s.cost; }
};

int main() {
  int c, n, m, s, d;
  while (cin >> c >> n >> m >> s >> d && c != 0) {
    --s;
    --d;
    vector<vector<pair<int, int>>> g(n);
    for (int i = 0; i < m; i++) {
      int a, b, f;
      cin >> a >> b >> f;
      --a;
      --b;
      g[a].push_back(make_pair(b, f));
      g[b].push_back(make_pair(a, f));
    }

    priority_queue<state> q;
    q.push(state(s, c, 0));
    vector<vector<int>> city(n, vector<int>(c + 1, 1000000));
    city[s][c] = 0;
    while (!q.empty()) {
      const state s = q.top();
      q.pop();
      if (s.node == d) {
        cout << s.cost << endl;
        break;
      }

      city[s.node][s.ticket] = s.cost;
      for (vector<pair<int, int>>::const_iterator it(g[s.node].begin());
           it != g[s.node].end(); ++it) {
        const int next = it->first;
        const int cost = s.cost + it->second;
        if (cost < city[next][s.ticket]) {
          city[next][s.ticket] = cost;
          q.push(state(next, s.ticket, cost));
        }
        if (s.ticket > 0) {
          const int cost2 = s.cost + it->second / 2;
          if (cost2 < city[next][s.ticket - 1]) {
            city[next][s.ticket - 1] = cost2;
            q.push(state(next, s.ticket - 1, cost2));
          }
        }
      }
    }
  }
  return 0;
}
Problem p00212 (C++)

Buggy submission (status: Runtime Error):
// AOJ 0212
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cstdio>
#include <functional>
#include <queue>
#include <vector>
#define rep(i, a) for (int i = 0; i != (a); ++i)

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

const int INF = 1 << 30;

int c, n, m, s, d;
std::vector<P> G[100];
int dist[100][11];

int dijkstra() {
  std::fill((int *)dist, (int *)(dist + 100), INF);
  dist[s][0] = 0;
  std::priority_queue<PP, std::vector<PP>, std::greater<PP>> pque;
  pque.push(PP(P(s, 0), 0));
  while (!pque.empty()) {
    PP p = pque.top();
    pque.pop();
    int v = p.first.first, cost = p.first.second, tic = p.second;

    if (dist[v][tic] < cost)
      continue;

    rep(i, G[v].size()) {
      P &e = G[v][i];

      if (tic < c && dist[e.first][tic + 1] > dist[v][tic] + e.second / 2) {
        dist[e.first][tic + 1] = dist[v][tic] + e.second / 2;
        pque.push(PP(P(e.first, dist[e.first][tic + 1]), tic + 1));
      }

      if (dist[e.first][tic] > dist[v][tic] + e.second) {
        dist[e.first][tic] = dist[v][tic] + e.second;
        pque.push(PP(P(e.first, dist[e.first][tic]), tic));
      }
    }
  }

  return *std::min_element(dist[d], dist[d] + 11);
}

int main() {
  while (scanf("%d%d%d%d%d", &c, &n, &m, &s, &d), c | n | m | s | d) {
    --s;
    --d;
    rep(i, 101) G[i].clear();

    rep(i, m) {
      int a, b, f;
      scanf("%d%d%d", &a, &b, &f);
      --a;
      --b;
      G[a].push_back(P(b, f));
      G[b].push_back(P(a, f));
    }

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

  return 0;
}

Fixed submission:
// AOJ 0212
#define _CRT_SECURE_NO_WARNINGS
#include <algorithm>
#include <cstdio>
#include <functional>
#include <queue>
#include <vector>
#define rep(i, a) for (int i = 0; i != (a); ++i)

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

const int INF = 1 << 30;

int c, n, m, s, d;
std::vector<P> G[100];
int dist[100][11];

int dijkstra() {
  std::fill((int *)dist, (int *)(dist + 100), INF);
  dist[s][0] = 0;
  std::priority_queue<PP, std::vector<PP>, std::greater<PP>> pque;
  pque.push(PP(P(s, 0), 0));
  while (!pque.empty()) {
    PP p = pque.top();
    pque.pop();
    int v = p.first.first, cost = p.first.second, tic = p.second;

    if (dist[v][tic] < cost)
      continue;

    rep(i, G[v].size()) {
      P &e = G[v][i];

      if (tic < c && dist[e.first][tic + 1] > dist[v][tic] + e.second / 2) {
        dist[e.first][tic + 1] = dist[v][tic] + e.second / 2;
        pque.push(PP(P(e.first, dist[e.first][tic + 1]), tic + 1));
      }

      if (dist[e.first][tic] > dist[v][tic] + e.second) {
        dist[e.first][tic] = dist[v][tic] + e.second;
        pque.push(PP(P(e.first, dist[e.first][tic]), tic));
      }
    }
  }

  return *std::min_element(dist[d], dist[d] + 11);
}

int main() {
  while (scanf("%d%d%d%d%d", &c, &n, &m, &s, &d), c | n | m | s | d) {
    --s;
    --d;
    rep(i, 100) G[i].clear();

    rep(i, m) {
      int a, b, f;
      scanf("%d%d%d", &a, &b, &f);
      --a;
      --b;
      G[a].push_back(P(b, f));
      G[b].push_back(P(a, f));
    }

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

  return 0;
}
Problem p00217 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <limits>
#include <math.h>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <string>

using namespace std;

int main(void) {
  for (int u = 0; u < 50; u++) {
    long n;
    long id[51], a[51], b[51];
    int p = -1;
    int q = 0;

    cin >> n;
    if (n == 0) {
      break;
    }

    for (long i = 0; i < n; i++) {
      cin >> id[i] >> a[i] >> b[i];
    }

    for (long i = 0; i < n; i++) {
      if (a[i] + b[i] > q) {
        p = 0;
        p = id[i];
        q = 0;
        q = a[i] + b[i];
      }
    }

    cout << p << " " << q << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <limits>
#include <math.h>
#include <sstream>
#include <stdio.h>
#include <string.h>
#include <string>

using namespace std;

int main(void) {
  for (int u = 0; u < 50; u++) {
    long n;
    long id[10000], a[10000], b[10000];
    int p = -1;
    int q = 0;

    cin >> n;
    if (n == 0) {
      break;
    }

    for (long i = 0; i < n; i++) {
      cin >> id[i] >> a[i] >> b[i];
    }

    for (long i = 0; i < n; i++) {
      if (a[i] + b[i] > q) {
        p = 0;
        p = id[i];
        q = 0;
        q = a[i] + b[i];
      }
    }

    cout << p << " " << q << endl;
  }
}