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

#include <algorithm>
#include <array>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

#define INF_LL 9000000000000000000
#define INF 2000000000

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;

class Union_find {
private:
  vector<int> par;
  vector<int> rank;
  int n;

public:
  Union_find(int a) {
    n = a;
    for (int i = 0; i < n; i++) {
      par.push_back(i);
      rank.push_back(0);
    }
  }

  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }

  bool same(int x, int y) { return find(x) == find(y); }
};

int main(void) {
  int n, k;
  while (cin >> n >> k && (n || k)) {
    vector<PII> G[101];

    REP(i, k) {
      int f;
      cin >> f;
      if (f == 0) {
        int a, b;
        cin >> a >> b;
        priority_queue<PII, vector<PII>, greater<PII>> que;
        int d[101];
        fill(d, d + 101, INF);
        d[a] = 0;
        que.push(make_pair(0, a));
        while (!que.empty()) {
          PII u = que.top();
          que.pop();
          int cost = u.first;
          int v = u.second;
          if (d[v] < u.first)
            continue;
          REP(i, G[v].size()) {
            PII e = G[v][i];
            if (d[e.first] > d[v] + e.second) {
              d[e.first] = d[v] + e.second;
              que.push(PII(d[e.first], e.first));
            }
          }
        }
        if (d[b] == INF) {
          cout << -1 << endl;
        } else {
          cout << d[b] << endl;
        }
      } else {
        int c, d, e;
        cin >> c >> d >> e;
        G[c].push_back(make_pair(d, e));
        G[d].push_back(make_pair(c, e));
      }
    }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

#define INF_LL 9000000000000000000
#define INF 2000000000

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)

using namespace std;

typedef long long ll;
typedef pair<int, int> PII;

class Union_find {
private:
  vector<int> par;
  vector<int> rank;
  int n;

public:
  Union_find(int a) {
    n = a;
    for (int i = 0; i < n; i++) {
      par.push_back(i);
      rank.push_back(0);
    }
  }

  int find(int x) {
    if (par[x] == x) {
      return x;
    } else {
      return par[x] = find(par[x]);
    }
  }

  void unite(int x, int y) {
    x = find(x);
    y = find(y);
    if (x == y)
      return;

    if (rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rank[x] == rank[y])
        rank[x]++;
    }
  }

  bool same(int x, int y) { return find(x) == find(y); }
};

int main(void) {
  int n, k;
  while (cin >> n >> k && (n || k)) {
    vector<PII> G[100];

    REP(i, k) {
      int f;
      cin >> f;
      if (f == 0) {
        int a, b;
        cin >> a >> b;
        priority_queue<PII, vector<PII>, greater<PII>> que;
        int d[100];
        fill(d, d + 100, INF);
        d[a] = 0;
        que.push(make_pair(0, a));
        while (!que.empty()) {
          PII u = que.top();
          que.pop();
          int cost = u.first;
          int v = u.second;
          if (d[v] < u.first)
            continue;
          REP(i, G[v].size()) {
            PII e = G[v][i];
            if (d[e.first] > d[v] + e.second) {
              d[e.first] = d[v] + e.second;
              que.push(PII(d[e.first], e.first));
            }
          }
        }
        if (d[b] == INF) {
          cout << -1 << endl;
        } else {
          cout << d[b] << endl;
        }
      } else {
        int c, d, e;
        cin >> c >> d >> e;
        G[c].push_back(make_pair(d, e));
        G[d].push_back(make_pair(c, e));
      }
    }
  }
}