Problem p00180 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <random>
#include <string.h>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
const double pi = acos(-1);
#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= (a); i--)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  REP(i, v.size()) {
    if (i)
      os << " ";
    os << v[i];
  }
  return os;
}
template <typename T>
ostream &operator<<(ostream &os, const vector<vector<T>> &v) {
  REP(i, v.size()) {
    if (i)
      os << endl;
    os << v[i];
  }
  return os;
}

class union_find {
public:
  union_find(int n) : par_(n, -1) {}
  void init(int n) { par_.assign(n, -1); }

  int root(int x) { return par_[x] < 0 ? x : par_[x] = root(par_[x]); }

  bool unite(int x, int y) {
    x = root(x);
    y = root(y);
    if (x == y) {
      return false;
    } else {
      if (par_[x] < par_[y]) {
        par_[x] += par_[y];
        par_[y] = x;
      } else {
        par_[y] += par_[x];
        par_[x] = y;
      }
      return true;
    }
  }

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

  int size(int x) { return -par_[root(x)]; }

private:
  std::vector<int> par_;
};

// s
using weight = int;

struct edge {
  int from, to;
  weight cost;
  bool operator<(edge const &rhs) const { return cost > rhs.cost; }
};

using edges = std::vector<edge>;
using graph = std::vector<edges>;

edges kruskal(int V, edges &es) {
  std::sort(es.rbegin(), es.rend());
  union_find uf(V);
  edges res;
  for (int i = 0; i < es.size(); ++i) {
    edge e = es[i];
    if (!uf.same(e.from, e.to)) {
      uf.unite(e.from, e.to);
      res.push_back(e);
    }
  }
  return res;
}

edges prim(graph &g) {
  edges res;
  std::vector<bool> used(g.size(), 0);
  priority_queue<edge> que;
  que.push((edge{-1, 0, 0}));
  while (!que.empty()) {
    edge e = que.top();
    que.pop();
    if (used[e.to])
      continue;
    if (e.from >= 0) {
      res.push_back(e);
    }
    used[e.to] = true;
    for (auto ee : g[e.to]) {
      if (!used[ee.to]) {
        que.push(ee);
      }
    }
  }
  sort(res.rbegin(), res.rend());
  return res;
}

int main() {
  int n, m;
  cin >> n >> m;
  edges es, answer;
  while (n != 0 && m != 0) {
    int sum = 0;
    edge e;
    es.erase(es.begin(), es.begin() + es.size());
    for (int i = 0; i < m; ++i) {
      int v1, v2, c;
      cin >> v1 >> v2 >> c;
      e = edge{v1, v2, c};
      es.push_back(e);
    }
    answer = kruskal(n, es);
    for (auto a : answer) {
      sum += a.cost;
    }
    cout << sum << endl;
    cin >> n >> m;
  }
}

Error: TLE

Fixed submission:

#include <climits>
#include <iostream>
#include <vector>

int main() {
  int a[2];
  while (std::cin >> a[0] >> a[1]) {
    int i, j;
    int city[a[0]][a[0]];
    int Tcity[a[0]];
    int cost = 0;
    for (i = 0; i < a[0]; i++) {
      Tcity[i] = 0;
      for (j = 0; j < a[0]; j++) {
        city[i][j] = INT_MAX;
      }
    }
    std::vector<std::vector<int>> v;
    for (i = 0; i < a[1]; i++) {
      std::vector<int> w;
      int s, g, c;
      std::cin >> s >> g >> c;
      city[s][g] = c;
      w.push_back(s);
      w.push_back(g);
      v.push_back(w);
    }
    Tcity[0] = 1;
    while (1) {
      int min = INT_MAX;
      int min_s, min_g;
      for (i = 0; i < a[0]; i++) {
        if (Tcity[i] == 1) {
          for (j = 0; j < a[0]; j++) {
            if (city[i][j] < min) {
              min = city[i][j];
              min_s = i;
              min_g = j;
            }
          }
        }
      }
      Tcity[min_s] = 1;
      Tcity[min_g] = 1;
      cost += min;
      int loop = 1;
      for (j = 0; j < a[0]; j++) {
        if (Tcity[j] == 0)
          loop = 0;
      }
      if (loop) {
        std::cout << cost << std::endl;
        break;
      }
    }
  }
  return 0;
}