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

#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>
typedef long long int ll;
#define BIG_NUM 2000000000
#define MOD 1000000007
using namespace std;

#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>
typedef long long int ll;
#define BIG_NUM 2000000000
using namespace std;

struct Info {
  Info(int arg_town, int arg_cost) {
    town = arg_town;
    cost = arg_cost;
  }
  bool operator<(const struct Info &arg) const { return cost > arg.cost; };
  int town, cost;
};

struct Dist {
  Dist(int arg_to, int arg_cost) {
    to = arg_to;
    cost = arg_cost;
  }
  int to, cost;
};

struct Data {
  Data(int arg_start, int arg_goal) {
    start = arg_start;
    goal = arg_goal;
  }
  int start, goal;
};

int main() {

  int N, R, s, t, d;
  scanf("%d %d", &N, &R);

  vector<Dist> V[N + 1];
  int **MIN_COST = new int *[N + 1];
  for (int i = 1; i <= N; i++) {
    MIN_COST[i] = new int[N + 1];
    for (int k = 1; k <= N; k++) {
      if (i != k)
        MIN_COST[i][k] =
            BIG_NUM; //??????????????????????????¨??????????????????
      else {
        MIN_COST[i][k] = 0;
      }
    }
  }

  for (int i = 0; i < R; i++) {
    scanf("%d %d %d", &s, &t, &d);
    V[s].push_back(Dist(t, d));
    V[t].push_back(Dist(s, d));
  }

  int maximum = -1, min_index;
  stack<Data> S;
  priority_queue<Info> Q;
  queue<int> WORK;
  vector<int> ANS;
  vector<int> PRE_TOWN[N + 1];

  bool *VISITED = new bool[N + 1];

  for (int from = 1; from <= N; from++) {

    for (int i = 1; i <= N; i++) {
      PRE_TOWN[i].clear();
      PRE_TOWN[i].push_back(from);
    }

    for (int i = 0; i < V[from].size(); i++) {
      MIN_COST[from][V[from][i].to] = V[from][i].cost;
      Q.push(Info(V[from][i].to, V[from][i].cost));
    }

    while (!Q.empty()) {
      if (Q.top().cost > MIN_COST[from][Q.top().town]) {
        Q.pop();
        continue;
      } else {
        min_index = Q.top().town;
        Q.pop();
      }

      for (int i = 0; i < V[min_index].size(); i++) {
        if (MIN_COST[from][V[min_index][i].to] >
            MIN_COST[from][min_index] + V[min_index][i].cost) {
          MIN_COST[from][V[min_index][i].to] =
              MIN_COST[from][min_index] + V[min_index][i].cost;
          PRE_TOWN[V[min_index][i].to].clear();
          PRE_TOWN[V[min_index][i].to].push_back(min_index);
          Q.push(Info(V[min_index][i].to, MIN_COST[from][V[min_index][i].to]));
        } else if (MIN_COST[from][V[min_index][i].to] ==
                   MIN_COST[from][min_index] + V[min_index][i].cost) {
          PRE_TOWN[V[min_index][i].to].push_back(min_index);
          // V[min_index][i].to??????????°??????????????§????Q????????£????????????????????????????????§????????§????????£????????????
        }
      }
    }

    //?????§?????¢??????????????´????????????????????????????????¢????????§??¨?????£?????°?????????????????????????????§????¶???°???????????????????????????vector????????£??????
    for (int i = 1; i <= N; i++) {
      if (maximum <= MIN_COST[from][i]) {
        if (maximum < MIN_COST[from][i]) {
          // printf("from:%d goal:%d ANS???????????¢\\n",from,i);
          ANS.clear(); //??´??????&????¶???°?????????????????????
        }
        maximum = MIN_COST[from][i];

        for (int k = 1; k <= N; k++)
          VISITED[k] = false;

        WORK.push(i);
        VISITED[i] = true;
        ANS.push_back(i);

        while (!WORK.empty()) {
          for (int k = 0; k < PRE_TOWN[WORK.front()].size(); k++) {
            if (VISITED[PRE_TOWN[WORK.front()][k]] == false) {
              // printf("from:%d goal:%d ?????¨??°:%d
              // ????????°???:%d\\n",from,i,WORK.front(),PRE_TOWN[WORK.front()][k]);
              VISITED[PRE_TOWN[WORK.front()][k]] = true;
              ANS.push_back(PRE_TOWN[WORK.front()][k]);
              WORK.push(PRE_TOWN[WORK.front()][k]);
            }
          }
          WORK.pop();
        }
      }
    }
  }

  bool *USED = new bool[N + 1];
  for (int i = 1; i <= N; i++)
    USED[i] = false;

  for (int i = 0; i < ANS.size(); i++)
    USED[ANS[i]] = true;

  int num = 0;

  for (int i = 1; i <= N; i++)
    if (USED[i] == false)
      num++;

  printf("%d\\n", num);
  for (int i = 1; i <= N; i++) {
    if (USED[i] == false) {
      printf("%d\\n", i);
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <stdio.h>
#include <vector>
typedef long long int ll;
#define BIG_NUM 1000000000
using namespace std;

struct Info {
  Info(int arg_town, int arg_cost) {
    town = arg_town;
    cost = arg_cost;
  }
  bool operator<(const struct Info &arg) const { return cost > arg.cost; };
  int town, cost;
};

struct Dist {
  Dist(int arg_to, int arg_cost) {
    to = arg_to;
    cost = arg_cost;
  }
  int to, cost;
};

struct Data {
  Data(int arg_start, int arg_goal) {
    start = arg_start;
    goal = arg_goal;
  }
  int start, goal;
};

int main() {

  int N, R, s, t, d;
  scanf("%d %d", &N, &R);

  vector<Dist> V[N + 1];
  int **MIN_COST = new int *[N + 1];
  for (int i = 1; i <= N; i++) {
    MIN_COST[i] = new int[N + 1];
    for (int k = 1; k <= N; k++)
      MIN_COST[i][k] = BIG_NUM;
  }

  for (int i = 0; i < R; i++) {
    scanf("%d %d %d", &s, &t, &d);
    V[s].push_back(Dist(t, d));
    V[t].push_back(Dist(s, d));
  }

  int maximum = -1, min_index;
  stack<Data> S;
  priority_queue<Info> Q;

  for (int from = 1; from <= N; from++) {
    for (int i = 0; i < V[from].size(); i++) {
      MIN_COST[from][V[from][i].to] = V[from][i].cost;
      Q.push(Info(V[from][i].to, V[from][i].cost));
    }

    while (!Q.empty()) {
      if (Q.top().cost > MIN_COST[from][Q.top().town]) {
        Q.pop();
        continue;
      } else {
        min_index = Q.top().town;
        Q.pop();
      }

      for (int i = 0; i < V[min_index].size(); i++) {
        if (MIN_COST[from][V[min_index][i].to] >
            MIN_COST[from][min_index] + V[min_index][i].cost) {
          MIN_COST[from][V[min_index][i].to] =
              MIN_COST[from][min_index] + V[min_index][i].cost;
          Q.push(Info(V[min_index][i].to, MIN_COST[from][V[min_index][i].to]));
        }
      }
    }

    for (int i = 1; i <= N; i++) {
      if (i != from && MIN_COST[from][i] != BIG_NUM &&
          MIN_COST[from][i] > maximum) {
        maximum = MIN_COST[from][i];
        while (!S.empty())
          S.pop();
        S.push(Data(from, i));
      } else if (i != from && MIN_COST[from][i] != BIG_NUM &&
                 MIN_COST[from][i] == maximum) {
        S.push(Data(from, i));
      }
    }
  }

  vector<int> G;
  stack<int> D[N + 1];

  bool FLG;

  while (!S.empty()) {
    FLG = true;
    for (int i = 0; i < G.size(); i++) {
      if (G[i] == S.top().start) {
        FLG = false;
        break;
      }
    }
    if (FLG)
      G.push_back(S.top().start);

    D[S.top().start].push(S.top().goal);

    S.pop();
  }

  bool *USED = new bool[N + 1];
  for (int i = 1; i <= N; i++)
    USED[i] = false;

  int start, goal;

  for (int i = 0; i < G.size(); i++) {
    start = G[i];
    USED[start] = true;

    while (!D[start].empty()) {
      goal = D[start].top();
      D[start].pop();
      USED[goal] = true;

      for (int k = 1; k <= N; k++) {
        if (maximum == MIN_COST[start][k] + MIN_COST[k][goal])
          USED[k] = true;
      }
    }
  }

  int num = 0;

  for (int i = 1; i <= N; i++)
    if (USED[i] == false)
      num++;

  printf("%d\\n", num);
  for (int i = 1; i <= N; i++) {
    if (USED[i] == false) {
      printf("%d\\n", i);
    }
  }

  return 0;
}