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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define CLEAR(arr, val) memset(arr, val, sizeof(arr))

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

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

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

const double PI = acos(-1.0);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

struct Edge {
  int to, cost;
  Edge(int to, int cost) : to(to), cost(cost) {}
};
int n, m;
vector<Edge> edge[128];
bool spanning(int mini, int maxi) {
  bool used[128];
  fill_n(used, n, false);
  queue<int> q;
  used[0] = true;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    foreach (e, edge[v]) {
      if (!used[e->to] && mini <= e->cost && e->cost <= maxi) {
        q.push(e->to);
        used[e->to] = true;
      }
    }
  }
  return count(used, used + n, true) == n;
}
int main() {
  while (cin >> n >> m, n) {
    vector<int> w;
    rep(i, n) edge[i].clear();
    rep(i, m) {
      int a, b, t;
      cin >> a >> b >> t;
      --a, --b;
      edge[a].push_back(Edge(b, t));
      edge[b].push_back(Edge(a, t));
      w.push_back(t);
    }
    sort(all(w));
    w.erase(unique(all(w)), w.end());

    const int INF = 1 << 26;
    int res = INF;
    for (int i = 0; i < w.size(); ++i) {
      int low = i - 1, high = w.size();
      while (low + 1 < high) {
        int mid = (low + high) / 2;
        if (spanning(w[i], w[mid]))
          high = mid;
        else
          low = mid;
      }
      if (high < w.size())
        min_swap(res, w[high] - w[i]);
    }

    if (res == INF)
      res = -1;
    cout << res << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define CLEAR(arr, val) memset(arr, val, sizeof(arr))

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

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

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

const double PI = acos(-1.0);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

struct Edge {
  int to, cost;
  Edge(int to, int cost) : to(to), cost(cost) {}
};
int n, m;
vector<Edge> edge[128];
bool spanning(int mini, int maxi) {
  bool used[128];
  fill_n(used, n, false);
  queue<int> q;
  used[0] = true;
  q.push(0);
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    foreach (e, edge[v]) {
      if (!used[e->to] && mini <= e->cost && e->cost <= maxi) {
        q.push(e->to);
        used[e->to] = true;
      }
    }
  }
  return count(used, used + n, true) == n;
}
int main() {
  while (cin >> n >> m, n) {
    vector<int> weight;
    rep(i, n) edge[i].clear();
    rep(i, m) {
      int a, b, w;
      cin >> a >> b >> w;
      --a, --b;
      edge[a].push_back(Edge(b, w));
      edge[b].push_back(Edge(a, w));
      weight.push_back(w);
    }
    sort(all(weight));
    weight.erase(unique(all(weight)), weight.end());
    vector<pint> wp;
    for (int i = 0; i < weight.size(); ++i)
      for (int j = i; j < weight.size(); ++j)
        wp.push_back(pint(weight[j] - weight[i], weight[i]));
    sort(all(wp));

    const int INF = 1 << 26;
    int res = INF;
    for (int i = 0; i < wp.size() && res == INF; ++i)
      if (spanning(wp[i].second, wp[i].second + wp[i].first))
        res = wp[i].first;

    if (res == INF)
      res = -1;
    cout << res << endl;
  }
}