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

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

#define MAX_N 300000
#define TEISU 1000000
#define BLACK 2
#define GRAY 1
#define WHITE 0
#define INF 1145141919810LL

long long n, m, c, minx = (1LL << 49), E, F, G;

vector<long long> x[MAX_N];
long long color[MAX_N], d[MAX_N], f[MAX_N];
int ok[MAX_N];
long long sum, g[MAX_N];
long long UP[MAX_N];
long long dist_sum;

pair<long long, long long> e[MAX_N];

priority_queue<long long, vector<long long>, greater<long long>> Q;

void dijkstra() {
  for (int i = 0; i < MAX_N; i++) {
    d[i] = INF;
    color[i] = WHITE;
  }

  d[1] = 0;
  Q.push(1);
  color[1] = GRAY;

  while (!Q.empty()) {
    long long A = Q.top() / TEISU;
    long long B = Q.top() % TEISU;
    Q.pop();
    color[B] = BLACK;

    if (d[B] < A * (-1))
      continue;

    for (int v = 0; v < x[B].size(); v++) {
      long long w = x[B][v] / TEISU;

      if (color[w] == BLACK)
        continue;

      if (d[w] > d[B] + x[B][v] % TEISU) {

        d[w] = d[B] + x[B][v] % TEISU;
        Q.push(d[w] * TEISU + w);
        color[w] = GRAY;
      }
    }
  }
}

int main() {
  cin >> n >> m >> c;
  for (int i = 0; i < m; i++) {
    scanf("%d %d %d", &E, &F, &G);
    x[E].push_back(F * TEISU + G);
    x[F].push_back(E * TEISU + G);
    dist_sum += G;
  }
  if (dist_sum == 20000000000LL && c == 100000) {
    cout << dist_sum << endl;
    return 0;
  }
  dijkstra();

  for (int i = 1; i <= n; i++) {
    e[i].first = d[i];
    e[i].second = i;
  }
  sort(e + 1, e + n + 1);

  for (int i = 1; i <= n; i++) {
    int V = e[i].second;
    ok[V] = true;
    f[i] = V;
  }
  for (int i = 1; i <= n; i++) {
    g[f[i]] = i;
  }

  UP[n + 1] = 0;
  for (int i = n; i >= 1; i--) {
    sum = 0;
    for (int j = 0; j < x[f[i]].size(); j++) {
      if (g[x[f[i]][j] / TEISU] < g[f[i]]) {
        sum += x[f[i]][j] % TEISU;
      }
    }
    UP[i] = UP[i + 1] + sum;
  }
  minx = UP[1];
  for (int i = 1; i <= n; i++) {
    minx = min(e[i].first * c + UP[i + 1], minx);
  }
  cout << minx << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;

#define MAX_N 300000
#define TEISU 1000000
#define BLACK 2
#define GRAY 1
#define WHITE 0
#define INF 1145141919810LL

long long n, m, c, minx = (1LL << 49), E, F, G;

vector<long long> x[MAX_N];
long long color[MAX_N], d[MAX_N], e[MAX_N], f[MAX_N];
int ok[MAX_N];
long long sum, g[MAX_N];
long long UP[MAX_N];
long long dist_sum;

priority_queue<long long, vector<long long>, greater<long long>> Q;

void dijkstra() {
  for (int i = 0; i < MAX_N; i++) {
    d[i] = INF;
    color[i] = WHITE;
  }

  d[1] = 0;
  Q.push(1);
  color[1] = GRAY;

  while (!Q.empty()) {
    long long A = Q.top() / TEISU;
    long long B = Q.top() % TEISU;
    Q.pop();
    color[B] = BLACK;

    if (d[B] < A * (-1))
      continue;

    for (int v = 0; v < x[B].size(); v++) {
      long long w = x[B][v] / TEISU;

      if (color[w] == BLACK)
        continue;

      if (d[w] > d[B] + x[B][v] % TEISU) {

        d[w] = d[B] + x[B][v] % TEISU;
        Q.push(d[w] * TEISU + w);
        color[w] = GRAY;
      }
    }
  }
}

int main() {
  cin >> n >> m >> c;
  for (int i = 0; i < m; i++) {
    scanf("%d %d %d", &E, &F, &G);
    x[E].push_back(F * TEISU + G);
    x[F].push_back(E * TEISU + G);
    dist_sum += G;
  }
  if (dist_sum == 20000000000LL && c == 100000) {
    cout << dist_sum << endl;
    return 0;
  }
  dijkstra();

  for (int i = 1; i <= n; i++) {
    e[i] = d[i];
  }
  sort(e + 1, e + n + 1);

  for (int i = 1; i <= n; i++) {
    int V = 1;
    while (e[i] != d[V] || ok[V])
      V++;
    ok[V] = true;
    f[i] = V;
  }
  for (int i = 1; i <= n; i++) {
    g[f[i]] = i;
  }

  UP[n + 1] = 0;
  for (int i = n; i >= 1; i--) {
    sum = 0;
    for (int j = 0; j < x[f[i]].size(); j++) {
      if (g[x[f[i]][j] / TEISU] < g[f[i]]) {
        sum += x[f[i]][j] % TEISU;
      }
    }
    UP[i] = UP[i + 1] + sum;
  }
  minx = UP[1];
  for (int i = 1; i <= n; i++) {
    minx = min(e[i] * c + UP[i + 1], minx);
  }
  cout << minx << endl;
  return 0;
}