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

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;

struct howto {
  int wit;
  vector<int> needs;
  howto(const int g) : wit(g), needs() {}
};
struct aa {
  int wit;
  int need;
};
class Compare {
public:
  // aa?????????????????¶
  bool operator()(const aa &l, const aa &r) { return l.need > r.need; }
};
int main() {
  int N, E, T;
  cin >> N >> E >> T;
  T--;
  vector<int> costs(N);
  for (int i = 0; i < N; ++i) {
    int w;
    cin >> w;
    if (w)
      costs[i] = 1;
    else
      costs[i] = 1e8;
  }
  vector<howto> hows;
  vector<bool> howuses(E);
  for (int i = 0; i < E; ++i) {
    int g, c;
    cin >> g >> c;
    g--;
    hows.push_back(howto(g));
    for (int j = 0; j < c; ++j) {
      int s;
      cin >> s;
      s--;
      hows[i].needs.push_back(s);
    }
  }
  priority_queue<aa, vector<aa>, Compare> que;
  for (int h = 0; h < hows.size(); ++h) {
    vector<int> neednums;
    for (auto n : hows[h].needs) {
      neednums.push_back(costs[n]);
    }
    sort(neednums.begin(), neednums.end());
    int amax = 0;
    for (int i = 0; i < neednums.size(); ++i) {
      amax = max(int(neednums[i] + neednums.size() - i - 1), amax);
    }
    if (amax < costs[hows[h].wit])
      que.push(aa{hows[h].wit, amax});
  }
  int ans = 1e8;
  while (!que.empty()) {
    aa atop(que.top());
    que.pop();
    if (atop.need < costs[atop.wit]) {
      costs[atop.wit] = atop.need;
      for (int h = 0; h < hows.size(); ++h) {
        vector<int> neednums;
        for (auto n : hows[h].needs) {
          neednums.push_back(costs[n]);
        }
        sort(neednums.begin(), neednums.end());
        int amax = 0;
        for (int i = 0; i < neednums.size(); ++i) {
          amax = max(int(neednums[i] + neednums.size() - i - 1), amax);
        }
        if (amax < costs[hows[h].wit])
          que.push(aa{hows[h].wit, amax});
      }
    }
  }
  if (costs[T] > 1e6)
    costs[T] = -1;
  cout << costs[T] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
#include <unordered_map>
#include <unordered_set>
#pragma warning(disable : 4996)
using namespace std;
using ld = long double;
template <class T> using Table = vector<vector<T>>;

struct howto {
  int wit;
  vector<int> needs;
  howto(const int g) : wit(g), needs() {}
};
struct aa {
  int wit;
  int need;
};
class Compare {
public:
  // aa?????????????????¶
  bool operator()(const aa &l, const aa &r) { return l.need > r.need; }
};
int main() {
  int N, E, T;
  cin >> N >> E >> T;
  T--;
  vector<int> costs(N);
  for (int i = 0; i < N; ++i) {
    int w;
    cin >> w;
    if (w)
      costs[i] = 1;
    else
      costs[i] = 1e8;
  }
  vector<howto> hows;
  vector<bool> howuses(E);
  for (int i = 0; i < E; ++i) {
    int g, c;
    cin >> g >> c;
    g--;
    hows.push_back(howto(g));
    for (int j = 0; j < c; ++j) {
      int s;
      cin >> s;
      s--;
      hows[i].needs.push_back(s);
    }
  }
  priority_queue<aa, vector<aa>, Compare> que;
  for (int h = 0; h < hows.size(); ++h) {
    vector<int> neednums;
    for (auto n : hows[h].needs) {
      neednums.push_back(costs[n]);
    }
    sort(neednums.begin(), neednums.end());
    int amax = 0;
    for (int i = 0; i < neednums.size(); ++i) {
      amax = max(int(neednums[i] + neednums.size() - i - 1), amax);
    }
    if (amax < costs[hows[h].wit])
      que.push(aa{hows[h].wit, amax});
  }
  int ans = 1e8;
  while (!que.empty()) {
    aa atop(que.top());
    que.pop();
    if (atop.need < costs[atop.wit]) {
      costs[atop.wit] = atop.need;
    }
    for (int h = 0; h < hows.size(); ++h) {
      vector<int> neednums;
      for (auto n : hows[h].needs) {
        neednums.push_back(costs[n]);
      }
      sort(neednums.begin(), neednums.end());
      int amax = 0;
      for (int i = 0; i < neednums.size(); ++i) {
        amax = max(int(neednums[i] + neednums.size() - i - 1), amax);
      }
      if (amax < costs[hows[h].wit])
        que.push(aa{hows[h].wit, amax});
    }
  }
  if (costs[T] > 1e6)
    costs[T] = -1;
  cout << costs[T] << endl;
  return 0;
}