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

#if 0
なんでTLEするんやぁ…
#endif

#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;

int V, E, R;
vector<pair<int, int>> ST[500010];
int C[100010];
bool Q[100010];
const int Inf = 10000 * 100000 + 100;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>>
    P;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cin >> V >> E >> R;
  for (int i = 0; i < E; ++i) {
    int s, t, d;
    cin >> s >> t >> d;
    ST[s].push_back(make_pair(t, d));
  }
  for (int i = 0; i < V; i++) {
    C[i] = (i == R ? 0 : Inf);
    Q[i] = true;
    P.push(make_pair(C[i], i));
  }
  int cnt = 0;
  while (cnt < V) {
    int d = P.top().first, u = P.top().second;
    P.pop();
    if (Q[u]) {
      Q[u] = false;
      cnt++;
      for (auto it = ST[u].begin(); it != ST[u].end(); ++it) {
        int v = (*it).first;
        int d = (*it).second;
        if (Q[v]) {
          C[v] = min(C[v], C[u] + d);
          P.push(make_pair(C[v], v));
        }
      }
    }
  }
  for (int i = 0; i < V; i++)
    cout << (C[i] == Inf ? "INF" : to_string(C[i])) << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

int C[100010];

int main() { // ダイクストラ(密)
  cin.tie(0);
  ios::sync_with_stdio(false);
  int vn, en, r;
  cin >> vn >> en >> r;
  int **V = new int *[vn];
  for (int i = 0; i < vn; i++) {
    V[i] = new int[vn];
  }
  for (int i = 0; i < vn; i++) {
    for (int j = 0; j < vn; j++)
      V[i][j] = 1000000000;
  }
  for (int i = 0; i < en; i++) {
    int s, t, d;
    cin >> s >> t >> d;
    V[s][t] = d;
  }
  for (int g = 0; g < vn; g++) {
    for (int i = 0; i < vn; i++)
      C[i] = V[r][i];
    set<int> U;
    for (int i = 0; i < vn; i++)
      if (i != r)
        U.insert(i);
    int w = r;
    int cmin;
    while (!U.empty() && w != g) {
      cmin = 1000000000;
      for (auto it = U.begin(); it != U.end(); ++it) {
        if (C[*it] < cmin) {
          cmin = C[*it];
          w = *it;
        }
      }
      if (cmin == 1000000000)
        break;
      U.erase(w);
      for (auto it = U.begin(); it != U.end(); ++it)
        C[*it] = min(C[*it], C[w] + V[w][*it]);
    }
    if (g == r)
      cout << 0 << endl;
    else if (cmin == 1000000000)
      cout << "INF" << endl;
    else
      cout << C[g] << endl;
  }
}

/*int main() { //ダイクストラ(疎)
  cin.tie(0);
  ios::sync_with_stdio(false);
  int vn, en, r; cin >> vn >> en >> r;
  int **V = new int*[vn];
  for (int i = 0; i < vn; i++) {
      V[i] = new int[vn];
  }
  for (int i = 0; i < vn; i++) {
    for (int j = 0; j < vn; j++)
      V[i][j] = 1000000000;
  }
  for (int i = 0; i < en; i++) {
    int s, t, d; cin >> s >> t >> d;
    V[s][t] = d;
  }
  for (int g = 0; g < vn; g++) {
    for (int i = 0; i < vn; i++)
      C[i] = V[r][i];
    set<int> U;
        auto c = [](int l, int r) {
      return !(!(C[l] > C[r]) && !(l > r));
    };
    priority_queue<int, vector<int>, decltype(c)> S(c);
    for (int i = 0; i < vn; i++)
      if (i != r) {
        U.insert(i); S.push(i);
      }
    int w = r;
    int cmin;
    while (!S.empty() && w != g) {
      w = S.top();
      cmin = C[w];
      if (cmin == 1000000000)
        break;
      U.erase(w); S.pop();
      for (auto it = U.begin(); it != U.end(); ++it)
        C[*it] = min(C[*it], C[w]+V[w][*it]);
    }
    if (g == r)
      cout << 0 << endl;
    else if (cmin == 1000000000)
      cout << "INF" << endl;
    else
      cout << C[g] << endl;
  }
}*/