alexjercan/bugnet huggingface dataset:
Problem p02241 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>
#include <vector>
#define INFTY 10000
#define PII pair<int, int>
#define MPII(a, b) make_pair(a, b)
using namespace std;
class NODE {
public:
  vector<PII> v;
  int index = 0;
  bool Isdone = false;
};
class Tree {
public:
  vector<int> V;
  int cost = 0;
};

int main() {
  NODE *nodes;
  nodes = new NODE[101];
  Tree T;
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      int t;
      cin >> t;
      if (t != -1) {
        PII p = MPII(t, j);
        nodes[i].v.push_back(p);
      }
    }
    sort(nodes[i].v.begin(), nodes[i].v.end());
  }
  T.V.push_back(1);
  nodes[1].Isdone = true;
  while (1) {
    if (T.V.size() == n)
      break;
    int mincost = INFTY;
    int addv = -1;
    int index;
    for (auto itr : T.V) {
      if (nodes[itr].v.size() <= nodes[itr].index)
        continue;
      while (1) {
        if (nodes[nodes[itr].v[nodes[itr].index].second].Isdone == false)
          break;
        nodes[itr].index++;
        if (nodes[itr].v.size() <= nodes[itr].index)
          break;
      }
      if (nodes[itr].v.size() <= nodes[itr].index)
        continue;
      if (mincost >= nodes[itr].v[nodes[itr].index].first) {
        mincost = nodes[itr].v[nodes[itr].index].first;
        addv = nodes[itr].v[nodes[itr].index].second;
        index = itr;
      }
    }

    nodes[index].index++;
    T.V.push_back(addv);
    nodes[addv].Isdone = true;
    T.cost += mincost;
  }
  cout << T.cost << endl;
  delete[] nodes;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define PII pair<int, int>
#define MPII(a, b) make_pair(a, b)
using namespace std;
class NODE {
public:
  vector<PII> v;
  int index = 0;
};
class Tree {
public:
  vector<int> V;
  int cost = 0;
};

int main() {
  NODE *nodes;
  nodes = new NODE[101];
  Tree T;
  int n;
  cin >> n;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      int t;
      cin >> t;
      if (t != -1) {
        PII p = MPII(t, j);
        nodes[i].v.push_back(p);
      }
    }
    sort(nodes[i].v.begin(), nodes[i].v.end());
  }
  T.V.push_back(1);
  while (1) {
    if (T.V.size() == n + 1)
      break;
    int mincost = 10000;
    int addv = -1;
    int index;
    for (auto itr : T.V) {
      if (mincost >= nodes[itr].v[nodes[itr].index].first) {
        mincost = nodes[itr].v[nodes[itr].index].first;
        addv = nodes[itr].v[nodes[itr].index].second;
        index = itr;
      }
    }
    nodes[index].index++;
    T.V.push_back(addv);
    T.cost += mincost;
  }
  cout << T.cost << endl;
  delete[] nodes;
  return 0;
}
Problem p02241 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
#define MAX 1 << 22
int map[1000][1000];
int n, minval = 999999;

int prim() {
  int sum = 0;
  int d[1000], fathernode[1000], color[1000];
  ;
  for (int i = 0; i < n; i++)
    d[i] = fathernode[i] = MAX;
  d[0] = 0;
  while (true) {
    int ch = -1, min = MAX;
    for (int i = 0; i < n; i++) {
      if (min > d[i] && color[i] == 0) // 选取未被染色的点
      {
        ch = i;
        min = d[i];
      }
    }
    if (ch == -1) // 选完了，跳出
      break;
    color[ch] = 1;              // 选取这个点后染色
    for (int j = 0; j < n; j++) // 选取V-T中与T距离最近的节点
    {
      if (d[j] > map[ch][j] && color[j] == 0) {
        d[j] = map[ch][j];
        fathernode[j] = ch; // j节点的父节点是ch
      }
    }
  }
  // 选取完毕，开始加起来
  for (int k = 0; k < n; k++) // 只加被染色（即被选中的）节点
  {
    if (fathernode[k] != MAX) // 只加上有父节点的点
      sum += map[k][fathernode[k]];
  }
  return sum;
}

int main(void) {
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      int t;
      scanf("%d", &t);
      map[i][j] = (t == -1 ? MAX : t);
    }
  printf("%d\\n", prim());
  return 0;
}


Error: WA

Fixed submission:

#include <stdio.h>
#define INF 999999

int n, k;
int color[3000], d[3000], fathernode[30];
int map[3000][3000];

int dijkstra(int s) {
  for (int i = 0; i < n; i++) {
    d[i] = INF;
    fathernode[i] = -1;
  }
  d[s] = 0;
  while (1) {
    int min = INF;
    int u = -1;
    for (int i = 0; i < n; i++) {
      if (min > d[i] && color[i] != 1) {
        u = i;
        min = d[i];
      }
    }
    if (u == -1)
      break;
    color[u] = 1; // 染色，以防后面重复使用
    for (int i = 0; i < n; i++) {
      if (color[i] != 1 && map[u][i] != INF)
        if (d[i] > d[u] + map[u][i])
          d[i] = d[u] + map[u][i];
    }
  }
  return 0;
}

int main(void) {
  int x, y, v;
  scanf("%d", &n);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      map[i][j] = INF;
  for (int i = 0; i < n; i++) {
    scanf("%d%d", &x, &k);
    for (int j = 0; j < k; j++) {
      scanf("%d%d", &y, &v);
      map[x][y] = v;
    }
  }
  dijkstra(0);

  for (int i = 0; i < n; i++) {
    if (d[i] != INF)
      printf("%d %d", i, d[i]);
    printf("\\n");
  }
  return 0;
}
Problem p02241 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <climits>
#include <iostream>
#include <queue>

using namespace std;
static const int MAX = 100;
static const int WHITE = 1;
static const int GRAY = 2;
static const int BLACK = 3;

int n, M[MAX + 10][MAX + 10];

int prim() {
  int u, minv;
  int d[MAX], p[MAX], color[MAX];

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

  d[0] = 0;

  while (1) {
    minv = INT_MAX;
    u = -1;
    for (int i = 0; i < n; i++) {
      if (minv > d[i] && color[i] != BLACK) {
        u = i;
        minv = d[i];
      }
    }
    if (u == -1)
      break;
    color[u] = BLACK;
    for (int v = 0; v < n; v++) {
      if (color[v] != BLACK && M[u][v] != INT_MAX) {
        if (d[v] > M[u][v]) {
          d[v] = M[u][v];
          p[v] = u;
          color[v] = GRAY;
        }
      }
    }
  }

  int sum = 0;
  for (int i = 0; i < n; i++) {
    if (p[i] != -1)
      sum += M[i][p[i]];
  }

  return sum;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      int e;
      cin >> e;
      M[i][j] = (e == -1) ? INT_MAX : e;
    }

  cout << prim() << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <climits>
#include <iostream>
#include <queue>

using namespace std;
static const int MAX = 100;
static const int WHITE = 1;
static const int GRAY = 2;
static const int BLACK = 3;

int n, M[MAX + 10][MAX + 10];

int prim() {
  int u, minv;
  int d[MAX], p[MAX], color[MAX];

  for (int i = 0; i < n; i++) {
    d[i] = INT_MAX;
    p[i] = -1;
    color[i] = BLACK;
  }

  d[0] = 0;

  while (1) {
    minv = INT_MAX;
    u = -1;
    for (int i = 0; i < n; i++) {
      if (minv > d[i] && color[i] != BLACK) {
        u = i;
        minv = d[i];
      }
    }
    if (u == -1)
      break;
    color[u] = BLACK;
    for (int v = 0; v < n; v++) {
      if (color[v] != BLACK && M[u][v] != INT_MAX) {
        if (d[v] > M[u][v]) {
          d[v] = M[u][v];
          p[v] = u;
          color[v] = GRAY;
        }
      }
    }
  }

  int sum = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; i < n; j++) {
      if (p[i] != -1)
        sum += M[i][p[i]];
    }

  return sum;
}

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      int e;
      cin >> e;
      M[i][j] = (e == -1) ? INT_MAX : e;
    }

  cout << prim() << endl;
  return 0;
}
Problem p02241 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
using namespace std;

static const int MAX = 100;
static const int INFTY = (1 << 21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, M[MAX][MAX];

int prim() {
  int u, minv;
  int d[MAX], p[MAX], color[MAX];

  for (int i = 0; i < n; i++) {
    d[i] = INFTY;
    p[i] = -1;
    color[i] = WHITE;
  }

  d[0] = 0;

  while (1) {
    minv = INFTY;
    u = -1;
    for (int i = 0; i < n; i++) {
      if (minv > d[i] && color[i] != BLACK) {
        u = i;
        minv = d[i];
      }
    }
    if (u == -1)
      break;
    color[u] = BLACK;
    for (int v = 0; v < n; v++) {
      if (color[v] != BLACK && M[u][v] != INFTY) {
        if (d[v] > M[u][v]) {
          d[v] = M[u][v];
          p[v] = u;
          color[v] = GRAY;
        }
      }
    }
  }

  int sum = 0;
  for (int i = 0; i < n; i++) {
    if (p[i] != -1)
      sum += M[i][p[i]];
  }
  return sum;
}

int main() {
  cin >> n;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int e;
      cin >> e;
      M[i][j] = (e == -1) ? INFTY : e;
    }
  }

  cout << prim() << endl;

  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
using namespace std;

static const int MAX = 100;
static const int INFTY = (1 << 21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, M[MAX][MAX];

int prim() {
  int u, minv;
  int d[MAX], p[MAX], color[MAX];

  for (int i = 0; i < n; i++) {
    d[i] = INFTY;
    p[i] = -1;
    color[i] = WHITE;
  }

  d[0] = 0;

  while (1) {
    minv = INFTY;
    u = -1;
    for (int i = 0; i < n; i++) {
      if (minv > d[i] && color[i] != BLACK) {
        u = i;
        minv = d[i];
      }
    }
    if (u == -1)
      break;
    color[u] = BLACK;
    for (int v = 0; v < n; v++) {
      if (color[v] != BLACK && M[u][v] != INFTY) {
        if (d[v] > M[u][v]) {
          d[v] = M[u][v];
          p[v] = u;
          color[v] = GRAY;
        }
      }
    }
  }

  int sum = 0;
  for (int i = 0; i < n; i++) {
    if (p[i] != -1)
      sum += M[i][p[i]];
  }
  return sum;
}

int main() {
  cin >> n;

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; i++) {
      int e;
      cin >> e;
      M[i][j] = (e == -1) ? INFTY : e;
    }
  }

  cout << prim() << endl;

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

#include <iostream>
using namespace std;
#define INFTY (1 << 30)

enum Color {
  WHITE,
  GRAY,
  BLACK,
};

void Prim(int verValWeight[][100], int verNum) {
  int distance[100];
  Color color[100];
  int parent[100];
  for (int i = 0; i < verNum; i++) {
    color[i] = WHITE;
    distance[i] = INFTY;
    parent[i] = -1;
  }
  distance[0] = 0;

  while (true) {
    int min_cost = INFTY;
    int subScript;
    for (int i = 0; i < verNum; i++) {
      if (color[i] != BLACK && distance[i] < min_cost) {
        min_cost = distance[i];
        subScript = i;
      }
    }

    if (min_cost == INFTY)
      break; // end

    color[subScript] = BLACK;

    for (int i = 0; i < verNum; i++) {
      if (color[i] != BLACK && verValWeight[subScript][i] != INFTY) {
        if (verValWeight[subScript][i] < distance[i]) {
          distance[i] = verValWeight[subScript][i];
          parent[i] = subScript;
          color[i] = GRAY;
        }
      }
    }
  }

  int weight_sum = 0;
  for (int i = 0; i < verNum; i++)
    if (parent[i] != -1)
      weight_sum += verValWeight[i][parent[i]];
  cout << weight_sum << endl;
}

int main() {
  int verNum;
  int verValWeight[100][100];
  int parent[100];

  cin >> verNum;
  for (int i = 0; i < verNum; i++)
    for (int j = 0; j < verNum; j++) {
      cin >> verValWeight[i][j];
      if (verValWeight[i][j] == -1)
        verValWeight[i][j] = INFTY;
    }

  Prim(verValWeight, verNum);

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
#define INFTY (1 << 30)

enum Color {
  WHITE,
  GRAY,
  BLACK,
};

void Prim(int verValWeight[][100], int verNum) {
  int distance[100];
  Color color[100];
  int parent[100];
  for (int i = 0; i < verNum; i++) {
    color[i] = WHITE;
    distance[i] = INFTY;
    parent[i] = -1;
  }
  distance[0] = 0;

  while (true) {
    int min_cost = INFTY;
    int subScript;
    for (int i = 0; i < verNum; i++) {
      if (color[i] != BLACK && distance[i] < min_cost) {
        min_cost = distance[i];
        subScript = i;
      }

      if (min_cost == INFTY)
        break; // end

      color[subScript] = BLACK;

      for (int i = 0; i < verNum; i++) {
        if (color[i] != BLACK && verValWeight[subScript][i] != INFTY) {
          if (verValWeight[subScript][i] < distance[i]) {
            distance[i] = verValWeight[subScript][i];
            parent[i] = subScript;
            color[i] = GRAY;
          }
        }
      }
    }
  }

  int weight_sum = 0;
  for (int i = 0; i < verNum; i++)
    if (parent[i] != -1)
      weight_sum += verValWeight[i][parent[i]];
  cout << weight_sum << endl;
}

int main() {
  int verNum;
  int verValWeight[100][100];
  int parent[100];

  cin >> verNum;
  for (int i = 0; i < verNum; i++)
    for (int j = 0; j < verNum; j++) {
      cin >> verValWeight[i][j];
      if (verValWeight[i][j] == -1)
        verValWeight[i][j] = INFTY;
    }

  Prim(verValWeight, verNum);

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

//?????????????????????????¨???????pair???????????¨???????????????????????£????????§31-179573????????????????????????????????????????????????????????????init(n)???????????????????????¨????°????????????????30?????????????????????????????£??????
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

int P[10010];
void init(int N) {
  for (int i = 0; i <= N; i++)
    P[i] = i;
}

int root(int a) {
  if (P[a] == a)
    return a;
  return (P[a] = root(P[a]));
}

bool is_same_set(int a, int b) { return root(a) == root(b); }
void unite(int a, int b) { P[root(a)] = root(b); }

int main() {
  int n;
  cin >> n;
  init(n);
  int A[101][101];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> A[i][j];
    }
  }
  int cost[10010], src[10010], dst[10010];
  int M = 0;
  for (int r = 0; r < n; r++) {
    for (int c = r + 1; c < n; c++) {
      if (A[r][c] > -1) {
        cost[M] = A[r][c];
        src[M] = r;
        dst[M] = c;
        M += 1;
      }
    }
  }
  pair<int, int> edges[10010];
  for (int i = 0; i < M; i++) {
    edges[i] = make_pair(cost[i], i);
  }

  sort(edges, edges + M);
  int s = 0;
  for (int i = 0; i < M; i++) {
    int k = edges[i].second;
    int p = src[k];
    int q = dst[k];
    if (is_same_set(p, q)) {
      continue;
    }
    unite(p, q);
    s += edges[i].first;
  }
  cout << s << endl;
}

Error: -11

Fixed submission:

// pair???????????¨???????????????????????£????????§31-179573????????????????????????????????????????????????????????????
#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

int P[10010];
void init(int N) {
  for (int i = 0; i <= N; i++)
    P[i] = i;
}

int root(int a) {
  if (P[a] == a)
    return a;
  return (P[a] = root(P[a]));
}

bool is_same_set(int a, int b) { return root(a) == root(b); }
void unite(int a, int b) { P[root(a)] = root(b); }

int main() {
  int n;
  cin >> n;
  int A[n][n];
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> A[i][j];
    }
  }
  vector<int> cost, src, dst;
  int M = 0;
  for (int r = 0; r < n; r++) {
    for (int c = r + 1; c < n; c++) {
      if (A[r][c] > -1) {
        cost[M] = A[r][c];
        src[M] = r;
        dst[M] = c;
        M += 1;
      }
    }
  }
  pair<int, int> edges[M];
  for (int i = 0; i < M; i++) {
    edges[i] = make_pair(cost[i], i);
  }

  sort(edges, edges + M);
  int s = 0;
  for (int i = 0; i < M; i++) {
    int k = edges[i].second;
    int p = src[k];
    int q = dst[k];
    if (is_same_set(p, q))
      continue;
    unite(p, q);
    s += edges[i].first;
  }
  cout << s << endl;
}
Problem p02241 (C++) — buggy submission, verdict: Runtime Error

// kruskal tree
#include <algorithm>
#include <iostream>
using namespace std;
#define M 9999
int parent[M], a[M], b[M];
pair<int, int> node[M];
int root(int a) { return parent[a] == a ? a : parent[a] = root(parent[a]); }
int unite(int a, int b) {
  int x = root(a), y = root(b);
  if (x == y)
    return 0;
  parent[x] = y;
  return 1;
}
int main() {
  int i, j, s, n, m, x;
  cin >> n;
  for (m = i = 0; i < n; i++)
    for (j = 0; j < n; j++)
      if (cin >> x, ~x)
        a[m] = i, b[m] = j, node[m].first = x, node[m].second = m++;
  sort(node, node + m);
  for (i = 0; i < n; i++)
    parent[i] = i;
  for (s = i = 0; i < m; i++)
    if (unite(a[node[i].second], b[node[i].second]))
      s += node[i].first;
  cout << s << endl;
}

Error: WA

Fixed submission:

// kruskal tree
#include <algorithm>
#include <iostream>
using namespace std;
#define M 999
int parent[M], a[M], b[M];
pair<int, int> node[M];
int root(int a) { return parent[a] == a ? a : parent[a] = root(parent[a]); }
int unite(int a, int b) {
  int x = root(a), y = root(b);
  if (x == y)
    return 0;
  parent[x] = y;
  return 1;
}
int main() {
  int i, j, s, n, m, x;
  cin >> n;
  for (m = i = 0; i < n; i++)
    for (j = 0; j < n; j++)
      if (cin >> x, ~x)
        a[m] = i, b[m] = j, node[m].first = x, node[m].second = m++;
  sort(node, node + m);
  for (i = 0; i < n; i++)
    parent[i] = i;
  for (s = i = 0; i < m; i++)
    if (unite(a[node[i].second], b[node[i].second]))
      s += node[i].first;
  cout << s << endl;
}
Problem p02241 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
#define INF 10000000

int main() {
  long n;
  long G[100][100], d[100];
  long minc, mi, i;
  bool flg[100];
  long sum = 0;

  cin >> n;
  for (i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      cin >> G[i][j];
      if (G[i][j] == -1)
        G[i][j] = INF;
    }
    d[i] = INF;
    flg[i] = false;
  }

  d[0] = 0;
  while (true) {
    minc = INF;
    for (i = 0; i < n; i++) {
      if (!flg[i] && d[i] < minc) {
        minc = d[i];
        mi = i;
      }
    }
    if (minc == INF)
      break;
    flg[mi] = true;
    for (i = 0; i < n; i++) {
      if (G[mi][i] == INF)
        continue;
      if (!flg[i] && d[i] > G[mi][i]) {
        d[i] = G[mi][i];
        // cout << d[i];
      }
    }
  }

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

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;

#define INF 1000000

int main() {
  long n;
  long G[100][100], d[100];
  long minc, mi, i;
  char flgs[i];
  long suma = 0;

  cin >> n;
  for (i = 0; i < n; i++) {
    for (long j = 0; j < n; j++) {
      cin >> G[i][j];
      if (G[i][j] == -1)
        G[i][j] = INF;
    }
    d[i] = INF;
    flgs[i] = 0;
  }

  d[0] = 0;
  while (true) {
    minc = INF;
    for (i = 0; i < n; i++) {
      if (flgs[i] != 1 && d[i] < minc) {
        minc = d[i];
        mi = i;
      }
    }
    if (minc == INF)
      break;
    flgs[mi] = 1;
    // for (i=0;i<n;i++)cout << flgs[i] << " ";
    for (i = 0; i < n; i++) {
      if (G[mi][i] == INF)
        continue;
      if (flgs[i] != 1 && d[i] > G[mi][i]) {
        d[i] = G[mi][i];
        // cout << d[i];
      }
    }
  }

  for (i = 0; i < n; i++) {
    // cout << d[i]<<endl;
    suma += d[i];
  }
  cout << suma << endl;

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

#include <algorithm>
#include <iostream>
#include <utility>
using namespace std;

int m, n, x, ans;
int P[100 + 10], edge1[10000 + 10], edge2[10000 + 10];

int root(int a) {
  if (P[a] == a)
    return a;
  return (P[a] = root(P[a]));
}

bool same(int a, int b) { return root(a) == root(b); }

void unite(int a, int b) { P[root(a)] = root(b); }

int main() {
  pair<int, int> edges[10000 + 10];
  cin >> n;

  for (int i = 1; i <= n; i++) {
    P[i] = i;
  }

  m = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> x;
      if (x > -1) {
        edge1[m] = i;
        edge2[m] = j;
        edges[m] = make_pair(x, m);
        m += 1;
      }
    }
  }

  sort(edges, edges + m);
  n = 1;
  for (int i = 0; i < m; i++) {
    if (n >= m)
      break;
    if (!same(edge1[edges[i].second], edge2[edges[i].second])) {
      unite(edge1[edges[i].second], edge2[edges[i].second]);
      ans += edges[i].first;
      n += 1;
    }
  }

  cout << ans << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <utility>
using namespace std;

int m, n, x, ans;
int P[100 + 10], edge1[2000 + 10], edge2[2000 + 10];

int root(int a) {
  if (P[a] == a)
    return a;
  return (P[a] = root(P[a]));
}

bool same(int a, int b) { return root(a) == root(b); }

void unite(int a, int b) { P[root(a)] = root(b); }

int main() {
  pair<int, int> edges[2000 + 10];
  cin >> n;

  for (int i = 1; i <= n; i++) {
    P[i] = i;
  }

  m = 0;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> x;
      if (x > -1) {
        edge1[m] = i;
        edge2[m] = j;
        edges[m] = make_pair(x, m);
        m += 1;
      }
    }
  }

  sort(edges, edges + m);
  n = 1;
  for (int i = 0; i < m; i++) {
    if (n >= m)
      break;
    if (!same(edge1[edges[i].second], edge2[edges[i].second])) {
      unite(edge1[edges[i].second], edge2[edges[i].second]);
      ans += edges[i].first;
      n += 1;
    }
  }

  cout << ans << endl;
}
Problem p02241 (C++) — buggy submission, verdict: Runtime Error

#include <stdio.h>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1 << 21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, M[MAX][MAX];

int prim() {
  int d[n], p[n];
  int color[n];

  for (int i = 0; i < n; i++) {
    color[i] = WHITE;
    d[i] = INFTY;
    p[i] = -1;
  }

  d[0] = 0;

  while (1) {
    int u;
    int mincost = INFTY;
    for (int i = 0; i < n; i++) {
      if (color[i] != BLACK && d[i] < mincost) {
        mincost = d[i];
        u = i;
      }
    }

    if (mincost == INFTY)
      break;

    color[u] = BLACK;

    for (int v = 0; v <= n; v++) {
      if (color[v] != BLACK && M[u][v] != INFTY) {
        if (M[u][v] < d[v]) {
          d[v] = M[u][v];
          p[v] = u;
          color[v] = GRAY;
        }
      }
    }
  }

  int sum = 0;
  for (int i = 0; i < n; i++) {
    if (p[i] != -1)
      sum += M[i][p[i]];
  }
  return sum;
}

int main() {
  scanf("%d", &n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int tmp;
      scanf("%d", &tmp);
      M[i][j] = (tmp == -1) ? INFTY : tmp;
    }
  }

  int sum = prim();
  printf("%d\\n", sum);

  return 0;
}


Error: -11

Fixed submission:

#include <stdio.h>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1 << 21);
static const int WHITE = 0;
static const int GRAY = 0;
static const int BLACK = 0;

int n, M[MAX][MAX];

int prim() {
  int d[n], p[n];
  int color[n];

  for (int i = 0; i < n; i++) {
    color[i] = WHITE;
    d[i] = INFTY;
  }
  d[0] = 0;
  p[0] = -1;

  while (1) {
    int u;
    int mincost = INFTY;
    for (int i = 0; i < n; i++) {
      if (color[i] != BLACK && d[i] < mincost) {
        mincost = d[i];
        u = i;
      }
    }

    if (mincost == INFTY)
      break;

    color[u] = BLACK;

    for (int v = 0; v <= n; v++) {
      if (color[v] != BLACK && M[u][v] != -1) {
        if (M[u][v] < d[v]) {
          d[v] = M[u][v];
          p[v] = u;
          color[v] = GRAY;
        }
      }
    }
  }

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

  return sum;
}

int main() {
  scanf("%d", n);
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      scanf("%d", &M[i][j]);
    }
  }

  int sum = prim();
  printf("%d\\n", sum);

  return 0;
}