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

#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

const int Inf = 10000 * 100000 + 100;

int main() {
  int V, E, R;
  cin >> V >> E >> R;
  vector<int> s(E), t(E), d(E);
  vector<vector<int>> edge(V);
  for (int i = 0; i < E; i++) {
    cin >> s[i] >> t[i] >> d[i];
    edge[s[i]].push_back(i);
  }
  vector<int> cost(V, Inf);
  cost[R] = 0;

  // < ?§??????????????????¢, ?????????id >
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q;
  q.push({0, R});

  // ????????????????????????
  vector<bool> fin(V, false);

  // vector<vector<int> > edge(V);
  // for(int i = 0; i < V; i++){
  //   edge[i] = {};
  //   // ?????? i ?????????????????????????????????
  //   for(int j = 0; j < E; j++){
  //     if(s[j] == i) edge[i].push_back(j);
  //   }
  // }

  while (!q.empty()) {
    // ?¢?????????????????????? u ??¨??????
    auto npair = q.top();
    int nd = npair.first, nu = npair.second;
    // cout << nd << " : " << nu << endl;

    q.pop();
    if (fin[nu])
      continue; // ?¢??????????????????????
    fin[nu] = true;
    cost[nu] = nd;
    for (auto i : edge[nu]) {
      // u ????§??????§?¢????????????§???????????????????????????????????????
      if (!fin[t[i]]) {
        // ????????? v ??¨??????.
        int nv = t[i];
        // ????????§?????????????????????
        if (cost[nu] + d[i] < cost[nv]) {
          cost[nv] = cost[nu] + d[i];
          q.push({cost[nv], nv});
        }
      }
    }
  }

  for (int i = 0; i < V; i++) {
    if (cost[i] == Inf)
      cout << "INF";
    else
      cout << cost[i];

    cout << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <utility>
#include <vector>

using namespace std;

const int Inf = 10000 * 100000 + 100;

int main() {
  int V, E, R;
  cin >> V >> E >> R;
  vector<int> s(E), t(E), d(E);
  for (int i = 0; i < E; i++)
    cin >> s[i] >> t[i] >> d[i];
  vector<int> cost(V, Inf);
  cost[R] = 0;

  // < ?§??????????????????¢, ?????????id >
  priority_queue<pair<int, int>, vector<pair<int, int>>,
                 greater<pair<int, int>>>
      q;
  q.push({0, R});

  // ????????????????????????
  vector<bool> fin(V, false);

  vector<vector<int>> edge(V);
  for (int i = 0; i < V; i++) {
    edge[i] = {};
    for (int j = 0; j < E; j++) {
      if (s[j] == i)
        edge[i].push_back(j);
    }
  }

  while (!q.empty()) {
    // ?¢?????????????????????? u ??¨??????
    auto npair = q.top();
    int nd = npair.first, nu = npair.second;
    // cout << nd << " : " << nu << endl;

    q.pop();
    if (fin[nu])
      continue; // ?¢??????????????????????
    fin[nu] = true;
    cost[nu] = nd;
    for (auto i : edge[nu]) {
      // u ????§??????§?¢????????????§???????????????????????????????????????
      if (!fin[t[i]]) {
        // ????????? v ??¨??????.
        int nv = t[i];
        // ????????§?????????????????????
        if (cost[nu] + d[i] < cost[nv]) {
          cost[nv] = cost[nu] + d[i];
          q.push({cost[nv], nv});
        }
      }
    }
  }

  for (int i = 0; i < V; i++) {
    if (cost[i] == Inf)
      cout << "INF";
    else
      cout << cost[i];

    cout << endl;
  }
}