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

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

#define MAX_N 3001
#define UNDEF -1
#define MAX_INT 2147483647

typedef struct {
  unsigned short int node1;
  unsigned short int node2;
  unsigned short int length;
  unsigned int distance;
} Road;

typedef struct {
  unsigned int distance;
  bool decided;
} Node;

int N, M, K;
vector<Road> I;
vector<Node> J;
Road Rtemp;
Node Ntemp;

Road *MAKEROAD(const unsigned short int node1, const unsigned short int node2,
               const unsigned short int length, const unsigned int distance) {
  Rtemp.node1 = node1;
  Rtemp.node2 = node2;
  Rtemp.length = length;
  Rtemp.distance = distance;
  return &Rtemp;
}

Node *MAKENODE(const unsigned int distance, const bool decided) {
  Ntemp.distance = distance;
  Ntemp.decided = decided;
  return &Ntemp;
}

int main(void) {
  cin >> N >> M >> K;
  int _a, _b, _i;
  for (int i = 0; i < M; i++) {
    cin >> _a >> _b >> _i;
    I.push_back(*MAKEROAD(_a, _b, _i, MAX_INT));
  }
  for (int i = 0; i < K; i++) {
    cin >> _a;
    I.push_back(*MAKEROAD(0, _a, 0, MAX_INT));
  }
  for (int i = 0; i < N + 1; i++) {
    J.push_back(*MAKENODE(MAX_INT, false));
  }
  J.begin()->distance = 0;
  int mindist = 0;
  auto mindist_itr = J.begin();
  int mindist_i = 0;
  bool chg = false;
  while (true) {
    mindist = MAX_INT;
    chg = false;
    for (auto itr = J.begin(); itr < J.end(); itr++) {
      if (!(itr->decided) && itr->distance < mindist) {
        mindist_itr = itr;
        mindist = itr->distance;
        chg = true;
      }
    }
    if (!chg)
      break;
    else {
      mindist_itr->decided = true;
      mindist_i = mindist_itr - J.begin();
      for (auto itr = I.begin(); itr < I.end(); itr++) {
        if (itr->node1 == mindist_i) {
          if (J[itr->node2].distance > J[mindist_i].distance + itr->length) {
            J[itr->node2].distance = J[mindist_i].distance + itr->length;
          }
        }
        if (itr->node2 == mindist_i) {
          if (J[itr->node1].distance > J[mindist_i].distance + itr->length) {
            J[itr->node1].distance = J[mindist_i].distance + itr->length;
          }
        }
      }
    }
  }
  for (auto itr = I.begin(); itr < I.end(); itr++) {
    _a = J[itr->node1].distance + J[itr->node2].distance + itr->length;
    if (_a % 2 == 1)
      _a++;
    itr->distance = _a / 2;
  }
  unsigned int result = 0;
  for (auto itr = I.begin(); itr < I.end(); itr++) {
    result = max(result, itr->distance);
  }
  for (auto itr = J.begin(); itr < J.end(); itr++) {
    result = max(result, itr->distance);
  }
  cout << result << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdint>
#include <fstream>
#include <iostream>

using namespace std;

#define MAX_N 3000
#define MAX_M 100000
#define UNDEF UINT32_MAX

uint32_t N, M, K, I[MAX_M * 2][3], D[MAX_N], LS[MAX_N], RC[MAX_M];
bool S[MAX_N];
uint32_t dist[MAX_N];
bool decided[MAX_N], updated;

uint32_t min(uint32_t a, uint32_t b) {
  if (a < b)
    return a;
  else
    return b;
}

uint32_t max(uint32_t a, uint32_t b) {
  if (a > b)
    return a;
  else
    return b;
}

int main(void) {
  for (int i = 0; i < MAX_N; i++) {
    D[i] = UNDEF;
    S[i] = false;
    LS[i] = UNDEF;
    I[i * 2][0] = UNDEF;
    I[i * 2][1] = UNDEF;
    I[i * 2][2] = UNDEF;
    I[(i * 2) + 1][0] = UNDEF;
    I[(i * 2) + 1][1] = UNDEF;
    I[(i * 2) + 1][2] = UNDEF;
  }
  cin >> N >> M >> K;
  int a, b, n;
  for (int i = 0; i < MAX_M; i++)
    RC[i] = UNDEF;
  for (int i = 0; i < M; i++) {
    cin >> a >> b >> n;
    a--;
    b--;
    I[(i * 2)][0] = a;
    I[(i * 2)][1] = b;
    I[(i * 2)][2] = n;
    I[(i * 2) + 1][0] = b;
    I[(i * 2) + 1][1] = a;
    I[(i * 2) + 1][2] = n;
  }
  for (int i = 0; i < K; i++) {
    cin >> n;
    n--;
    S[n] = true;
    LS[i] = n;
  }
  for (int e = 0; e < K; e++) {
    for (int ii = 0; ii < MAX_N; ii++) {
      dist[ii] = UNDEF;
      decided[ii] = false;
    }
    dist[LS[e]] = 0;
    bool loop = true;
    while (loop) {
      updated = false;
      int minn = UNDEF;
      int mini = -1;
      for (int ii = 0; ii < N; ii++) {
        if (minn >= dist[ii] && !decided[ii]) {
          mini = ii;
          minn = dist[ii];
          updated = true;
        }
      }
      decided[mini] = true;
      if (updated) {
        for (int ii = 0; ii < M * 2; ii++) {
          if (I[ii][0] == mini && I[ii][2] + dist[I[ii][0]] < dist[I[ii][1]]) {
            dist[I[ii][1]] = I[ii][2] + dist[I[ii][0]];
          }
        }
      } else {
        loop = false;
      }
    }
    for (int ii = 0; ii < N; ii++) {
      if (dist[ii] == UNDEF) {
        exit(-1);
      }
      D[ii] = min(D[ii], dist[ii]);
    }
  }
  for (int i = 0; i < M; i++) {
    n = (D[I[i * 2][0]] + D[I[i * 2][1]] + I[i * 2][2]);
    if (n % 2 == 0)
      n = n / 2;
    else
      n = (n + 1) / 2;
    RC[i] = n;
  }
  uint32_t result = 0;
  for (int i = 0; i < N; i++)
    result = max(D[i], result);
  for (int i = 0; i < M; i++)
    result = max(RC[i], result);
  cout << result << endl;
  return 0;
}