Problem p00180 (C++) — buggy submission, verdict: Runtime Error

#include <bitset>
#include <iostream>
#include <limits.h>
#include <queue>
#include <vector>
using namespace std;

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

struct Bridge {
  int a, b, cost;
};

bool operator<(const Bridge &b1, const Bridge &b2) { return b1.cost < b2.cost; }

bool operator>(const Bridge &b1, const Bridge &b2) { return b1.cost > b2.cost; }

int n, m, a, b, cost;
const int MAX_N = 100;
int T[MAX_N][MAX_N];
priority_queue<Bridge, vector<Bridge>, greater<Bridge>> q;

int main() {
  while (cin >> n >> m, n | m) {
    fill(&T[0][0], &T[0][0] + MAX_N * MAX_N, INT_MAX);
    rep(i, m) {
      cin >> a >> b >> cost;
      T[a][b] = T[b][a] = cost;
    }

    q = priority_queue<Bridge, vector<Bridge>, greater<Bridge>>();
    rep(i, n) {
      if (T[0][i] != INT_MAX) {
        Bridge bdg;
        bdg.a = 0;
        bdg.b = i;
        bdg.cost = T[0][i];
        q.push(bdg);
      }
    }

    int total = 0;
    bitset<MAX_N> visited;
    visited.set(0);
    while (!q.empty()) {
      Bridge bdg = q.top();
      q.pop();
      if (visited[bdg.b])
        continue;

      total += bdg.cost;
      visited.set(bdg.b);
      rep(i, n) {
        if (!visited[i] && T[bdg.b][i] != INT_MAX) {
          Bridge new_bdg;
          new_bdg.a = bdg.b;
          new_bdg.b = i;
          new_bdg.cost = T[bdg.b][i];
          q.push(new_bdg);
        }
      }

      if (visited.count() == n)
        break;
    }

    cout << total << endl;
  }
}

Error: 0

Fixed submission:


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

using namespace std;

class Bridge {
public:
  int from;
  int to;
  int cost;

  Bridge() : from(0), to(0), cost(0) {}
  Bridge(const Bridge &b) : from(b.from), to(b.to), cost(b.cost) {}

  bool operator>(const Bridge &b) const { return cost > b.cost; }

  bool operator<(const Bridge &b) const { return cost < b.cost; }
};

int nCity, nBridge;

Bridge bridge[100];
bool city[100];

int main() {
  while (1) {
    // load data
    cin >> nCity >> nBridge;
    if (nCity == 0 && nBridge == 0) {
      break;
    }

    for (int i = 0; i < nBridge; i++) {
      bridge[i] = Bridge();
      cin >> bridge[i].from >> bridge[i].to >> bridge[i].cost;
    }

    for (int i = 0; i < nCity; i++) {
      city[i] = false;
    }

    // set initial node to queue
    priority_queue<Bridge, vector<Bridge>, greater<Bridge>> que;
    for (int i = 0; i < nBridge; i++) {
      if (bridge[i].from == 0 || bridge[i].to == 0) {
        que.push(bridge[i]);
      }
    }

    city[0] = true;

    // iteration
    int totalCost = 0;
    while (!que.empty()) {
      // end check
      bool isEnd = true;
      for (int i = 0; i < nCity; i++) {
        if (!city[i]) {
          isEnd = false;
          break;
        }
      }

      if (isEnd) {
        break;
      }

      // check highest priority node
      Bridge current = que.top();
      que.pop();

      totalCost += current.cost;

      int next = 0;
      if (!city[current.to]) {
        city[current.to] = true;
        next = current.to;
      }

      if (!city[current.from]) {
        city[current.from] = true;
        next = current.from;
      }

      for (int i = 0; i < nBridge; i++) {
        if (bridge[i].to == next || bridge[i].from == next) {
          if (!city[bridge[i].to] || !city[bridge[i].from]) {
            que.push(bridge[i]);
          }
        }
      }
    }

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