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

#include <bits/stdc++.h>
#define int long long
#define PB push_back
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
static const int inf = 1ll << 60;
static const int MAX_N = 1e6;
struct edge {
  int to;
  int cost;
  edge() {}
  edge(int p1, int p2) {
    to = p1;
    cost = p2;
  }
};

int N, M, C;
int maxx = 0;
vector<pii> V;
vector<edge> G[MAX_N + 5];
int d[MAX_N + 5];
bool used[MAX_N + 5];
int sum;
int ans = inf;

void dijkstra() {
  priority_queue<pii, vector<pii>, greater<pii>> que;
  fill(d, d + MAX_N, inf);
  d[0] = 0;
  que.push(pii(0, 0));
  while (!que.empty()) {
    pii p = que.top();
    que.pop();
    int v = p.se;
    if (d[v] < p.fi)
      continue;
    for (int i = 0; i < G[v].size(); ++i) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(pii(d[e.to], e.to));
      }
    }
  }
}

int score(int X) {
  int res = X * C;
  int cnt = inf;
  for (int i = 0; i < N; ++i) {
    if (d[i] <= X) {
      // cout<<i<<endl;
      used[i] = true;
      continue;
    }
    cnt = min(cnt, i);
  }
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < G[i].size(); ++j) {
      if (G[i][j].to <= i)
        continue;
      if (used[G[i][j].to] == true && used[i] == true)
        continue;
      res += G[i][j].cost;
    }
  }
  return res;
}

signed main() {
  cin >> N >> M >> C;
  for (int i = 0; i < M; ++i) {
    int a, b, d;
    cin >> a >> b >> d;
    a--;
    b--;
    G[a].PB(edge(b, d));
    G[b].PB(edge(a, d));
    sum += d;
  }
  dijkstra();
  // int X=0;
  for (int i = 0; i < N; ++i) {
    V.PB(pii(d[i], i));
  }
  sort(V.begin(), V.end());
  fill(used, used + N, false);
  for (int i = 0; i < V.size(); ++i) {
    int now = V[i].se;
    for (int j = 0; j < G[now].size(); ++j) {
      edge e = G[now][j];
      if (used[e.to] == true) {
        sum -= e.cost;
      }
      used[now] = true;
    }
    used[now] = true;
    ans = min(ans, V[i].fi * C + sum);
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define int long long
#define PB push_back
#define fi first
#define se second
using namespace std;
typedef pair<int, int> pii;
static const int inf = 1ll << 60;
static const int MAX_N = 1e6;
struct edge {
  int to;
  int cost;
  edge() {}
  edge(int p1, int p2) {
    to = p1;
    cost = p2;
  }
};

int N, M, C;
int maxx = 0;
vector<int> V;
vector<edge> G[MAX_N + 5];
int d[MAX_N + 5];
bool used[MAX_N + 5];
int ans = inf;

void dijkstra() {
  priority_queue<pii, vector<pii>, greater<pii>> que;
  fill(d, d + MAX_N, inf);
  d[0] = 0;
  que.push(pii(0, 0));
  while (!que.empty()) {
    pii p = que.top();
    que.pop();
    int v = p.se;
    if (d[v] < p.fi)
      continue;
    for (int i = 0; i < G[v].size(); ++i) {
      edge e = G[v][i];
      if (d[e.to] > d[v] + e.cost) {
        d[e.to] = d[v] + e.cost;
        que.push(pii(d[e.to], e.to));
      }
    }
  }
}

int score(int X) {
  int res = X * C;
  int cnt = 0;
  for (int i = 0; i < N; ++i) {
    if (d[i] <= X) {
      // cout<<i<<endl;
      used[i] = true;
    }
  }
  for (int i = 0; i < N; ++i) {
    for (int j = 0; j < G[i].size(); ++j) {
      if (G[i][j].to <= i)
        continue;
      if (used[G[i][j].to] == true && used[i] == true)
        continue;
      res += G[i][j].cost;
    }
  }
  return res;
}

signed main() {
  cin >> N >> M >> C;
  for (int i = 0; i < M; ++i) {
    int a, b, d;
    cin >> a >> b >> d;
    a--;
    b--;
    G[a].PB(edge(b, d));
    G[b].PB(edge(a, d));
  }
  dijkstra();
  // int X=0;
  for (int i = 0; i < N; ++i)
    V.PB(d[i]);
  sort(V.begin(), V.end());
  fill(used, used + N, false);
  for (int i = 0; i < V.size(); ++i) {
    int res;
    int X = V[i];
    res = score(X);
    if (ans > res) {
      ans = res;
    }
  }
  cout << ans << endl;
}