Problem p03405 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <deque>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
const long long mod = 1000000007;
const long long inf = mod * mod;
const long long d2 = (mod + 1) / 2;
const long double EPS = 1e-9;
const long double PI = acos(-1.0);
int ABS(int a) { return max(a, -a); }
long long ABS(long long a) { return max(a, -a); }
long double ABS(long double a) { return max(a, -a); }
int p[2100];
int q[2100];
int r[2100];
int UF[1100];
vector<pair<int, int>> g[1100];
int FIND(int a) {
  if (UF[a] < 0)
    return a;
  return UF[a] = FIND(UF[a]);
}
void UNION(int a, int b) {
  a = FIND(a);
  b = FIND(b);
  if (a == b)
    return;
  UF[a] += UF[b];
  UF[b] = a;
}
long long pw(long long a, long long b) {
  long long ret = 1;
  while (b) {
    if (b % 2)
      ret = ret * a % mod;
    a = a * a % mod;
    b /= 2;
  }
  return ret;
}
pair<int, pair<int, int>> edge[2100];
int vis[2100];
int calc(int a, int b) {
  for (int i = 0; i < 1100; i++)
    vis[i] = 0;
  queue<pair<int, int>> Q;
  Q.push(make_pair(a, 0));
  vis[a] = 1;
  while (Q.size()) {
    int at = Q.front().first;
    int cost = Q.front().second;
    Q.pop();
    if (at == b)
      return cost;
    for (int i = 0; i < g[at].size(); i++) {
      if (vis[g[at][i].first])
        continue;
      Q.push(make_pair(g[at][i].first, max(cost, g[at][i].second)));
    }
  }
  return -1;
}
int main() {
  int a, b;
  long long c;
  scanf("%d%d%lld", &a, &b, &c);
  for (int i = 0; i < b; i++) {
    scanf("%d%d%d", p + i, q + i, r + i);
    p[i]--;
    q[i]--;
    edge[i] = make_pair(r[i], make_pair(p[i], q[i]));
  }
  for (int i = 0; i < a; i++)
    UF[i] = -1;
  std::sort(edge, edge + b);
  long long cur = 0;
  for (int i = 0; i < b; i++) {
    if (FIND(edge[i].second.first) != FIND(edge[i].second.second)) {
      cur += edge[i].first;
      UNION(edge[i].second.first, edge[i].second.second);
      g[edge[i].second.first].push_back(
          make_pair(edge[i].second.second, edge[i].first));
      g[edge[i].second.second].push_back(
          make_pair(edge[i].second.first, edge[i].first));
    }
  }
  if (cur > c) {
    printf("0\n");
    return 0;
  }
  long long ret = 0;
  int tmp = 0;
  int ks = 0;
  for (int i = 0; i < b; i++) {
    long long v = cur + r[i] - calc(p[i], q[i]);
    if (v == c) {
      tmp++;
    } else if (v > c) {
      ks++;
    }
  }
  if (tmp + ks == b)
    ret = pw(2, ks) * (pw(2, tmp) - 2) % mod;
  else
    ret = pw(2, ks) * (pw(2, tmp) - 1) % mod * 2 % mod;
  printf("%lld\n", ret);
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <deque>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
const long long mod = 1000000007;
const long long inf = mod * mod;
const long long d2 = (mod + 1) / 2;
const long double EPS = 1e-9;
const long double PI = acos(-1.0);
int ABS(int a) { return max(a, -a); }
long long ABS(long long a) { return max(a, -a); }
long double ABS(long double a) { return max(a, -a); }
int p[2100];
int q[2100];
int r[2100];
int UF[1100];
vector<pair<int, int>> g[1100];
int FIND(int a) {
  if (UF[a] < 0)
    return a;
  return UF[a] = FIND(UF[a]);
}
void UNION(int a, int b) {
  a = FIND(a);
  b = FIND(b);
  if (a == b)
    return;
  UF[a] += UF[b];
  UF[b] = a;
}
long long pw(long long a, long long b) {
  long long ret = 1;
  while (b) {
    if (b % 2)
      ret = ret * a % mod;
    a = a * a % mod;
    b /= 2;
  }
  return ret;
}
pair<int, pair<int, int>> edge[2100];
int vis[2100];
int calc(int a, int b) {
  for (int i = 0; i < 1100; i++)
    vis[i] = 0;
  queue<pair<int, int>> Q;
  Q.push(make_pair(a, 0));
  vis[a] = 1;
  while (Q.size()) {
    int at = Q.front().first;
    int cost = Q.front().second;
    Q.pop();
    if (at == b)
      return cost;
    for (int i = 0; i < g[at].size(); i++) {
      if (vis[g[at][i].first])
        continue;
      vis[g[at][i].first] = 1;
      Q.push(make_pair(g[at][i].first, max(cost, g[at][i].second)));
    }
  }
  return -1;
}
int main() {
  int a, b;
  long long c;
  scanf("%d%d%lld", &a, &b, &c);
  for (int i = 0; i < b; i++) {
    scanf("%d%d%d", p + i, q + i, r + i);
    p[i]--;
    q[i]--;
    edge[i] = make_pair(r[i], make_pair(p[i], q[i]));
  }
  for (int i = 0; i < a; i++)
    UF[i] = -1;
  std::sort(edge, edge + b);
  long long cur = 0;
  for (int i = 0; i < b; i++) {
    if (FIND(edge[i].second.first) != FIND(edge[i].second.second)) {
      cur += edge[i].first;
      UNION(edge[i].second.first, edge[i].second.second);
      g[edge[i].second.first].push_back(
          make_pair(edge[i].second.second, edge[i].first));
      g[edge[i].second.second].push_back(
          make_pair(edge[i].second.first, edge[i].first));
    }
  }
  if (cur > c) {
    printf("0\n");
    return 0;
  }
  long long ret = 0;
  int tmp = 0;
  int ks = 0;
  for (int i = 0; i < b; i++) {
    long long v = cur + r[i] - calc(p[i], q[i]);
    if (v == c) {
      tmp++;
    } else if (v > c) {
      ks++;
    }
  }
  if (tmp + ks == b)
    ret = pw(2, ks) * (pw(2, tmp) - 2) % mod;
  else
    ret = pw(2, ks) * (pw(2, tmp) - 1) % mod * 2 % mod;
  printf("%lld\n", ret);
}