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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

struct Edge {
  int s, t, c;
  Edge(int s, int t, int c) : s(s), t(t), c(c) {}
};
bool operator<(const Edge &a, const Edge &b) { return a.c < b.c; }

struct UnionFind {
  vector<int> data;
  UnionFind(int size) : data(size, -1) {}
  bool unionSet(int x, int y) {
    x = root(x);
    y = root(y);
    if (x != y) {
      if (data[y] < data[x])
        swap(x, y);
      data[x] += data[y];
      data[y] = x;
    }
    return x != y;
  }
  int root(int x) { return data[x] < 0 ? x : data[x] = root(data[x]); }
  int size(int x) { return -data[root(x)]; }
};

int main() {
  int n, m;
  while (cin >> n >> m, n || m) {
    vector<Edge> es;
    REP(i, m) {
      int a, b, c;
      cin >> a >> b >> c;
      a--;
      b--;
      es.push_back(Edge(a, b, c));
    }

    sort(ALL(es));
    int ans = INF;
    for (int i = 0; i < m; ++i) {
      UnionFind uf(n);
      for (int j = i; j < m; ++j) {
        uf.unionSet(es[j].s, es[j].t);
        if (uf.size(es[j].s) == n) {
          ans = min(ans, es[j].c - es[i].c);
        }
      }
    }
    if (ans == INF)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <valarray>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
const int INF = 1 << 29;

typedef int Weight;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};

bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
             e.src != f.src ? e.src < f.src
                            : e.dst < f.dst;
}

typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

// Cð½·àÌªub¤É èACð½·Å¬lðßéÆ«B
template <class T> T lower_search(T lb, T ub, bool (*C)(T)) {
  while (ub - lb > 1) {
    T mid = (lb + ub) / 2;
    if (C(mid))
      ub = mid;
    else
      lb = mid;
  }
  return ub;
}

bool ok(const Graph &g) {
  int n = g.size();
  queue<int> Q;
  vector<bool> visited(n);
  Q.push(0);
  int cnt = 0;
  while (!Q.empty()) {
    int now = Q.front();
    Q.pop();
    if (visited[now])
      continue;
    visited[now] = 1;
    cnt++;
    FOR(e, g[now]) {
      if (!visited[e->dst])
        Q.push(e->dst);
    }
  }
  return cnt == n;
}

Graph g;
int n, m;

bool C(int w) {
  for (int i = 1; i <= 10000 - w; ++i) {
    Graph g2(n);
    REP(j, n) {
      FOR(jt, g[j]) {
        if (i <= jt->weight && jt->weight <= i + w)
          g2[j].push_back(*jt);
      }
    }
    // if (i == 6 && w == 4) cout << "hoge" << endl;
    if (ok(g2))
      return 1;
  }
  return 0;
}

int main() {
  while (cin >> n >> m, n || m) {
    g = Graph(n);
    REP(i, m) {
      int a, b, c;
      cin >> a >> b >> c;
      a--;
      b--;
      g[a].push_back(Edge(a, b, c));
      g[b].push_back(Edge(b, a, c));
    }
    int ans = lower_search<int>(-1, 10002, C);
    if (ans == 10002)
      cout << -1 << endl;
    else
      cout << ans << endl;
  }
}