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

#include "bits/stdc++.h"
using namespace std;

typedef vector<vector<pair<vector<int>, long long int>>> type;

int V, E, K;
type time(const type &lts, const type &rts, const bool flag) {
  type squs(V, vector<pair<vector<int>, long long int>>(V));
  for (int i = 0; i < V; ++i) {
    for (int j = 0; j < V; ++j) {
      if (i == j)
        squs[i][j] = make_pair(vector<int>(), 0ll);
      else
        squs[i][j] = make_pair(vector<int>(), static_cast<long long>(-1e17));
    }
  }
  vector<vector<int>> ps(V, vector<int>(V));
  for (int k = 0; k < V; ++k) {
    for (int i = 0; i < V; ++i) {
      for (int j = 0; j < V; ++j) {
        if (squs[i][j].second < lts[i][k].second + rts[k][j].second) {
          squs[i][j].second = lts[i][k].second + rts[k][j].second;
          if (flag) {
            ps[i][j] = k;
          }
        }
      }
    }
  }

  if (flag) {
    for (int i = 0; i < V; ++i) {
      for (int j = 0; j < V; ++j) {
        const int k = ps[i][j];
        squs[i][j].first.clear();
        squs[i][j].first.insert(squs[i][j].first.end(), lts[i][k].first.begin(),
                                lts[i][k].first.end());
        squs[i][j].first.insert(squs[i][j].first.end(), rts[k][j].first.begin(),
                                rts[k][j].first.end());
      }
    }
  }
  return squs;
}
type square(const type &ts, bool flag) { return time(ts, ts, flag); }

type power(const type &ts, const long long int num, const bool flag) {
  assert(num);
  if (num == 1)
    return ts;
  else {
    type pot(square(power(ts, num / 2, flag), flag));
    if (num % 2) {
      pot = time(pot, ts, flag);
    }
    return pot;
  }
}
vector<int> ans;
bool check(const type &ts, const long long int run, const int point,
           const bool flag) {

  type t(power(ts, run, flag && run <= 100));
  long long int amax = 0;
  int ai = -1;
  int aj = -1;
  for (int i = 0; i < V; ++i) {
    for (int j = 0; j < V; ++j) {
      if (amax < t[i][j].second) {
        amax = t[i][j].second;
        ai = i;
        aj = j;
      }
    }
  }
  if (run <= 100 && flag) {
    ans.clear();
    ans.push_back(ai);
    ans.insert(ans.end(), t[ai][aj].first.begin(), t[ai][aj].first.end());
  }
  if (amax >= point)
    return true;
  else
    return false;
}

int main() {
  cin >> V >> E >> K;
  type times(
      V, vector<pair<vector<int>, long long int>>(
             V, make_pair(vector<int>(), static_cast<long long int>(-1e18))));
  for (int i = 0; i < E; ++i) {
    long long int a, b, c;
    cin >> a >> b >> c;

    times[a][b].second = max(times[a][b].second, c);
    times[a][b].first = vector<int>(1, b);
  }
  for (int i = 0; i < V; ++i) {
    times[i][i] = make_pair(vector<int>(1, i), 0);
  }
  long long int amin = 0;
  long long int amax = 1e6 + 10;
  while (amin + 1 != amax) {
    long long int amid((amin + amax) / 2);
    if (check(times, amid, K, false)) {
      amax = amid;
    } else {
      amin = amid;
    }
  }
  if (amax > 1e6 + 5) {
    cout << -1 << endl;
  } else {
    check(times, amax, K, true);
    cout << amax << endl;
    if (amax <= 100) {
      for (int i = 0; i < ans.size(); ++i) {
        cout << ans[i];
        if (i != ans.size() - 1)
          cout << " ";
        else
          cout << 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>>;
const ld eps = 1e-9;

//// < "D:\\D_Download\\Visual Studio
///2015\\Projects\\programing_contest_c++\\Debug\\a.txt" > "D:\\D_Download\\Visual
///Studio 2015\\Projects\\programing_contest_c++\\Debug\\b.answer"

typedef long long int Weight;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};

bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
             e.src != f.src ? e.src < f.src
                            : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<vector<pair<vector<int>, long long int>>> type;
int V, E, K;
type time(const type &lts, const type &rts, const bool flag) {
  type squs(V, vector<pair<vector<int>, long long int>>(V));
  for (int i = 0; i < V; ++i) {
    for (int j = 0; j < V; ++j) {
      if (i == j)
        squs[i][j] = make_pair(vector<int>(), 0ll);
      else
        squs[i][j] = make_pair(vector<int>(), static_cast<long long>(-1e17));
    }
  }
  for (int k = 0; k < V; ++k) {
    for (int i = 0; i < V; ++i) {
      for (int j = 0; j < V; ++j) {
        if (squs[i][j].second < lts[i][k].second + rts[k][j].second) {
          squs[i][j].second = lts[i][k].second + rts[k][j].second;
          if (flag) {
            squs[i][j].first.clear();
            squs[i][j].first.insert(squs[i][j].first.end(),
                                    lts[i][k].first.begin(),
                                    lts[i][k].first.end());
            squs[i][j].first.insert(squs[i][j].first.end(),
                                    rts[k][j].first.begin(),
                                    rts[k][j].first.end());
          }
        }
      }
    }
  }
  return squs;
}
type square(const type &ts, bool flag) { return time(ts, ts, flag); }

type power(const type &ts, const long long int num, const bool flag) {
  assert(num);
  if (num == 1)
    return ts;
  else {
    type pot(square(power(ts, num / 2, flag), flag));
    if (num % 2) {
      pot = time(pot, ts, flag);
    }
    return pot;
  }
}
vector<int> ans;
bool check(const type &ts, const long long int run, const int point,
           const bool flag) {

  type t(power(ts, run, flag && run <= 100));
  long long int amax = 0;
  int ai = -1;
  int aj = -1;
  for (int i = 0; i < V; ++i) {
    for (int j = 0; j < V; ++j) {
      if (amax < t[i][j].second) {
        amax = t[i][j].second;
        ai = i;
        aj = j;
      }
    }
  }
  if (run <= 100 && flag) {
    ans.clear();
    ans.push_back(ai);
    ans.insert(ans.end(), t[ai][aj].first.begin(), t[ai][aj].first.end());
  }
  if (amax >= point)
    return true;
  else
    return false;
}

int main() {
  cin >> V >> E >> K;
  type times(
      V, vector<pair<vector<int>, long long int>>(
             V, make_pair(vector<int>(), static_cast<long long int>(-1e18))));
  for (int i = 0; i < E; ++i) {
    long long int a, b, c;
    cin >> a >> b >> c;

    times[a][b].second = max(times[a][b].second, c);
    times[a][b].first = vector<int>(1, b);
  }
  for (int i = 0; i < V; ++i) {
    times[i][i] = make_pair(vector<int>(1, i), 0);
  }
  long long int amin = 0;
  long long int amax = 1e6 + 10;
  while (amin + 1 != amax) {
    long long int amid((amin + amax) / 2);
    if (check(times, amid, K, false)) {
      amax = amid;
    } else {
      amin = amid;
    }
  }
  if (amax > 1e6 + 5) {
    cout << -1 << endl;
  } else {
    check(times, amax, K, true);
    cout << amax << endl;
    if (amax <= 100) {
      for (int i = 0; i < ans.size(); ++i) {
        cout << ans[i];
        if (i != ans.size() - 1)
          cout << " ";
        else
          cout << endl;
      }
    }
  }

  return 0;
}