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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <queue>
#include <set>
#include <string.h>
#include <string>
#include <vector>

const int MAXN = 1e5;
const int MAX_E = 5e5;
const long long INF = ((long long)1 << 62) - 1;
struct Edge {
  int vx;
  long long w;

  bool operator<(const Edge &e) { return w < e.w; }
};

struct NodeInfo {
  int vx;
  long long *d;

  bool operator<(const NodeInfo &node) { return (*d) < *(node.d); }
};

std::vector<Edge> graph[MAXN];
long long dist[MAXN];
int pre_vx[MAXN];
int visit[MAXN];
int V, E, r;

void init() {
  int n = V;
  while (n--) {
    dist[n] = INF;
  }
  memset(pre_vx, -1, sizeof(pre_vx));
  memset(visit, 0, sizeof(pre_vx));
}

void input() {
  scanf("%d%d%d", &V, &E, &r);
  for (int i = 0; i < E; i++) {
    int s, t, w;
    scanf("%d%d%d", &s, &t, &w);
    graph[s].push_back({t, w});
  }
}

void dij_sp() {
  dist[r] = 0;
  std::set<std::pair<long long, int>> setds;
  setds.insert(std::make_pair(dist[r], r));
  while (!setds.empty()) {
    std::pair<long long, int> fir = *(setds.begin());
    setds.erase(setds.begin());
    //
    int u = fir.second;
    if (dist[u] == INF) {
      return;
    }
    for (std::vector<Edge>::iterator it = graph[u].begin();
         it != graph[u].end(); it++) {
      int v = it->vx;
      if (visit[v]) {
        continue;
      }
      if (dist[v] > dist[u] + it->w) {
        if (dist[v] != INF) {
          setds.erase(setds.find(std::make_pair(dist[v], v)));
        }
        // update the set
        dist[v] = dist[u] + it->w;
        setds.insert(std::make_pair(dist[v], v));
      }
    }
    // mark visited
    visit[u] = 1;
  }
}

void output() {
  for (int i = 0; i < V; i++) {
    if (i == r) {
      printf("0\\n");
    } else {
      if (dist[i] == INF) {
        printf("INF\\n");
      } else {
        printf("%lld\\n", dist[i]);
      }
    }
  }
}

/**
 *http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=GRL_1_A
 */
int main() {
  input();
  init();
  dij_sp();
  output();
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <queue>
#include <string.h>
#include <string>
#include <vector>

const int MAXN = 1e5;
const int MAX_E = 5e5;
const long long INF = ((long long)1 << 62) - 1;
struct Edge {
  int vx;
  long long w;

  bool operator<(const Edge &e) { return w > e.w; }
};

struct NodeInfo {
  int vx;
  long long *d;

  bool operator<(const NodeInfo &node) { return (*d) > *(node.d); }
};

std::vector<Edge> graph[MAXN];
long long dist[MAXN];
int pre_vx[MAXN];
int visit[MAXN];
int V, E, r;

void init() {
  int n = V;
  while (n--) {
    dist[n] = INF;
  }
  memset(pre_vx, -1, sizeof(pre_vx));
  memset(visit, 0, sizeof(pre_vx));
}

void input() {
  scanf("%d%d%d", &V, &E, &r);
  for (int i = 0; i < E; i++) {
    int s, t, w;
    scanf("%d%d%d", &s, &t, &w);
    graph[s].push_back({t, w});
  }
}

void dij_sp() {
  dist[r] = 0;
  visit[r] = 1;
  int cur = r;
  for (std::vector<Edge>::iterator it = graph[cur].begin();
       it != graph[cur].end(); it++) {
    dist[it->vx] = it->w;
  }
  std::vector<NodeInfo> vc(MAXN + 1);
  vc.clear();
  for (int i = 0; i < V; i++) {
    if (i != cur) {
      vc.push_back({i, dist + i});
    }
  }
  //
  std::make_heap(vc.begin(), vc.end());
  while (vc.size()) {
    NodeInfo node = vc[0];
    std::pop_heap(vc.begin(), vc.end());
    vc.pop_back();
    bool change = false;
    if (*node.d != INF) { // the node is accessable, important!!!
      for (std::vector<Edge>::iterator it = graph[node.vx].begin();
           it != graph[node.vx].end(); it++) {
        if (!visit[it->vx]) {
          if (dist[it->vx] == INF) {
            dist[it->vx] = *node.d + it->w;
            change = true;
          } else {
            if (dist[it->vx] > (*node.d + it->w)) {
              dist[it->vx] = *node.d + it->w;
              change = true;
            }
          }
        }
      }
    } else {
      return;
    }
    // mark visited
    visit[node.vx] = 1;
    if (change) {
      std::make_heap(vc.begin(), vc.end());
    }
  }
}

void output() {
  for (int i = 0; i < V; i++) {
    if (i == r) {
      printf("0\\n");
    } else {
      if (dist[i] == INF) {
        printf("INF\\n");
      } else {
        printf("%lld\\n", dist[i]);
      }
    }
  }
}

int main() {
  input();
  init();
  dij_sp();
  output();
  return 0;
}