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

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <tuple>
#include <vector>

#pragma warning(disable : 4996)

using namespace std;

int N, M, C, A, B, E;
long long sum;

vector<long long> P;

vector<tuple<int, int, int>> edges;

vector<vector<pair<int, int>>> G;

int main() {
  scanf("%d", &N);
  scanf("%d", &M);
  scanf("%d", &C);

  G.resize(N);

  for (int i = 0; i < M; i++) {
    scanf("%d", &A);
    scanf("%d", &B);
    scanf("%d", &E);

    sum += E;

    G[A - 1].push_back(make_pair(B - 1, E));
    G[B - 1].push_back(make_pair(A - 1, E));

    edges.push_back(make_tuple(A - 1, B - 1, E));
  }

  vector<long long> D(N, 999999999999999999LL);
  D[0] = 0;

  priority_queue<pair<long long, int>, vector<pair<long long, int>>,
                 greater<pair<long long, int>>>
      que;

  que.push(make_pair(0LL, 0));

  while (!que.empty()) {
    pair<long long, int> p1 = que.top();
    que.pop();

    long long dist = p1.first;

    int node = p1.second;

    for (int i = 0; i < G[node].size(); i++) {
      if (D[G[node][i].first] > D[node] + G[node][i].second) {
        D[G[node][i].first] = D[node] + G[node][i].second;

        que.push(make_pair(D[G[node][i].first], G[node][i].first));
      }
    }
  }

  vector<pair<long long, int>> I;

  for (int i = 0; i < M; i++) {
    I.push_back(make_pair(max(D[get<0>(edges[i])], D[get<1>(edges[i])]),
                          get<2>(edges[i])));
  }

  sort(I.begin(), I.end());

  vector<long long> J;
  for (int i = 0; i < M; i++)
    J.push_back(I[i].first);

  vector<int> K;
  for (int i = 0; i < M; i++)
    K.push_back(I[i].second);

  long long ret = 999999999999999999LL;

  for (int i = 0; i < N; i++) {
    long long cost = C * D[i] + sum;

    int ptr = upper_bound(J.begin(), J.end(), D[i]) - J.begin();

    for (int j = 0; j < ptr; j++) {
      cost -= K[j];
    }

    ret = min(ret, cost);
  }

  printf("%lld\\n", ret);

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>

#pragma warning(disable : 4996)

using namespace std;

int N, M, C, A, B, E;

vector<vector<pair<int, int>>> G;

int main() {
  scanf("%d", &N);
  scanf("%d", &M);
  scanf("%d", &C);

  G.resize(N);

  for (int i = 0; i < M; i++) {
    scanf("%d", &A);
    scanf("%d", &B);
    scanf("%d", &E);

    G[A - 1].push_back(make_pair(B - 1, E));
    G[B - 1].push_back(make_pair(A - 1, E));
  }

  vector<long long> D(N, 999999999999999999LL);
  D[0] = 0;

  priority_queue<pair<long long, int>, vector<pair<long long, int>>,
                 greater<pair<long long, int>>>
      que;

  que.push(make_pair(0LL, 0));

  while (!que.empty()) {
    pair<long long, int> p1 = que.top();
    que.pop();

    long long dist = p1.first;

    int node = p1.second;

    for (int i = 0; i < G[node].size(); i++) {
      if (D[G[node][i].first] > D[node] + G[node][i].second) {
        D[G[node][i].first] = D[node] + G[node][i].second;

        que.push(make_pair(D[G[node][i].first], G[node][i].first));
      }
    }
  }

  long long ret = 999999999999999999LL;

  for (int i = 0; i < N; i++) {
    long long cost = C * D[i] * 2;

    for (int j = 0; j < N; j++) {
      for (int k = 0; k < G[j].size(); k++) {
        if (D[j] > D[i] || D[G[j][k].first] > D[i]) {
          cost += G[j][k].second;
        }
      }
    }

    ret = min(ret, cost / 2);
  }

  printf("%lld\\n", ret);

  return 0;
}