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

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

const int MAX_N = 100;
const int INF = (1 << 21);

int N;
vector<pair<int, int>> A[MAX_N];

void dj() {
  int d[MAX_N], c[MAX_N];

  // (first: distance*-1, second: adjacent)
  priority_queue<pair<int, int>> PQ;

  for (int i = 0; i < N; i++) {
    d[i] = INF;
    c[i] = -1;
  }

  d[0] = c[0] = 0;
  PQ.push(make_pair(0, 0));

  while (!PQ.empty()) {
    pair<int, int> t = PQ.top();
    PQ.pop();
    int cur_n = t.second;
    c[cur_n] = 1;

    if (d[cur_n] < t.first * -1)
      continue;

    for (int i = 0; i < A[cur_n].size(); i++) {
      int adj_n = A[cur_n][i].first;

      if (c[adj_n] == 1)
        continue;

      if (d[cur_n] + A[cur_n][i].second < d[adj_n]) {
        d[adj_n] = d[cur_n] + A[cur_n][i].second;
        PQ.push(make_pair(d[adj_n] * -1, adj_n));
        c[adj_n] = 0;
      }
    }
  }

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

int main() {
  cin >> N;

  for (int i = 0; i < N; i++) {
    int cur_n, k;
    cin >> cur_n >> k;

    for (int j = 0; j < k; j++) {
      int u, v;
      cin >> u >> v;
      A[cur_n].push_back(make_pair(u, v));
    }
  }

  dj();

  return 0;
}

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

const int MAX_N = 10000;
const int INF = (1 << 21);

int N;
vector<pair<int, int>> A[MAX_N];

void dj() {
  int d[MAX_N], c[MAX_N];

  // (first: distance*-1, second: adjacent)
  priority_queue<pair<int, int>> PQ;

  for (int i = 0; i < N; i++) {
    d[i] = INF;
    c[i] = -1;
  }

  d[0] = c[0] = 0;
  PQ.push(make_pair(0, 0));

  while (!PQ.empty()) {
    pair<int, int> t = PQ.top();
    PQ.pop();
    int cur_n = t.second;
    c[cur_n] = 1;

    if (d[cur_n] < t.first * -1)
      continue;

    for (int i = 0; i < A[cur_n].size(); i++) {
      int adj_n = A[cur_n][i].first;

      if (c[adj_n] == 1)
        continue;

      if (d[cur_n] + A[cur_n][i].second < d[adj_n]) {
        d[adj_n] = d[cur_n] + A[cur_n][i].second;
        PQ.push(make_pair(d[adj_n] * -1, adj_n));
        c[adj_n] = 0;
      }
    }
  }

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

int main() {
  cin >> N;

  for (int i = 0; i < N; i++) {
    int cur_n, k;
    cin >> cur_n >> k;

    for (int j = 0; j < k; j++) {
      int u, v;
      cin >> u >> v;
      A[cur_n].push_back(make_pair(u, v));
    }
  }

  dj();

  return 0;
}
Problem p02243 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <functional>
#include <queue>
#include <utility>
#include <vector>
using namespace std;

typedef pair<int, int> pii;

int main() {
  int n;
  scanf("%d", &n);
  vector<vector<pii>> G(n);
  for (int i = 0; i < n; ++i) {
    int u, k, v, c;
    scanf("%d%d", &u, &k);
    for (int j = 0; j < k; ++j) {
      scanf("%d%d", &v, &c);
      G[u].push_back(pii(c, v));
    }
  }

  vector<int> d(n, 1 << 30);
  d[0] = 0;
  priority_queue<pii, vector<pii>, greater<pii>> pq;
  pq.push(pii(0, 0));
  while (!pq.empty()) {
    int c = pq.top().first;
    int u = pq.top().second;
    if (d[u] != c) {
      continue;
    }
    for (int i = 0; i < G[u].size(); ++i) {
      int b = c + G[u][i].first;
      int v = G[u][i].second;
      if (d[v] > b) {
        d[v] = b;
        pq.push(pii(b, v));
      }
    }
  }

  for (int i = 0; i < n; ++i) {
    printf("%d %d\n", i, d[i]);
  }
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <functional>
#include <queue>
#include <utility>
#include <vector>
using namespace std;

typedef pair<int, int> pii;

int main() {
  int n;
  scanf("%d", &n);
  vector<vector<pii>> G(n);
  for (int i = 0; i < n; ++i) {
    int u, k, v, c;
    scanf("%d%d", &u, &k);
    for (int j = 0; j < k; ++j) {
      scanf("%d%d", &v, &c);
      G[u].push_back(pii(c, v));
    }
  }

  vector<int> d(n, 1 << 30);
  d[0] = 0;
  priority_queue<pii, vector<pii>, greater<pii>> pq;
  pq.push(pii(0, 0));
  while (!pq.empty()) {
    int c = pq.top().first;
    int u = pq.top().second;
    pq.pop();
    if (d[u] != c) {
      continue;
    }
    for (int i = 0; i < G[u].size(); ++i) {
      int b = c + G[u][i].first;
      int v = G[u][i].second;
      if (d[v] > b) {
        d[v] = b;
        pq.push(pii(b, v));
      }
    }
  }

  for (int i = 0; i < n; ++i) {
    printf("%d %d\n", i, d[i]);
  }
}
Problem p02243 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

const int MAX = 105;
int state[MAX], N, D[MAX];
vector<pair<int, int>> S[MAX];
priority_queue<pair<int, int>> Q;

void dijkstra() {
  int cnt = 1;
  for (int i = 0; i < N; i++) {
    state[i] = 0;
    D[i] = 10000000;
  }
  state[0] = 1;
  D[0] = 0;
  for (int i = 0; i < S[0].size(); i++) {
    Q.push(make_pair(-S[0][i].second, S[0][i].first));
    D[S[0][i].first] = min(D[S[0][i].first], D[0] + S[0][i].second);
  }
  while (1) {
    int a = Q.top().second;
    if (state[a] == 0) {
      Q.pop();
      state[a] = 1;
      for (int i = 0; i < S[a].size(); i++) {
        if (state[S[a][i].first] == 0) {
          Q.push(make_pair(-(D[a] + S[a][i].second), S[a][i].first));
          D[S[a][i].first] = min(D[S[a][i].first], D[a] + S[a][i].second);
        }
      }
      cnt++;
    } else
      Q.pop();
    if (cnt == N)
      break;
  }
  return;
}

int main() {

  cin >> N;
  for (int i = 0; i < N; i++) {
    int l, a;
    cin >> l >> a;
    for (int j = 0; j < a; j++) {
      int b, c;
      cin >> b >> c;
      S[i].push_back(make_pair(b, c));
    }
  }
  dijkstra();
  for (int i = 0; i < N; i++) {
    cout << i << " " << D[i] << endl;
  }
}


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

const int MAX = 10005;
int state[MAX], N, D[MAX];
vector<pair<int, int>> S[MAX];
priority_queue<pair<int, int>> Q;

void dijkstra() {
  int cnt = 1;
  for (int i = 0; i < N; i++) {
    state[i] = 0;
    D[i] = 10000000;
  }
  state[0] = 1;
  D[0] = 0;
  for (int i = 0; i < S[0].size(); i++) {
    Q.push(make_pair(-S[0][i].second, S[0][i].first));
    D[S[0][i].first] = min(D[S[0][i].first], D[0] + S[0][i].second);
  }
  while (1) {
    int a = Q.top().second;
    if (state[a] == 0) {
      Q.pop();
      state[a] = 1;
      for (int i = 0; i < S[a].size(); i++) {
        if (state[S[a][i].first] == 0) {
          Q.push(make_pair(-(D[a] + S[a][i].second), S[a][i].first));
          D[S[a][i].first] = min(D[S[a][i].first], D[a] + S[a][i].second);
        }
      }
      cnt++;
    } else
      Q.pop();
    if (cnt == N)
      break;
  }
  return;
}

int main() {

  cin >> N;
  for (int i = 0; i < N; i++) {
    int l, a;
    cin >> l >> a;
    for (int j = 0; j < a; j++) {
      int b, c;
      cin >> b >> c;
      S[i].push_back(make_pair(b, c));
    }
  }
  dijkstra();
  for (int i = 0; i < N; i++) {
    cout << i << " " << D[i] << endl;
  }
}
Problem p02243 (C++)

Buggy submission (status: Runtime Error):
#include <climits>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
using namespace std;

typedef map<int, int>::iterator mitr;

map<int, int> w[100];
int d[100];
char pos[100];

priority_queue<pair<int, int>> others;
pair<int, int> p;

void findPath() {
  p = others.top();
  others.pop();
  while (pos[p.second]) {
    p = others.top();
    others.pop();
  }
  pos[p.second] = 1;

  int tmpu = p.second;
  int tmpd = -p.first;

  for (mitr i = w[tmpu].begin(); i != w[tmpu].end(); i++) {
    if (tmpd + i->second < d[i->first]) {
      d[i->first] = tmpd + i->second;
      others.push(make_pair(-d[i->first], i->first));
    }
  }
}

int main() {
  int i, j, u, k, n, v, c;

  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d %d", &u, &k);
    for (j = 0; j < k; j++) {
      scanf("%d %d", &v, &c);
      w[u][v] = c;
    }
  }

  memset(pos, 0, sizeof(char) * 100);
  for (i = 0; i < n; i++)
    d[i] = INT_MAX;
  d[0] = 0;
  others.push(make_pair(0, 0));
  for (i = 0; i < n; i++)
    findPath();
  for (i = 0; i < n; i++)
    printf("%d %d\n", i, d[i]);

  return 0;
}


Fixed submission:
#include <climits>
#include <cstdio>
#include <cstring>
#include <map>
#include <queue>
using namespace std;

typedef map<int, int>::iterator mitr;

map<int, int> w[10000];
int d[10000];
char pos[10000];

priority_queue<pair<int, int>> others;
pair<int, int> p;

void findPath() {
  p = others.top();
  others.pop();
  while (pos[p.second]) {
    p = others.top();
    others.pop();
  }
  pos[p.second] = 1;

  int tmpu = p.second;
  int tmpd = -p.first;

  for (mitr i = w[tmpu].begin(); i != w[tmpu].end(); i++) {
    if (tmpd + i->second < d[i->first]) {
      d[i->first] = tmpd + i->second;
      others.push(make_pair(-d[i->first], i->first));
    }
  }
}

int main() {
  int i, j, u, k, n, v, c;

  scanf("%d", &n);
  for (i = 0; i < n; i++) {
    scanf("%d %d", &u, &k);
    for (j = 0; j < k; j++) {
      scanf("%d %d", &v, &c);
      w[u][v] = c;
    }
  }

  memset(pos, 0, sizeof(char) * 100);
  for (i = 0; i < n; i++)
    d[i] = INT_MAX;
  d[0] = 0;
  others.push(make_pair(0, 0));
  for (i = 0; i < n; i++)
    findPath();
  for (i = 0; i < n; i++)
    printf("%d %d\n", i, d[i]);

  return 0;
}
Problem p02243 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <limits.h>
#include <queue>
#include <vector>

using namespace std;

struct Node {
  int id;
  int cost;
  bool done;
  vector<int> next_node;
  vector<int> len_cost;

  bool operator<(const Node &right) const { return cost > right.cost; }
} node[100];

int main() {
  int n, id, k;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> id >> k;
    node[id].id = id;
    node[id].cost = INT_MAX;
    node[id].done = false;
    node[id].next_node.resize(k);
    node[id].len_cost.resize(k);
    for (int j = 0; j < k; j++) {
      cin >> node[id].next_node[j] >> node[id].len_cost[j];
    }
  }

  priority_queue<Node> q;
  node[0].cost = 0;
  q.push(node[0]);
  while (!q.empty()) {
    Node temp = q.top();
    q.pop();

    if (temp.done == true) {
      continue;
    }
    node[temp.id].done = true;

    for (int i = 0; i < temp.next_node.size(); i++) {
      int to = temp.next_node[i];
      int cost = temp.cost + temp.len_cost[i];
      if (node[to].cost > cost) {
        node[to].cost = cost;
        if (node[to].done == false) {
          q.push(node[to]);
        }
      }
    }
  }

  for (int i = 0; i < n; i++) {
    cout << i << " " << node[i].cost << endl;
  }

  return 0;
}

Fixed submission:
#include <iostream>
#include <limits.h>
#include <queue>
#include <vector>

using namespace std;

struct Node {
  int id;
  int cost;
  bool done;
  vector<int> next_node;
  vector<int> len_cost;

  bool operator<(const Node &right) const { return cost > right.cost; }
} node[10000];

int main() {
  int n, id, k;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> id >> k;
    node[id].id = id;
    node[id].cost = INT_MAX;
    node[id].done = false;
    node[id].next_node.resize(k);
    node[id].len_cost.resize(k);
    for (int j = 0; j < k; j++) {
      cin >> node[id].next_node[j] >> node[id].len_cost[j];
    }
  }

  priority_queue<Node> q;
  node[0].cost = 0;
  q.push(node[0]);
  while (!q.empty()) {
    Node temp = q.top();
    q.pop();

    if (temp.done == true) {
      continue;
    }
    node[temp.id].done = true;

    for (int i = 0; i < temp.next_node.size(); i++) {
      int to = temp.next_node[i];
      int cost = temp.cost + temp.len_cost[i];
      if (node[to].cost > cost) {
        node[to].cost = cost;
        if (node[to].done == false) {
          q.push(node[to]);
        }
      }
    }
  }

  for (int i = 0; i < n; i++) {
    cout << i << " " << node[i].cost << endl;
  }

  return 0;
}
Problem p02243 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define rep(index, num) for (int index = 0; index < num; index++)
#define rep1(index, num) for (int index = 1; index <= num; index++)
#define scan(argument) cin >> argument
#define prin(argument) cout << argument << endl
#define kaigyo cout << endl
#define eps 1e-15
#define mp(a1, a2) make_pair(a1, a2)
typedef long long ll;
using namespace std;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
int INF = 1e+9 + 1;
int N;
vpint adj[10000];
int d[100], color[100], p[100];
void dijkstra() {
  priority_queue<pint> PQ;
  rep(i, N) {
    d[i] = INF;
    color[i] = 0;
  }
  d[0] = 0;
  color[0] = 1;
  PQ.push(mp(0, 0));
  while (!PQ.empty()) {
    pint f = PQ.top();
    PQ.pop();
    int u = f.second;
    color[u] = 2;
    if (d[u] < -f.first)
      continue;
    rep(j, adj[u].size()) {
      int v = adj[u][j].first;
      if (color[v] == 2)
        continue;
      if (d[v] > d[u] + adj[u][j].second) {
        d[v] = d[u] + adj[u][j].second;
        PQ.push(mp(-d[v], v));
        color[v] = 1;
      }
    }
  }
}
int main() {
  scan(N);
  int k, c, u, v;
  rep(i, N) {
    scan(u);
    scan(k);
    rep(j, k) {
      scan(v);
      scan(c);
      adj[u].push_back(mp(v, c));
    }
  }
  dijkstra();
  rep(i, N) { printf("%d %d\n", i, (d[i] == INF ? -1 : d[i])); }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <vector>
#define rep(index, num) for (int index = 0; index < num; index++)
#define rep1(index, num) for (int index = 1; index <= num; index++)
#define scan(argument) cin >> argument
#define prin(argument) cout << argument << endl
#define kaigyo cout << endl
#define eps 1e-15
#define mp(a1, a2) make_pair(a1, a2)
typedef long long ll;
using namespace std;
typedef pair<int, int> pint;
typedef vector<int> vint;
typedef vector<pint> vpint;
int INF = 1e+9 + 1;
int N;
vpint adj[10000];
int d[10000], color[10000], p[10000];
void dijkstra() {
  priority_queue<pint> PQ;
  rep(i, N) {
    d[i] = INF;
    color[i] = 0;
  }
  d[0] = 0;
  color[0] = 1;
  PQ.push(mp(0, 0));
  while (!PQ.empty()) {
    pint f = PQ.top();
    PQ.pop();
    int u = f.second;
    color[u] = 2;
    if (d[u] < -f.first)
      continue;
    rep(j, adj[u].size()) {
      int v = adj[u][j].first;
      if (color[v] == 2)
        continue;
      if (d[v] > d[u] + adj[u][j].second) {
        d[v] = d[u] + adj[u][j].second;
        PQ.push(mp(-d[v], v));
        color[v] = 1;
      }
    }
  }
}
int main() {
  scan(N);
  int k, c, u, v;
  rep(i, N) {
    scan(u);
    scan(k);
    rep(j, k) {
      scan(v);
      scan(c);
      adj[u].push_back(mp(v, c));
    }
  }
  dijkstra();
  rep(i, N) { printf("%d %d\n", i, (d[i] == INF ? -1 : d[i])); }
  return 0;
}
Problem p02243 (C++)

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

static const int MAX = 1000;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n;
vector<pair<int, int>> adj[MAX];

void dijkstra() {
  priority_queue<pair<int, int>> PQ;
  int color[MAX];
  int d[MAX];

  for (int i = 0; i < n; i++) {
    d[i] = INT_MAX;
    color[i] = WHITE;
  }

  d[0] = 0;
  PQ.push(make_pair(0, 0));
  color[0] = GRAY;

  while (!PQ.empty()) {
    int u;
    pair<int, int> f = PQ.top();
    PQ.pop();
    u = f.second;

    color[u] = BLACK;

    for (int i = 0; i < adj[u].size(); i++) {
      int v = adj[u][i].first;
      if (color[v] == BLACK)
        continue;
      if (d[u] + adj[u][i].second < d[v]) {
        d[v] = d[u] + adj[u][i].second;
        PQ.push(make_pair(d[v] * (-1), v));
        color[v] = GRAY;
      }
    }
  }

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

int main() {
  int k, u, v, c;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      cin >> v >> c;
      adj[u].push_back(make_pair(v, c));
    }
  }

  dijkstra();

  return 0;
}

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

static const int MAX = 10000;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n;
vector<pair<int, int>> adj[MAX];

void dijkstra() {
  priority_queue<pair<int, int>> PQ;
  int color[MAX];
  int d[MAX];

  for (int i = 0; i < n; i++) {
    d[i] = INT_MAX;
    color[i] = WHITE;
  }

  d[0] = 0;
  PQ.push(make_pair(0, 0));
  color[0] = GRAY;

  while (!PQ.empty()) {
    int u;
    pair<int, int> f = PQ.top();
    PQ.pop();
    u = f.second;

    color[u] = BLACK;

    for (int i = 0; i < adj[u].size(); i++) {
      int v = adj[u][i].first;
      if (color[v] == BLACK)
        continue;
      if (d[u] + adj[u][i].second < d[v]) {
        d[v] = d[u] + adj[u][i].second;
        PQ.push(make_pair(d[v] * (-1), v));
        color[v] = GRAY;
      }
    }
  }

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

int main() {
  int k, u, v, c;

  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      cin >> v >> c;
      adj[u].push_back(make_pair(v, c));
    }
  }

  dijkstra();

  return 0;
}
Problem p02243 (C++)

Buggy submission (status: Runtime Error):
//
// Created by ?????°?????? on 2016/05/19.
//

#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
const int MAX = 100;
const int INFTY = (1 << 21);
const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;

int n;
vector<pair<int, int>> adj[MAX];

void dijkstra() {
  priority_queue<pair<int, int>> PQ;
  int color[MAX];
  int d[MAX];
  for (int i = 0; i < n; ++i) {
    d[i] = INFTY;
    color[i] = WHITE;
  }

  d[0] = 0;
  PQ.push(make_pair(0, 0));
  color[0] = GRAY;

  while (!PQ.empty()) {
    pair<int, int> f = PQ.top();
    PQ.pop();
    int u = f.second;
    color[u] = BLACK;

    if (d[u] < f.first * -1)
      continue;

    for (int j = 0; j < adj[u].size(); ++j) {
      int v = adj[u][j].first;
      if (color[v] == BLACK)
        continue;
      if (d[v] > d[u] + adj[u][j].second) {
        d[v] = d[u] + adj[u][j].second;
        PQ.push(make_pair(d[v] * (-1), v));
        color[v] = GRAY;
      }
    }
  }

  for (int i = 0; i < n; ++i) {
    cout << i << " " << (d[i] == INFTY ? -1 : d[i]) << endl;
  }
}

int main() {
  cin >> n;
  int k, c, u, v;
  for (int i = 0; i < n; ++i) {
    cin >> u >> k;
    for (int j = 0; j < k; ++j) {
      cin >> v >> c;
      adj[u].push_back(make_pair(v, c));
    }
  }

  dijkstra();

  return 0;
}

Fixed submission:
//
// Created by ?????°?????? on 2016/05/19.
//

#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
const int MAX = 10000;
const int INFTY = (1 << 20);
const int WHITE = 0;
const int GRAY = 1;
const int BLACK = 2;

int n;
vector<pair<int, int>> adj[MAX];

void dijkstra() {
  priority_queue<pair<int, int>> PQ;
  int color[MAX];
  int d[MAX];
  for (int i = 0; i < n; ++i) {
    d[i] = INFTY;
    color[i] = WHITE;
  }

  d[0] = 0;
  PQ.push(make_pair(0, 0));
  color[0] = GRAY;

  while (!PQ.empty()) {
    pair<int, int> f = PQ.top();
    PQ.pop();
    int u = f.second;
    color[u] = BLACK;

    if (d[u] < f.first * -1)
      continue;

    for (int j = 0; j < adj[u].size(); ++j) {
      int v = adj[u][j].first;
      if (color[v] == BLACK)
        continue;
      if (d[v] > d[u] + adj[u][j].second) {
        d[v] = d[u] + adj[u][j].second;
        PQ.push(make_pair(d[v] * (-1), v));
        color[v] = GRAY;
      }
    }
  }

  for (int i = 0; i < n; ++i) {
    cout << i << " " << (d[i] == INFTY ? -1 : d[i]) << endl;
  }
}

int main() {
  cin >> n;
  int k, c, u, v;
  for (int i = 0; i < n; ++i) {
    cin >> u >> k;
    for (int j = 0; j < k; ++j) {
      cin >> v >> c;
      adj[u].push_back(make_pair(v, c));
    }
  }

  dijkstra();

  return 0;
}
Problem p02243 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <limits>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> Edge;
int main() {
  int n;
  cin >> n;
  vector<vector<Edge>> lists;

  for (int i = 0; i < n; i++) {
    vector<Edge> list;
    int u, k;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      Edge edge;
      cin >> edge.second >> edge.first;
      list.push_back(edge);
    }
    lists.push_back(list);
  }

  vector<int> costs(n, -1);
  costs[0] = 0;
  priority_queue<Edge> queue;
  queue.push(Edge(0, 0));

  while (!queue.empty()) {
    Edge edge = queue.top();
    queue.pop();
    int dest = edge.second;
    int cost = edge.first;
    if (costs[dest] < cost)
      continue;
    for (Edge next : lists[dest]) {
      next.first += cost;
      if (costs[next.second] > next.first || costs[next.second] == -1) {
        costs[next.second] = next.first;
        queue.push(next);
      }
    }
  }
  for (int i = 0; i < n; i++)
    cout << i << " " << costs[i] << endl;

  char c;
  cin >> c;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <limits>
#include <queue>
#include <utility>
#include <vector>
using namespace std;
typedef pair<int, int> Edge;
int main() {
  int n;
  cin >> n;
  vector<vector<Edge>> lists;

  for (int i = 0; i < n; i++) {
    vector<Edge> list;
    int u, k;
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      Edge edge;
      cin >> edge.second >> edge.first;
      list.push_back(edge);
    }
    lists.push_back(list);
  }

  vector<int> costs(n, -1);
  costs[0] = 0;
  priority_queue<Edge, vector<Edge>, greater<Edge>> queue;
  queue.push(Edge(0, 0));

  while (!queue.empty()) {
    Edge edge = queue.top();
    queue.pop();
    int dest = edge.second;
    int cost = edge.first;
    if (costs[dest] < cost)
      continue;
    for (Edge next : lists[dest]) {
      next.first += cost;
      if (costs[next.second] > next.first || costs[next.second] == -1) {
        costs[next.second] = next.first;
        queue.push(next);
      }
    }
  }
  for (int i = 0; i < n; i++)
    cout << i << " " << costs[i] << endl;

  char c;
  cin >> c;

  return 0;
}
Problem p02243 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

using namespace std;

vector<int> adja[10000], cost[10000];

#define INF (1 << 30)

int main() {
  int n, id, k;

  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> id >> k;
    adja[id].resize(k);
    cost[id].resize(k);
    for (int j = 0; j < k; ++j) {
      cin >> adja[id][j] >> cost[id][j];
    }
  }

  priority_queue<pair<int, int>> que;
  que.push(make_pair(0, 0)); // cost, node
  int best[10000];
  fill(best, best + n, INF);
  best[0] = 0;

  while (!que.empty()) {
    pair<int, int> top = que.top();
    que.pop();

    if (best[top.second] < top.first) {
      continue;
    }

    for (int j = 0, len = adja[top.second].size(); j < len; ++j) {
      if (best[adja[top.second][j]] > top.first + cost[top.second][j]) {
        best[adja[top.second][j]] = top.first + cost[top.second][j];
        que.push(
            make_pair(top.first + cost[top.second][j], adja[top.second][j]));
      }
    }
  }

  for (int i = 0; i < n; ++i) {
    cout << i << " " << best[i] << endl;
  }

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

vector<int> adja[10000], cost[10000];

#define INF (1 << 30)

int main() {
  int n, id, k;

  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> id >> k;
    adja[id].resize(k);
    cost[id].resize(k);
    for (int j = 0; j < k; ++j) {
      cin >> adja[id][j] >> cost[id][j];
    }
  }

  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      que;
  que.push(make_pair(0, 0)); // cost, node
  int best[10000];
  fill(best, best + n, INF);
  best[0] = 0;

  while (!que.empty()) {
    pair<int, int> top = que.top();
    que.pop();

    if (best[top.second] < top.first) {
      continue;
    }

    for (int j = 0, len = adja[top.second].size(); j < len; ++j) {
      if (best[adja[top.second][j]] > top.first + cost[top.second][j]) {
        best[adja[top.second][j]] = top.first + cost[top.second][j];
        que.push(
            make_pair(top.first + cost[top.second][j], adja[top.second][j]));
      }
    }
  }

  for (int i = 0; i < n; ++i) {
    cout << i << " " << best[i] << endl;
  }

  return 0;
}
Problem p02243 (C++)

Buggy submission (status: Time Limit Exceeded):
// -*- mode:c++; coding:utf-8; c-basic-offset:2; -*-
// ALDS1_12_C: Single Source Shortest Path II

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

using uint = unsigned int;

class Graph {
  using vc_t = std::pair<uint, uint>;
  std::vector<std::vector<vc_t>> g_;

public:
  Graph(const uint n) { g_.resize(n); }

  void push_to(const uint u, const uint v, const uint c) {
    g_[u].push_back(std::make_pair(v, c));
  }

  void dijkstra(uint *dist) {
    auto n = g_.size();
    const uint INFINIT = 100000 * (n - 1) + 1;
    std::fill_n(dist, n, INFINIT);
    auto cmp = [](vc_t left, vc_t right) { return left.second < right.second; };
    std::priority_queue<vc_t, std::vector<vc_t>, decltype(cmp)> q(cmp);

    q.push(vc_t(0, 0));
    dist[0] = 0u;
    while (!q.empty()) {
      auto bvc = q.top();
      q.pop();
      auto v = bvc.first;
      auto c = bvc.second;
      if (dist[v] < c) {
        continue;
      }
      dist[v] = c;
      for (auto vc : g_[v]) {
        q.push(std::make_pair(vc.first, vc.second + c));
      }
    }
  }
};

int main() {
  uint n;
  std::scanf("%u", &n);

  Graph g(n);

  uint u, k, v, c;
  for (auto i = 0u; i < n; ++i) {
    std::scanf("%u %u", &u, &k);
    for (auto j = 0u; j < k; ++j) {
      std::scanf("%u %u", &v, &c);
      g.push_to(u, v, c);
    }
  }

  uint dist[n];
  g.dijkstra(dist);
  for (auto i = 0u; i < n; ++i) {
    std::printf("%u %u\n", i, dist[i]);
  }
}

// eof

Fixed submission:
// -*- mode:c++; coding:utf-8; c-basic-offset:2; -*-
// ALDS1_12_C: Single Source Shortest Path II

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

using uint = unsigned int;

class Graph {
  using vc_t = std::pair<uint, uint>;
  std::vector<std::vector<vc_t>> g_;

public:
  Graph(const uint n) { g_.resize(n); }

  void push_to(const uint u, const uint v, const uint c) {
    g_[u].push_back(std::make_pair(v, c));
  }

  void dijkstra(uint *dist) {
    auto n = g_.size();
    const uint INFINIT = 100000 * (n - 1) + 1;
    std::fill_n(dist, n, INFINIT);
    auto cmp = [](vc_t left, vc_t right) { return left.second > right.second; };
    std::priority_queue<vc_t, std::vector<vc_t>, decltype(cmp)> q(cmp);

    q.push(vc_t(0, 0));
    dist[0] = 0u;
    while (!q.empty()) {
      auto bvc = q.top();
      q.pop();
      auto v = bvc.first;
      auto c = bvc.second;
      if (dist[v] < c) {
        continue;
      }
      dist[v] = c;
      for (auto vc : g_[v]) {
        q.push(std::make_pair(vc.first, vc.second + c));
      }
    }
  }
};

int main() {
  uint n;
  std::scanf("%u", &n);

  Graph g(n);

  uint u, k, v, c;
  for (auto i = 0u; i < n; ++i) {
    std::scanf("%u %u", &u, &k);
    for (auto j = 0u; j < k; ++j) {
      std::scanf("%u %u", &v, &c);
      g.push_to(u, v, c);
    }
  }

  uint dist[n];
  g.dijkstra(dist);
  for (auto i = 0u; i < n; ++i) {
    std::printf("%u %u\n", i, dist[i]);
  }
}

// eof
Problem p02243 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
static const int MAX = 100;
static const int INFTY = 2000000000;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;
int n;
vector<pair<int, int>> adj[MAX];
void dijkstra() {
  priority_queue<pair<int, int>> PQ;
  int d[MAX], color[MAX];
  for (int i = 0; i < n; i++) {
    d[i] = INFTY;
    color[i] = WHITE;
  }
  d[0] = 0;
  PQ.push(make_pair(0, 0));
  color[0] = GRAY;
  while (!PQ.empty()) {
    pair<int, int> f = PQ.top();
    PQ.pop();
    int u = f.second;
    color[u] = BLACK;
    if (d[u] < f.first * (-1))
      continue;
    for (int j = 0; j < adj[u].size(); j++) {
      int v = adj[u][j].first;
      if (color[v] == BLACK)
        continue;
      if (d[v] > d[u] + adj[u][j].second) {
        d[v] = d[u] + adj[u][j].second;
        PQ.push(make_pair(d[v] * (-1), v));
        color[v] = GRAY;
      }
    }
  }
  for (int i = 0; i < n; i++)
    cout << i << " " << (d[i] == INFTY ? -1 : d[i]) << "\n";
}
int main() {
  int k, u, v, c;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      cin >> v >> c;
      adj[u].push_back(make_pair(v, c));
    }
  }
  dijkstra();
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <queue>
using namespace std;
static const int MAX = 100000;
static const int INFTY = 2000000000;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;
int n;
vector<pair<int, int>> adj[MAX];
void dijkstra() {
  priority_queue<pair<int, int>> PQ;
  int d[MAX], color[MAX];
  for (int i = 0; i < n; i++) {
    d[i] = INFTY;
    color[i] = WHITE;
  }
  d[0] = 0;
  PQ.push(make_pair(0, 0));
  color[0] = GRAY;
  while (!PQ.empty()) {
    pair<int, int> f = PQ.top();
    PQ.pop();
    int u = f.second;
    color[u] = BLACK;
    if (d[u] < f.first * (-1))
      continue;
    for (int j = 0; j < adj[u].size(); j++) {
      int v = adj[u][j].first;
      if (color[v] == BLACK)
        continue;
      if (d[v] > d[u] + adj[u][j].second) {
        d[v] = d[u] + adj[u][j].second;
        PQ.push(make_pair(d[v] * (-1), v));
        color[v] = GRAY;
      }
    }
  }
  for (int i = 0; i < n; i++)
    cout << i << " " << (d[i] == INFTY ? -1 : d[i]) << "\n";
}
int main() {
  int k, u, v, c;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> u >> k;
    for (int j = 0; j < k; j++) {
      cin >> v >> c;
      adj[u].push_back(make_pair(v, c));
    }
  }
  dijkstra();
  return 0;
}
Problem p02243 (C++)

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

#define NUM 100
#define NIL -1
#define INF 1000000000
#define WHITE 0
#define BLACK 1
int node_num;
vector<tuple<int, int>> G[NUM];
priority_queue<tuple<int, int>> PQ;
int V[NUM];
int color[NUM];

void init(void) {
  scanf("%d", &node_num);

  for (int i = 0; i < node_num; i++) {
    int u, k;
    scanf("%d", &u);
    scanf("%d", &k);

    for (int j = 0; j < k; j++) {
      int v_, c;
      scanf("%d", &v_);
      scanf("%d", &c);
      G[u].push_back(make_tuple(v_, c));
    }
  }

  for (int i = 0; i < node_num; i++) {
    color[i] = WHITE;
    V[i] = INF;
  }
}

void updateAround(int n) {
  color[n] = BLACK;
  for (int i = 0; i < G[n].size(); i++) {
    int old_dist = V[get<0>(G[n][i])];
    int new_dist = V[n] + get<1>(G[n][i]);
    if (new_dist < old_dist) {
      V[get<0>(G[n][i])] = new_dist;
      PQ.push(make_tuple(-new_dist, get<0>(G[n][i])));
    }
  }

  int min_index = NIL;
  int min_value = INF;
  while (!PQ.empty()) {
    tuple<int, int> f = PQ.top();
    PQ.pop();
    if (color[get<1>(f)] == BLACK) {
      continue;
    }
    min_value = -get<0>(f);
    min_index = get<1>(f);
    break;
  }

  if (min_index != NIL) {
    updateAround(min_index);
  }
}

int main(void) {
  init();

  V[0] = 0;
  updateAround(0);

  for (int i = 0; i < node_num; i++) {
    printf("%d %d\n", i, V[i]);
  }
}


Fixed submission:
#include <iostream>
#include <queue>
#include <stdio.h>
#include <tuple>
#include <vector>
using namespace std;

#define NUM 10000
#define NIL -1
#define INF 1000000000
#define WHITE 0
#define BLACK 1
int node_num;
vector<tuple<int, int>> G[NUM];
priority_queue<tuple<int, int>> PQ;
int V[NUM];
int color[NUM];

void init(void) {
  scanf("%d", &node_num);

  for (int i = 0; i < node_num; i++) {
    int u, k;
    scanf("%d", &u);
    scanf("%d", &k);

    for (int j = 0; j < k; j++) {
      int v_, c;
      scanf("%d", &v_);
      scanf("%d", &c);
      G[u].push_back(make_tuple(v_, c));
    }
  }

  for (int i = 0; i < node_num; i++) {
    color[i] = WHITE;
    V[i] = INF;
  }
}

void updateAround(int n) {
  color[n] = BLACK;
  for (int i = 0; i < G[n].size(); i++) {
    int old_dist = V[get<0>(G[n][i])];
    int new_dist = V[n] + get<1>(G[n][i]);
    if (new_dist < old_dist) {
      V[get<0>(G[n][i])] = new_dist;
      PQ.push(make_tuple(-new_dist, get<0>(G[n][i])));
    }
  }

  int min_index = NIL;
  int min_value = INF;
  while (!PQ.empty()) {
    tuple<int, int> f = PQ.top();
    PQ.pop();
    if (color[get<1>(f)] == BLACK) {
      continue;
    }
    min_value = -get<0>(f);
    min_index = get<1>(f);
    break;
  }

  if (min_index != NIL) {
    updateAround(min_index);
  }
}

int main(void) {
  init();

  V[0] = 0;
  updateAround(0);

  for (int i = 0; i < node_num; i++) {
    printf("%d %d\n", i, V[i]);
  }
}
Problem p02243 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define int(x)                                                                 \
  int x;                                                                       \
  scanf("%d", &x)
#define input(x) scanf("%d", &x)
#define rep(x, n) for (int x = 0; x < n; x++)
#define _br printf("\n")
#define INF 2000000000

vector<pair<int, int>> PathList[100];
int d[100];
bool complete[100];

void dijkstra(int n, vector<pair<int, int>> PathList[]) {
  rep(i, n) {
    d[i] = INF;
    complete[i] = false;
  }
  d[0] = 0;

  int count = 0;

  while (count != n) {
    int mind = INF;
    int mindi = 0;

    // select
    rep(i, n) {
      if (!complete[i] && d[i] < mind) {
        mind = d[i];
        mindi = i;
      }
    }

    complete[mindi] = true;
    count++;

    // update
    rep(i, PathList[mindi].size()) {
      if (PathList[mindi][i].second + d[mindi] < d[PathList[mindi][i].first]) {
        d[PathList[mindi][i].first] = PathList[mindi][i].second + d[mindi];
      }
    }
  }
}

int main() {
  int(n);
  rep(i, n) {
    int(k);
    int(num);
    rep(j, num) {
      int(s);
      int(t);
      PathList[i].push_back(make_pair(s, t));
    }
  }

  dijkstra(n, PathList);

  rep(i, n) { printf("%d %d\n", i, d[i]); }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
#define int(x)                                                                 \
  int x;                                                                       \
  scanf("%d", &x)
#define input(x) scanf("%d", &x)
#define rep(x, n) for (int x = 0; x < n; x++)
#define _br printf("\n")
#define INF 2000000000

vector<pair<int, int>> PathList[10000];
int d[10000];
bool complete[10000];

void dijkstra(int n, vector<pair<int, int>> PathList[]) {
  rep(i, n) {
    d[i] = INF;
    complete[i] = false;
  }
  d[0] = 0;

  int count = 0;

  while (count != n) {
    int mind = INF;
    int mindi = 0;

    // select
    rep(i, n) {
      if (!complete[i] && d[i] < mind) {
        mind = d[i];
        mindi = i;
      }
    }

    complete[mindi] = true;
    count++;

    // update
    rep(i, PathList[mindi].size()) {
      if (PathList[mindi][i].second + d[mindi] < d[PathList[mindi][i].first]) {
        d[PathList[mindi][i].first] = PathList[mindi][i].second + d[mindi];
      }
    }
  }
}

int main() {
  int(n);
  rep(i, n) {
    int(k);
    int(num);
    rep(j, num) {
      int(s);
      int(t);
      PathList[i].push_back(make_pair(s, t));
    }
  }

  dijkstra(n, PathList);

  rep(i, n) { printf("%d %d\n", i, d[i]); }

  return 0;
}
Problem p02243 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define INT_MAX 2147483647
#define INT_MIN -2147483646
#define Loop(i, n) for (int i = 0; i < (int)n; i++)
#define Loop1(i, n) for (int i = 1; i <= (int)n; i++)
#define Loopr(i, n) for (int i = (int)n - 1; i >= 0; i--)
#define Loopr1(i, n) for (int i = (int)n; i >= 1; i--)
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;

static const int N = 105;

struct vertex {
  int id;
  int p;
  int d;
  bool operator<(const vertex &another) const {
    return (d != another.d ? d > another.d : id > another.id);
  }
};

struct edge {
  int end;
  int w;
};

int main() {
  int n;
  cin >> n;
  vector<vector<edge>> E(N);
  Loop(i, n) {
    int u, k;
    cin >> u >> k;
    Loop(i, k) {
      int v, c;
      cin >> v >> c;
      E[u].push_back({v, c});
    }
  }
  //
  priority_queue<vertex> pq;
  vi color(N, 0), dist(N, 0);
  pq.push({0, -1, 0});
  while (pq.size() > 0) {
    vertex v = pq.top();
    pq.pop();
    if (color[v.id] == 1)
      continue;
    dist[v.id] = v.d;
    color[v.id] = 1;
    Loop(j, E[v.id].size()) {
      pq.push({E[v.id][j].end, v.id, dist[v.id] + E[v.id][j].w});
    }
  }
  Loop(i, n) { cout << i << " " << dist[i] << endl; }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <stack>
#include <string>
#include <tuple>
#include <vector>
#define INT_MAX 2147483647
#define INT_MIN -2147483646
#define Loop(i, n) for (int i = 0; i < (int)n; i++)
#define Loop1(i, n) for (int i = 1; i <= (int)n; i++)
#define Loopr(i, n) for (int i = (int)n - 1; i >= 0; i--)
#define Loopr1(i, n) for (int i = (int)n; i >= 1; i--)
using namespace std;
typedef long long int ll;
typedef vector<int> vi;
typedef vector<vector<int>> vvi;

static const int N = 10005;

struct vertex {
  int id;
  int p;
  int d;
  bool operator<(const vertex &another) const {
    return (d != another.d ? d > another.d : id > another.id);
  }
};

struct edge {
  int end;
  int w;
};

int main() {
  int n;
  cin >> n;
  vector<vector<edge>> E(N);
  Loop(i, n) {
    int u, k;
    cin >> u >> k;
    Loop(i, k) {
      int v, c;
      cin >> v >> c;
      E[u].push_back({v, c});
    }
  }
  //
  priority_queue<vertex> pq;
  vi color(N, 0), dist(N, 0);
  pq.push({0, -1, 0});
  while (pq.size() > 0) {
    vertex v = pq.top();
    pq.pop();
    if (color[v.id] == 1)
      continue;
    dist[v.id] = v.d;
    color[v.id] = 1;
    Loop(j, E[v.id].size()) {
      pq.push({E[v.id][j].end, v.id, dist[v.id] + E[v.id][j].w});
    }
  }
  Loop(i, n) { cout << i << " " << dist[i] << endl; }
  return 0;
}
Problem p02244 (C++)

Buggy submission (status: Runtime Error):
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <string.h>

namespace {

const int N = 8;
// the positions of k-queens are fixed.
// Once initialized, the values remain unchanged.
bool row[N] = {false};

bool col[N] = {false};
bool down[2 * N - 1] = {false};
bool up[2 * N - 1] = {false};
int pos[N];

void Print() {
  for (int r = 0; r < N; ++r) {
    const int qc = pos[r];
    for (int c = 0; c < N; ++c) {
      if (c == qc)
        printf("Q");
      else
        printf(".");
    }
    printf("\n");
  }
}

// OK returns true if we can put a queen at (r, c)
inline bool OK(int r, int c) {
  return !col[c] && !down[r - c + N - 1] && !up[r + c];
}

inline void Update(int r, int c) {
  pos[r] = c;
  col[c] = true;
  down[r - c + N - 1] = true;
  up[r + c] = true;
}

inline void Cancel(int r, int c) {
  pos[r] = -1;
  col[c] = false;
  down[r - c + N - 1] = false;
  up[r + c] = false;
}

bool Put(int r) {
  if (row[r])
    return Put(r + 1);

  for (int c = 0; c < 8; ++c) {
    if (!OK(r, c))
      continue;

    Update(r, c);

    if (r == 7)
      return true;

    if (Put(r + 1))
      return true;
    else
      Cancel(r, c);
  }
  return false;
}

} // namespace

int main() {
  int k;
  std::cin >> k;

  memset(pos, -1, sizeof(int) * N);
  for (int i = 0; i < k; ++i) {
    int r, c;
    std::cin >> r >> c;
    Update(r, c);
    row[r] = true;
  }

  if (!Put(0)) {
    std::cerr << "ERROR: not found" << std::endl;
    Print();
    std::exit(1);
  }

  Print();

  return 0;
}

Fixed submission:
#include <cstdlib>
#include <iostream>
#include <stdio.h>
#include <string.h>

namespace {

const int N = 8;
// the positions of k-queens are fixed.
// Once initialized, the values remain unchanged.
bool row[N] = {false};

bool col[N] = {false};
bool down[2 * N - 1] = {false};
bool up[2 * N - 1] = {false};
int pos[N];

void Print() {
  for (int r = 0; r < N; ++r) {
    const int qc = pos[r];
    for (int c = 0; c < N; ++c) {
      if (c == qc)
        printf("Q");
      else
        printf(".");
    }
    printf("\n");
  }
}

// OK returns true if we can put a queen at (r, c)
inline bool OK(int r, int c) {
  return !col[c] && !down[r - c + N - 1] && !up[r + c];
}

inline void Update(int r, int c) {
  pos[r] = c;
  col[c] = true;
  down[r - c + N - 1] = true;
  up[r + c] = true;
}

inline void Cancel(int r, int c) {
  pos[r] = -1;
  col[c] = false;
  down[r - c + N - 1] = false;
  up[r + c] = false;
}

bool Put(int r) {
  if (row[r])
    return r < 7 ? Put(r + 1) : true;

  for (int c = 0; c < 8; ++c) {
    if (!OK(r, c))
      continue;

    Update(r, c);

    if (r == 7)
      return true;

    if (Put(r + 1))
      return true;
    else
      Cancel(r, c);
  }
  return false;
}

} // namespace

int main() {
  int k;
  std::cin >> k;

  memset(pos, -1, sizeof(int) * N);
  for (int i = 0; i < k; ++i) {
    int r, c;
    std::cin >> r >> c;
    Update(r, c);
    row[r] = true;
  }

  if (!Put(0)) {
    std::cerr << "ERROR: not found" << std::endl;
    Print();
    std::exit(1);
  }

  Print();

  return 0;
}
Problem p02245 (C++)

Buggy submission (status: Runtime Error):
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
queue<pair<string, int>> Q;

void Sw(string st, int x) {
  int i;
  int pos, npos;
  int d[4] = {-3, 3, -1, 1};
  string st1;

  for (i = 0; i <= 8; i++) {
    if (st[i] == '0') {
      pos = i;
      break;
    }
  }
  for (i = 0; i <= 3; i++) {
    st1 = st;
    if (i == 2 && pos % 3 == 0)
      continue;
    if (i == 3 && pos % 3 == 0)
      continue;
    npos = pos + d[i];
    if (npos >= 9 || npos <= -1)
      continue;
    swap(st1[pos], st1[npos]);
    Q.push(pair<string, int>(st1, x + 1));
  }
}

int main() {
  string puzzle = "";
  string goal = "123456780";
  map<string, bool> M;
  int i, x;
  string P;

  for (i = 0; i <= 8; i++) {
    cin >> x;
    puzzle += '0' + x;
  }
  Q.push(pair<string, int>(puzzle, 0));
  while (1) {
    pair<string, int> p = Q.front();
    Q.pop();
    P = p.first;
    x = p.second;
    if (P == goal) {
      cout << x << endl;
      break;
    }
    if (M[P])
      continue;
    else
      M[P] = 1;
    Sw(P, x);
  }
  return 0;
}

Fixed submission:
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
using namespace std;
queue<pair<string, int>> Q;

void Sw(string st, int x) {
  int i;
  int pos, npos;
  int d[4] = {-3, 3, -1, 1};
  string st1;

  for (i = 0; i <= 8; i++) {
    if (st[i] == '0') {
      pos = i;
      break;
    }
  }
  for (i = 0; i <= 3; i++) {
    st1 = st;
    if (i == 2 && pos % 3 == 0)
      continue;
    if (i == 3 && pos % 3 == 2)
      continue;
    npos = pos + d[i];
    if (npos >= 9 || npos <= -1)
      continue;
    swap(st1[pos], st1[npos]);
    Q.push(pair<string, int>(st1, x + 1));
  }
}

int main() {
  string puzzle = "";
  string goal = "123456780";
  map<string, bool> M;
  int i, x;
  string P;

  for (i = 0; i <= 8; i++) {
    cin >> x;
    puzzle += '0' + x;
  }
  Q.push(pair<string, int>(puzzle, 0));
  while (1) {
    pair<string, int> p = Q.front();
    Q.pop();
    P = p.first;
    x = p.second;
    if (P == goal) {
      cout << x << endl;
      break;
    }
    if (M[P])
      continue;
    else
      M[P] = 1;
    Sw(P, x);
  }
  return 0;
}
Problem p02247 (C++)

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

int main() {
  string T, P;

  cin >> T;
  cin >> P;

  for (int i = 0; i <= T.length() - P.length(); i++)
    if (T.substr(i, P.length()) == P)
      cout << i << endl;

  return 0;
}

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

int main() {
  string T, P;

  cin >> T;
  cin >> P;

  if (T.length() >= P.length())
    for (int i = 0; i <= T.length() - P.length(); i++)
      if (T.substr(i, P.length()) == P)
        cout << i << endl;

  return 0;
}
Problem p02247 (C++)

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

  string a, b;
  int tmp;
  cin >> a >> b;
  for (int i = 0; i < a.size() - b.size() + 1; i++) {
    if (a[i] == b[0]) {
      tmp = 1;
      for (int j = 0; j < b.size(); j++)
        if (a[i + j] != b[j]) {
          tmp = 0;
          break;
        }
      if (tmp)
        cout << i << endl;
    }
  }

  return 0;
}

Fixed submission:
#include <iostream>
#include <string>
using namespace std;
int main() {

  string a, b;
  int tmp;
  cin >> a >> b;
  if (a.size() < b.size())
    return 0;
  for (int i = 0; i < a.size() - b.size() + 1; i++) {
    if (a[i] == b[0]) {
      tmp = 1;
      for (int j = 0; j < b.size(); j++)
        if (a[i + j] != b[j]) {
          tmp = 0;
          break;
        }
      if (tmp)
        cout << i << endl;
    }
  }

  return 0;
}
Problem p02247 (C++)

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

int main() {
  std::ios_base::sync_with_stdio(false);

  std::string a;
  std::string b;

  std::cin >> a;
  std::cin >> b;

  bool match = false;

  for (int i = 0; i <= (a.size() - b.size()); i++) {
    match = true;

    for (int j = 0; j < b.size(); j++) {
      if (a[i + j] != b[j]) {
        match = false;

        break;
      }
    }

    if (match) {
      std::cout << i << std::endl;
    }
  }

  return 0;
}

Fixed submission:
#include <iostream>

int main() {
  std::ios_base::sync_with_stdio(false);

  std::string a;
  std::string b;

  std::cin >> a;
  std::cin >> b;

  bool match = false;

  if (a.size() < b.size()) {
    return 0;
  }

  for (int i = 0; i <= (a.size() - b.size()); i++) {
    match = true;

    for (int j = 0; j < b.size(); j++) {
      if (a[i + j] != b[j]) {
        match = false;

        break;
      }
    }

    if (match) {
      std::cout << i << std::endl;
    }
  }

  return 0;
}
Problem p02247 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, char *argv[]) {
  int count = 0;
  vector<int> index;
  string a, b;
  cin >> a >> b;
  for (int i = 0; i <= a.size() - b.size(); i++) {
    if (a.substr(i, b.size()) == b) {
      count++;
      index.push_back(i);
    }
  }
  if (count != 0) {
    for (int i : index)
      cout << i << endl;
  }
}

Fixed submission:
#include <iostream>
#include <vector>
using namespace std;
int main(int argc, char *argv[]) {
  int count = 0;
  vector<int> index;
  string a, b;
  cin >> a >> b;
  if (a.size() < b.size())
    return 0;
  for (int i = 0; i <= a.size() - b.size(); i++) {
    if (a.substr(i, b.size()) == b) {
      count++;
      index.push_back(i);
    }
  }
  if (count != 0) {
    for (int i : index)
      cout << i << endl;
  }
}
Problem p02247 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  string t, p;
  cin >> t >> p;
  for (int i = 0; i < t.size() - p.size() + 1; i++) {
    int f = 0;
    for (int j = 0; j < p.size(); j++)
      if (t[i + j] != p[j])
        f = 1;
    if (f)
      continue;
    cout << i << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  string t, p;
  cin >> t >> p;
  if (t.size() >= p.size())
    for (int i = 0; i < t.size() - p.size() + 1; i++) {
      int f = 0;
      for (int j = 0; j < p.size(); j++)
        if (t[i + j] != p[j])
          f = 1;
      if (f)
        continue;
      cout << i << endl;
    }
  return 0;
}