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

#include <algorithm>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#define cost first
#define node second
using namespace std;
typedef pair<int, int> P;
int n, m;
vector<P> V[101];

int prim(int mn) {
  int D[101] = {}, visited[101] = {};
  for (int i = 0; i <= n; i++)
    D[i] = 100000;
  priority_queue<P, vector<P>, greater<P>> Q;
  Q.push((P){0, 1});

  int cnt = 0, mxc = 0, mnc = 10000;
  while (!Q.empty()) {
    P t = Q.top();
    Q.pop();
    if (visited[t.node] == 1)
      continue;
    visited[t.node] = 1, cnt++;

    if (cnt != 1)
      mxc = max(mxc, t.cost), mnc = min(mnc, t.cost);
    if (cnt == n && mnc == mn)
      return mxc;
    if (cnt == n)
      return -1;
    for (int i = 0; i < V[t.node].size(); i++) {
      int nn = V[t.node][i].node, ncost = V[t.node][i].cost;
      if (ncost < mn || visited[nn] == 1 || D[nn] <= ncost)
        continue;
      D[nn] = ncost;
      Q.push((P){ncost, nn});
    }
  }
  return -1;
}

int main() {
  while (1) {
    cin >> n >> m;
    int cost[5001];
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i <= n; i++)
      V[i].clear();
    for (int i = 0; i < m; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      V[a].push_back((P){c, b});
      V[b].push_back((P){c, a});
      cost[i] = c;
    }
    sort(cost, cost + m);
    int ans = 100000;
    for (int i = 0; i < m; i++) {
      int res = prim(cost[i]);
      if (res != -1)
        ans = min(ans, res - cost[i]);
    }
    if (ans == 100000)
      ans = -1;
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <vector>
using namespace std;
struct po {
  int node, cost;
};
int n, m, cnt;
vector<po> V[101];
bool visited[101];

bool check(int node, int mx, int mn) {
  if (cnt == n)
    return true;
  for (int i = 0; i < V[node].size(); i++) {
    int nn = V[node][i].node;
    int ncost = V[node][i].cost;
    if (visited[nn] == 1 || ncost > mx || mn > ncost)
      continue;
    cnt++;
    visited[nn] = 1;
    if (check(nn, mx, mn) == true)
      return true;
    ;
  }
  return false;
}

int main() {
  while (1) {
    cin >> n >> m;
    if (n == 0 && m == 0)
      break;
    for (int i = 0; i <= n; i++)
      V[i].clear();
    int cost[5001] = {};
    for (int i = 0; i < m; i++) {
      int a, b, c;
      cin >> a >> b >> c;
      V[a].push_back((po){b, c});
      V[b].push_back((po){a, c});
      cost[i] = c;
    }
    sort(cost, cost + m);
    for (int i = 0; i < m - 1; i++)
      if (cost[i] == cost[i + 1] && cost[i + 1] == cost[i + 2])
        cost[i] = -1;
    int ans = 10001;
    memset(visited, 0, sizeof(visited)), cnt = visited[1] = 1;
    if (check(1, cost[m - 1], cost[0]))
      ans = cost[m - 1] - cost[0];
    if (ans != 10001)
      for (int i = 0; i < m; i++)
        for (int j = i + 1; j < m; j++) {
          if (cost[j] - cost[i] >= ans || cost[i] == -1 || cost[j] == -1)
            continue;
          memset(visited, 0, sizeof(visited)), cnt = visited[1] = 1;
          if (check(1, cost[j], cost[i]))
            ans = min(ans, cost[j] - cost[i]);
        }
    if (ans == 10001)
      ans = -1;
    cout << ans << endl;
  }
  return 0;
}