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

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define PB push_back
#define PPB pop_back
typedef pair<int, int> pii;
static const int INF = 1LL << 61;
static const int MAX_N = 100005;

int n, m, c, d[MAX_N], cumu[MAX_N], visit[MAX_N];
vector<pii> G[MAX_N], res;

void dijkstra() {
  fill(d, d + MAX_N, INF);
  priority_queue<pii, vector<pii>, greater<pii>> q;
  d[1] = 0;
  q.push(pii(0, 1));

  while (q.size()) {
    pii p = q.top();
    q.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;

    for (int i = 0; i < G[v].size(); ++i) {
      pii e = G[v][i];
      int to = e.first, cost = e.second;

      if (d[to] < cost + d[v])
        continue;
      d[to] = cost + d[v];
      q.push(pii(d[to], to));
    }
  }
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n >> m >> c;
  for (int i = 0; i < m; ++i) {
    int a, b, d;
    cin >> a >> b >> d;
    G[a].PB(pii(b, d));
    G[b].PB(pii(a, d));
  }

  dijkstra();

  for (int i = 1; i <= n; ++i) {
    res.PB(pii(d[i], i));
  }
  sort(res.begin(), res.end());

  for (int i = res.size() - 1; 0 <= i; --i) {
    int v = res[i].second;
    visit[v] = true;
    cumu[i] += cumu[i + 1];
    for (int j = 0; j < G[v].size(); ++j) {
      int to = G[v][j].first;
      if (visit[to])
        continue;
      cumu[i] += G[v][j].second;
    }
  }

  int ans = INF;
  for (int i = 0; i < res.size(); ++i) {
    int temp = res[i].first * c, v = res[i].second;

    temp += cumu[i + 1];
    ans = min(ans, temp);
  }

  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define PB push_back
#define PPB pop_back
typedef pair<int, int> pii;
static const int INF = 1LL << 61;
static const int MAX_N = 100005;

int n, m, c, d[MAX_N], visit[MAX_N];
vector<pii> G[MAX_N], res;

void dijkstra() {
  fill(d, d + MAX_N, INF);
  priority_queue<pii, vector<pii>, greater<pii>> q;
  d[1] = 0;
  q.push(pii(0, 1));

  while (q.size()) {
    pii p = q.top();
    q.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;

    for (int i = 0; i < G[v].size(); ++i) {
      pii e = G[v][i];
      int to = e.first, cost = e.second;

      if (d[to] < cost + d[v])
        continue;
      d[to] = cost + d[v];
      q.push(pii(d[to], to));
    }
  }
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n >> m >> c;
  for (int i = 0; i < m; ++i) {
    int a, b, d;
    cin >> a >> b >> d;
    G[a].PB(pii(b, d));
    G[b].PB(pii(a, d));
  }

  dijkstra();

  for (int i = 1; i <= n; ++i) {
    res.PB(pii(d[i], i));
  }
  sort(res.begin(), res.end());

  int ans = INF, pre = -1;
  for (int i = 0; i < res.size(); ++i) {
    pii p = res[i];
    if (p.first == pre)
      continue;
    pre = p.first;
    visit[p.second] = true;
    int temp = c * p.first;
    for (int j = 0; j < res.size(); ++j) {
      pii tp = res[j];
      int v = tp.second, cost = tp.first;
      for (int k = 0; k < G[v].size(); ++k) {
        int tto = G[v][k].first;
        if (tto < v || (d[v] <= pre && d[tto] <= pre))
          continue;
        temp += G[v][k].second;
      }
    }

    ans = min(ans, temp);
  }

  cout << ans << endl;

  return 0;
}