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

#include <bits/stdc++.h>

const int INF = (1 << 30);

typedef struct Network {
  std::vector<std::tuple<int, int>> *edges;
  int v;
} Network;

namespace hidden {
std::stack<Network *> networks;
};
void push(Network &network) { hidden::networks.push(&network); }
void pop(Network &network) { hidden::networks.pop(); }

Network initNetwork(int v) {
  Network network;
  network.edges = new std::vector<std::tuple<int, int>>[v + 3];
  network.v = v + 3;
  return network;
}

void deleteNetwork(Network network) {
  if (network.edges == nullptr) {
    delete[] network.edges;
  }
}

void addEdge(int from, int to, int weight) {
  Network *network = hidden::networks.top();
  network->edges[from].push_back(std::tuple<int, int>(to, weight));
}

std::vector<int> dijkstra(Network network, int source) {
  std::vector<int> res(network.v, INF);
  res[source] = 0;
  std::priority_queue<std::tuple<int, int>> q;
  q.push(std::tuple<int, int>(0, source));
  while (not q.empty()) {
    int dist;
    int id;
    std::tie(dist, id) = q.top();
    q.pop();
    dist = -dist;
    for (std::tuple<int, int> edge : network.edges[id]) {
      int next;
      int weight;
      std::tie(next, weight) = edge;
      int nextdist = dist + weight;
      if (nextdist >= res[next])
        continue;
      res[next] = nextdist;
      q.push(std::tuple<int, int>(-nextdist, next));
    }
  }
  return res;
}

int main() {
  int v, e, r;
  scanf("%d %d %d", &v, &e, &r);
  Network network = initNetwork(v);
  push(network);
  for (int i = 0; i < e; ++i) {
    int s, t, d;
    scanf("%d %d %d", &s, &t, &d);
    addEdge(s, t, d);
  }
  pop(network);
  std::vector<int> dists = dijkstra(network, r);
  for (int i = 0; i < v; ++i) {
    if (dists[i] != INF) {
      printf("%d\\n", dists[i]);
    } else {
      puts("INF");
    }
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

const int INF = (1 << 29);

typedef struct Network {
  std::vector<std::tuple<int, int>> *edges;
  int v;
} Network;

namespace hidden {
std::stack<Network *> networks;
};
void push(Network &network) { hidden::networks.push(&network); }
void pop(Network &network) { hidden::networks.pop(); }

Network initNetwork(int v) {
  Network network;
  network.edges = new std::vector<std::tuple<int, int>>[v + 3];
  network.v = v + 3;
  return network;
}

void deleteNetwork(Network network) {
  if (network.edges == nullptr) {
    delete[] network.edges;
  }
}

void addEdge(int from, int to, int weight) {
  Network *network = hidden::networks.top();
  network->edges[from].push_back(std::tuple<int, int>(to, weight));
}

std::vector<int> dijkstra(Network network, int source) {
  std::vector<int> res(network.v, INF);
  res[source] = 0;
  std::priority_queue<std::tuple<int, int>> q;
  q.push(std::tuple<int, int>(0, source));
  while (not q.empty()) {
    int dist;
    int id;
    std::tie(dist, id) = q.top();
    q.pop();
    for (std::tuple<int, int> edge : network.edges[id]) {
      int next;
      int weight;
      std::tie(next, weight) = edge;
      int nextdist = dist + weight;
      if (nextdist >= res[next])
        continue;
      res[next] = nextdist;
      q.push(std::tuple<int, int>(nextdist, next));
    }
  }
  return res;
}

int main() {
  int v, e, r;
  scanf("%d %d %d", &v, &e, &r);
  Network network = initNetwork(v);
  push(network);
  for (int i = 0; i < e; ++i) {
    int s, t, d;
    scanf("%d %d %d", &s, &t, &d);
    addEdge(s, t, d);
  }
  pop(network);
  std::vector<int> dists = dijkstra(network, r);
  for (int i = 0; i < v; ++i) {
    if (dists[i] != INF) {
      printf("%d\\n", dists[i]);
    } else {
      puts("INF");
    }
  }
}