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

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

int V, E, r;
const int Inf = 10000 * 100000 + 100;
int C[100010];
int main() {
  while (cin >> V >> E >> r && V > 0) {
    vector<vector<pair<int, int>>> v(V + 10);
    for (int i = 0; i < E; i++) {
      int s, t, d;
      cin >> s >> t >> d;
      v[s].push_back(make_pair(d, t));
    }
    priority_queue<pair<int, int>> P;
    P.push(make_pair(0, r));
    for (int i = 0; i < V; i++) {
      C[i] = Inf;
    }
    C[r] = 0;
    while (!P.empty()) {
      pair<int, int> t = P.top();
      int s = t.second;
      P.pop();
      if (C[s] < t.first)
        continue;
      for (int i = 0; i < v[s].size(); i++) {
        pair<int, int> e = v[s][i];
        if (C[e.second] > C[s] + e.first) {
          C[e.second] = C[s] + e.first;
          P.push(make_pair(-C[e.second], e.second));
        }
      }
    }
    for (int i = 0; i < V; i++) {
      if (C[i] < Inf)
        cout << C[i] << endl;
      else
        cout << "INF" << endl;
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <deque>
#include <iostream>
#include <set>
#include <utility>
#include <vector>

using namespace std;
int V, E, R, S[500010], T[500010], D[500010];
int C[100010];
const int Inf = 10000 * 100000 + 100;
/*struct Node{
    int n;
    Node(int i){
        n = i;
    }
    bool operator<(Node& rhs){
        return C[this.n] < C[rhs.n];
    };
    bool operator??????(Node& rhs){
        return this.n == rhs.n;
    }
};*/
int main() {
  cin >> V >> E >> R;
  vector<pair<int, int>> tmp(0);
  vector<vector<pair<int, int>>> edges(V);
  for (int i = 0; i < E; i++) {
    int s, t, d;
    cin >> s >> t >> d;
    edges[s].push_back(make_pair(t, d));
  }
  // set<Node> Q;
  deque<int> Q;
  for (int i = 0; i < V; i++) {

    C[i] = Inf;
    // Q.insert(Node(i));
  }
  C[R] = 0;
  Q.push_back(R);
  set<int> S;
  // sort(Q.begin(), Q.end(), [](int left, int right) {return C[left] <
  // C[right];});
  while (!Q.empty()) {
    int n = Q.front();
    // cout << n << endl;
    Q.erase(Q.begin());
    S.insert(n);
    auto changed = Q.end();
    bool change = false;
    // cout << edges[n].size() << endl;
    if (!edges.empty() && !edges[n].empty()) {
      for (int i = 0; i < edges[n].size(); ++i) {
        int t = edges[n][i].first;
        int d = edges[n][i].second;
        // cout << "n=" << n << " t=" << t << " d=" << d << endl;
        if (find(Q.begin(), Q.end(), t) == Q.end() && S.count(t) == 0)
          Q.push_back(t);
        // cout << "C[t]=" << C[t] << " C[n] + d=" << C[n] + d << endl;
        if (C[t] > C[n] + d) {
          // Q.erase(itr);
          C[t] = C[n] + d;

          // cout << "change" << endl;
          // Q.insert(Node(*itr));
        }
      }
    }
    sort(Q.begin(), Q.end(),
         [](int left, int right) { return C[left] < C[right]; });

    /*for(int i = 0; i < E; ++i){
        int s = S[i], t = T[i], d = D[i];
        if(s == n){
            for(auto itr = Q.begin(), end = Q.end(); itr != end; itr++){
                if((*itr).second == t && (*itr).first > C[n] + d){
                    Q.erase(itr);
                    Q.insert(make_pair(C[n] + d, t));
                    //cout << t << " " << C[n] + d << endl;
                    break;
                }
            }
        }
    }*/
  }
  for (int i = 0; i < V; i++) {
    if (C[i] == Inf) {
      cout << "INF" << endl;
    } else {
      cout << C[i] << endl;
    }
  }
}