Problem p03925 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <cwchar>
#include <cwctype>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <math.h>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <stdio.h>
#include <stdlib.h>
#include <streambuf>
#include <string.h>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;
typedef long long ll;
struct edg {
  int w;
  int u, v;
  int nud;
  edg(int _a = 0, int _b = 0, int _c = 0, int _d = 0) {
    w = _a;
    u = _b;
    v = _c;
    nud = _d;
  }
  bool operator<(const edg &a) const {
    if (w != a.w)
      return w < a.w;
    if (u != a.u)
      return u < a.u;
    if (v != a.v)
      return v < a.v;
    return nud < a.nud;
  }
};
int fa[200010];
int rnk[200010];
int get(int i) { return fa[i] == i ? i : fa[i] = get(fa[i]); }
bool sameset(int i, int j) { return get(i) == get(j); }
void unite(int i, int j) {
  i = get(i);
  j = get(j);
  if (rnk[i] < rnk[j]) {
    fa[i] = j;
    return;
  }
  if (rnk[i] > rnk[j]) {
    fa[j] = i;
    return;
  }
  fa[j] = i;
  ++rnk[i];
}
int main(void) {
  int n, q;
  scanf("%d%d", &n, &q);
  priority_queue<edg> pq;
  for (int i = 1; i <= q; ++i) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    pq.push(edg(-w, u, v, 0));
    pq.push(edg(-w - 1, v, u == n - 1 ? 0 : u + 1, 0));
  }
  for (int i = 0; i < n; ++i) {
    fa[i] = i;
  }
  ll res = 0;
  int hve = 0;
  edg t;
  while (hve < n - 1) {
    t = pq.top();
    pq.pop();
    if (sameset(t.u, t.v)) {
      if (t.nud == 20)
        continue;
      pq.push(edg(t.w - 2, t.u == n - 1 ? 0 : t.u + 1,
                  t.v == n - 1 ? 0 : t.v + 1, t.nud + 1));
      continue;
    }
    res += -t.w;
    ++hve;
    unite(t.u, t.v);
    pq.push(edg(t.w - 2, t.u == n - 1 ? 0 : t.u + 1, t.v == n - 1 ? 0 : t.v + 1,
                0));
  }
  printf("%lld\n", res);
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <complex>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <ctype.h>
#include <cwchar>
#include <cwctype>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <math.h>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <stdio.h>
#include <stdlib.h>
#include <streambuf>
#include <string.h>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
using namespace std;
typedef long long ll;
struct edg {
  int w;
  int u, v;
  int nud;
  edg(int _a = 0, int _b = 0, int _c = 0, int _d = 0) {
    w = _a;
    u = _b;
    v = _c;
    nud = _d;
  }
  bool operator<(const edg &a) const {
    if (w != a.w)
      return w < a.w;
    if (u != a.u)
      return u < a.u;
    if (v != a.v)
      return v < a.v;
    return nud < a.nud;
  }
};
int fa[200010];
int rnk[200010];
int get(int i) { return fa[i] == i ? i : fa[i] = get(fa[i]); }
bool sameset(int i, int j) { return get(i) == get(j); }
void unite(int i, int j) {
  i = get(i);
  j = get(j);
  if (rnk[i] < rnk[j]) {
    fa[i] = j;
    return;
  }
  if (rnk[i] > rnk[j]) {
    fa[j] = i;
    return;
  }
  fa[j] = i;
  ++rnk[i];
}
int main(void) {
  int n, q;
  scanf("%d%d", &n, &q);
  priority_queue<edg> pq;
  for (int i = 1; i <= q; ++i) {
    int u, v, w;
    scanf("%d%d%d", &u, &v, &w);
    pq.push(edg(-w, u, v, 0));
    pq.push(edg(-w - 1, v, u == n - 1 ? 0 : u + 1, 0));
  }
  for (int i = 0; i < n; ++i) {
    fa[i] = i;
  }
  ll res = 0;
  int hve = 0;
  edg t;
  while (hve < n - 1) {
    t = pq.top();
    pq.pop();
    if (sameset(t.u, t.v)) {
      if (t.nud == 10)
        continue;
      pq.push(edg(t.w - 2, t.u == n - 1 ? 0 : t.u + 1,
                  t.v == n - 1 ? 0 : t.v + 1, t.nud + 1));
      continue;
    }
    res += -t.w;
    ++hve;
    unite(t.u, t.v);
    pq.push(edg(t.w - 2, t.u == n - 1 ? 0 : t.u + 1, t.v == n - 1 ? 0 : t.v + 1,
                0));
  }
  printf("%lld\n", res);
  return 0;
}