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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
#define MAX_N 100000
#define F first
#define S second
using namespace std;
typedef pair<long long int, long long int> P;
typedef pair<long long int, P> PP;
long long int INF = 1844674407370955461;

long long int n, m, c, sum = 0, sum2 = 0, ans = INF;
P flag[MAX_N + 1] = {};
vector<P> g[MAX_N + 1];
vector<int> vec;
priority_queue<PP> pq;
int main() {
  cin >> n >> m >> c;
  for (int i = 0; i < m; i++) {
    int a, b, e;
    scanf("%d%d%d", &a, &b, &e);
    g[a].push_back(P(b, e));
    g[b].push_back(P(a, e));
    sum += e;
  }

  for (int i = 0; i < g[1].size(); i++) {
    pq.push(make_pair(-g[1][i].S, make_pair(1, g[1][i].F)));
  }

  while (!pq.empty()) {
    PP a = pq.top();
    pq.pop();
    if (!flag[a.S.S].F) {
      if (a.S.S == 1) {
        flag[a.S.S].S = 1;
        continue;
      }
      flag[a.S.S].F = -a.F;
      flag[a.S.S].S = a.S.S;
      for (int i = 0; i < g[a.S.S].size(); i++) {
        pq.push(make_pair(-flag[a.S.S].F - g[a.S.S][i].S,
                          make_pair(a.S.S, g[a.S.S][i].F)));
      }
    }
  }
  sort(flag + 1, flag + n + 1);
  for (int i = 1; i < n + 1; i++) {
    for (int j = 0; j < g[flag[i].S].size(); j++) {
      if (binary_search(vec.begin(), vec.end(), g[flag[i].S][j].F)) {
        sum2 += g[flag[i].S][j].S;
      }
    }
    vec.insert(upper_bound(vec.begin(), vec.end(), flag[i].S), flag[i].S);
    if (flag[i].F == flag[i + 1].F) {
      continue;
    }
    ans = min(ans, sum + c * flag[i].F - sum2);
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
#define MAX_N 100000
#define F first
#define S second
using namespace std;
typedef pair<long long int, long long int> P;
typedef pair<long long int, P> PP;
long long int INF = 1844674407370955461;

long long int n, m, c, sum = 0, sum2 = 0, ans = INF;
P flag[MAX_N + 1] = {};
vector<P> g[MAX_N + 1];
vector<int> vec;
priority_queue<PP> pq;
int main() {
  cin >> n >> m >> c;
  for (int i = 0; i < m; i++) {
    int a, b, e;
    scanf("%d%d%d", &a, &b, &e);
    g[a].push_back(P(b, e));
    g[b].push_back(P(a, e));
    sum += e;
  }

  for (int i = 0; i < g[1].size(); i++) {
    pq.push(make_pair(-g[1][i].S, make_pair(1, g[1][i].F)));
  }

  while (!pq.empty()) {
    PP a = pq.top();
    pq.pop();
    if (!flag[a.S.S].F) {
      if (a.S.S == 1) {
        flag[a.S.S].S = 1;
        continue;
      }
      flag[a.S.S].F = -a.F;
      flag[a.S.S].S = a.S.S;
      for (int i = 0; i < g[a.S.S].size(); i++) {
        pq.push(make_pair(-flag[a.S.S].F - g[a.S.S][i].S,
                          make_pair(a.S.S, g[a.S.S][i].F)));
      }
    }
  }
  sort(flag + 1, flag + n + 1);
  for (int i = 1; i < n + 1; i++) {
    sort(vec.begin(), vec.end());
    for (int j = 0; j < g[flag[i].S].size(); j++) {
      if (binary_search(vec.begin(), vec.end(), g[flag[i].S][j].F)) {
        sum2 += g[flag[i].S][j].S;
      }
    }
    vec.push_back(flag[i].S);
    if (flag[i].F == flag[i + 1].F) {
      continue;
    }
    ans = min(ans, sum + c * flag[i].F - sum2);
  }
  cout << ans << endl;
}