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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}
struct Graph {
  struct Edge {
    int from, to, cost;
    Edge(int from, int to, int cost) : from(from), to(to), cost(cost) {}
    bool operator<(const Edge &f) const {
      return cost != f.cost ? cost > f.cost : // !!INVERSE!!
                 from != f.from ? from < f.from
                                : to < f.to;
    }
  };
  typedef vector<Edge> Edges;
  vector<Edges> G;
  Graph(int size) : G(size) {}
  inline void add(int from, int to, int cost) {
    G[from].push_back(Edge(from, to, cost));
  }
  // O(ElogV)
  pair<vector<int>, vector<int>> dijkstra(int s) {
    int n = G.size();
    vector<int> dist(n, INF);
    dist[s] = 0;
    vector<int> prev(n, -1);
    priority_queue<Edge> que;
    for (que.push(Edge(s, s, 0)); !que.empty();) {
      Edge e = que.top();
      que.pop();
      if (prev[e.to] != -1)
        continue;
      prev[e.to] = e.from;
      FORIT(f, G[e.to]) {
        if (dist[f->to] > dist[f->from] + f->cost) {
          dist[f->to] = dist[f->from] + f->cost;
          que.push(*f);
        }
      }
    }
    return make_pair(dist, prev);
  }
  pair<vector<int>, vector<int>> BellmanFord(int s) {
    int n = G.size();
    vector<int> dist(n, INF);
    dist[s] = 0;
    vector<int> prev(n, -1);
    REP(i, n) {
      FORIT(e, G[i]) {
        if (dist[e->to] > dist[e->from] + e->cost) {
          dist[e->to] = dist[e->from] + e->cost;
          if (i == n - 1) {
            return make_pair(vector<int>(n, -INF), vector<int>(n, -1));
          }
        }
      }
    }
    return make_pair(dist, prev);
  }
  // O(ElogV)
  int prim2(int s, int c) {
    int n = G.size();
    vector<bool> used(n, false);
    int ret = 0;
    int usenum = 0;
    priority_queue<Edge> que;
    for (que.push(Edge(s, s, 0)); !que.empty();) {
      Edge e = que.top();
      que.pop();
      if (used[e.to])
        continue;
      used[e.to] = true;
      ret = max(ret, e.cost);
      usenum++;
      FORIT(f, G[e.to]) {
        if (f->cost >= c && !used[f->to]) {
          que.push(*f);
        }
      }
    }
    if (usenum < n)
      ret = INF;
    return ret;
  }
};
int main() {
  int n, m;
  while (cin >> n >> m, n) {
    Graph graph(n);
    REP(i, m) {
      int u, v, w;
      cin >> u >> v >> w;
      u--;
      v--;
      graph.add(u, v, w);
      graph.add(v, u, w);
    }
    int ans = INF;
    REP(i, n) FORIT(f, graph.G[i]) {
      if (f->from > f->to)
        continue;
      int t = graph.prim2(f->from, f->cost);
      if (t != INF)
        ans = min(ans, t - f->cost);
    }
    if (ans == INF)
      ans = -1;
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}
struct Graph {
  struct Edge {
    int from, to, cost;
    Edge(int from, int to, int cost) : from(from), to(to), cost(cost) {}
    bool operator<(const Edge &f) const {
      return cost != f.cost ? cost > f.cost : // !!INVERSE!!
                 from != f.from ? from < f.from
                                : to < f.to;
    }
  };
  typedef vector<Edge> Edges;
  vector<Edges> G;
  Graph(int size) : G(size) {}
  inline void add(int from, int to, int cost) {
    G[from].push_back(Edge(from, to, cost));
  }
  // O(ElogV)
  pair<vector<int>, vector<int>> dijkstra(int s) {
    int n = G.size();
    vector<int> dist(n, INF);
    dist[s] = 0;
    vector<int> prev(n, -1);
    priority_queue<Edge> que;
    for (que.push(Edge(s, s, 0)); !que.empty();) {
      Edge e = que.top();
      que.pop();
      if (prev[e.to] != -1)
        continue;
      prev[e.to] = e.from;
      FORIT(f, G[e.to]) {
        if (dist[f->to] > dist[f->from] + f->cost) {
          dist[f->to] = dist[f->from] + f->cost;
          que.push(*f);
        }
      }
    }
    return make_pair(dist, prev);
  }
  pair<vector<int>, vector<int>> BellmanFord(int s) {
    int n = G.size();
    vector<int> dist(n, INF);
    dist[s] = 0;
    vector<int> prev(n, -1);
    REP(i, n) {
      FORIT(e, G[i]) {
        if (dist[e->to] > dist[e->from] + e->cost) {
          dist[e->to] = dist[e->from] + e->cost;
          if (i == n - 1) {
            return make_pair(vector<int>(n, -INF), vector<int>(n, -1));
          }
        }
      }
    }
    return make_pair(dist, prev);
  }
  // O(ElogV)
  int prim2(int s, int c) {
    int n = G.size();
    vector<bool> used(n, false);
    int ret = 0;
    int usenum = 0;
    priority_queue<Edge> que;
    for (que.push(Edge(s, s, 0)); !que.empty();) {
      Edge e = que.top();
      que.pop();
      if (used[e.to])
        continue;
      used[e.to] = true;
      ret = max(ret, e.cost);
      usenum++;
      FORIT(f, G[e.to]) {
        if (f->cost >= c && !used[f->to]) {
          que.push(*f);
        }
      }
    }
    if (usenum < n)
      ret = INF;
    return ret;
  }
};
int main() {
  int n, m;
  while (cin >> n >> m, n) {
    Graph graph(n);
    REP(i, m) {
      int u, v, w;
      cin >> u >> v >> w;
      u--;
      v--;
      graph.add(u, v, w);
      graph.add(v, u, w);
    }
    int ans = INF;
    priority_queue<Graph::Edge> que;
    REP(i, n) FORIT(f, graph.G[i]) {
      que.push(*f);
      int t = graph.prim2(f->from, f->cost);
      if (t != INF)
        ans = min(ans, t - f->cost);
    }
    if (ans == INF)
      ans = -1;
    cout << ans << endl;
  }
  return 0;
}