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

#include <iostream>
#include <queue>
#include <vector>
using namespace std;

class Node {
public:
  vector<int> to;
  vector<int> ecost;
  int cost;
  bool done;
  bool avail;
  Node() {
    done = 0;
    cost = -1;
    avail = 0;
  }
};

void find(Node *node, int nodes, int start);

int main() {
  int v, e, r;
  cin >> v >> e >> r;

  int s[e], t[e], d[e];
  int i;
  for (i = 0; i < e; i++) {
    cin >> s[i] >> t[i] >> d[i];
  }

  Node node[v];
  for (i = 0; i < e; i++) {
    node[s[i]].to.push_back(t[i]);
    node[s[i]].ecost.push_back(d[i]);
  }

  find(node, v, r);
  // cout << "finded" << endl;
  for (i = 0; i < v; i++) {
    if (node[i].cost == -1) {
      cout << "INF" << endl;
    } else {
      cout << node[i].cost << endl;
    }
  }

  return 0;
}

void find(Node *node, int nodes, int start) {
  int i, j;
  if (node[start].done)
    return;
  queue<int> list;
  node[start].cost = 0;
  node[start].done = 1;

  for (i = 0; i < node[start].to.size(); i++) {
    node[node[start].to[i]].cost = node[start].ecost[i];
    list.push(node[start].to[i]);
  }

  bool dflag;

  while (!list.empty()) {
    // cout << list.front() << endl;
    dflag = 1;
    for (j = 0; j < node[list.front()].to.size(); j++) {
      if (node[node[list.front()].to[j]].cost == -1) {
        node[node[list.front()].to[j]].cost =
            node[list.front()].cost + node[list.front()].ecost[j];
        list.push(node[list.front()].to[j]);
        dflag = 0;
        continue;
      }
      if (node[node[list.front()].to[j]].cost >
          node[list.front()].cost + node[list.front()].ecost[j]) {
        node[node[list.front()].to[j]].cost =
            node[list.front()].cost + node[list.front()].ecost[j];
        node[node[list.front()].to[j]].done = 0;
        list.push(node[list.front()].to[j]);
        dflag = 0;
        continue;
      }
    }
    if (dflag)
      node[list.front()].done = 1;
    list.pop();
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <queue>
#include <vector>
using namespace std;

class Node {
public:
  vector<int> to;
  vector<int> from;
  vector<int> ecost;
  int cost;
  bool done;
  bool avail;
  Node() {
    done = 0;
    cost = -1;
    avail = 0;
  }
};

void check(Node *node, int nodes, int start);
void find(Node *node, int nodes, int start);

int main() {
  int v, e, r;
  cin >> v >> e >> r;

  int s[e], t[e], d[e];
  int i;
  for (i = 0; i < e; i++) {
    cin >> s[i] >> t[i] >> d[i];
  }

  Node node[v];
  for (i = 0; i < e; i++) {
    node[s[i]].to.push_back(t[i]);
    node[s[i]].ecost.push_back(d[i]);
    node[t[i]].from.push_back(s[i]);
  }

  check(node, v, r);
  find(node, v, r);
  for (i = 0; i < v; i++) {
    if (node[i].cost == -1) {
      cout << "INF" << endl;
    } else {
      cout << node[i].cost << endl;
    }
  }

  // cout << "end";
  // cin >> i;
  return 0;
}

void check(Node *node, int nodes, int start) {
  int i, j;
  int loop = 0;

  node[start].avail = 1;
  for (i = 0; i < node[start].to.size(); i++) {
    node[node[start].to[i]].avail = 1;
    loop = 1;
  }

  while (loop) {
    loop = 0;
    for (i = 0; i < nodes; i++) {
      for (j = 0; j < node[i].to.size(); j++) {
        if (node[node[i].to[j]].avail == 0) {
          node[node[i].to[j]].avail = 1;
          loop = 1;
        }
      }
    }
  }
  for (i = 0; i < nodes; i++) {
    if (node[i].avail == 0) {
      node[i].done = 1;
    }
  }
}

void find(Node *node, int nodes, int start) {
  int i, j;
  if (node[start].done)
    return;
  queue<int> to;
  node[start].cost = 0;
  node[start].done = 1;

  for (i = 0; i < node[start].to.size(); i++) {
    node[node[start].to[i]].cost = node[start].ecost[i];
    to.push(node[start].to[i]);
  }

  bool dflag;

  while (1) {
    j = 1;
    for (i = 0; i < nodes; i++) {
      j *= node[i].done;
    }
    if (j)
      break;

    while (!to.empty()) {
      dflag = 1;
      for (j = 0; j < node[to.front()].to.size(); j++) {
        if (node[node[to.front()].to[j]].cost == -1) {
          node[node[to.front()].to[j]].cost =
              node[to.front()].cost + node[to.front()].ecost[j];
          to.push(node[to.front()].to[j]);
          dflag = 0;
          continue;
        }
        if (node[node[to.front()].to[j]].cost >
            node[to.front()].cost + node[to.front()].ecost[j]) {
          node[node[to.front()].to[j]].cost =
              node[to.front()].cost + node[to.front()].ecost[j];
          node[node[to.front()].to[j]].done = 0;
          to.push(node[to.front()].to[j]);
          dflag = 0;
          continue;
        }
      }
      if (dflag)
        node[to.front()].done = 1;
      to.pop();
    }
  }
}