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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define M 100000000

// ツパツケツッツトツつェツ経ツ由ツつキツづゥツδ仰ーツタツづ個催短ツ仰猟猟」ツづーツダツイツクツスツトツδ可づ仰づ淞づゥツ。
// ttl >= ツ個仰嘉環づ按づァ ツ個仰嘉環づーツ陛板つキ
// ttl < ツ個仰嘉環づ按づァ -1 ツづーツ陛板つキ
int dijkstra(int from, int to, vector<vector<int>> t) {
  int size = t.size();
  vector<int> d(size, M);
  vector<bool> v(size, false);
  int i, p;

  d[from] = 1;

  int mind = M;

  do {
    mind = M;
    for (i = 1; i < size; ++i) {
      if (!v[i] && mind > d[i]) {
        mind = d[i];
        p = i;
      } // end if
    }   // end for

    v[p] = true;

    for (i = 1; i < size; ++i) {
      if (d[p] + t[p][i] < d[i]) {
        d[i] = d[p] + t[p][i];
      } // end if
    }   // end for
  } while (mind != M);

  return d[to];
}

int main() {
  //	cut here before submit
  //	freopen ("testcase.packet", "r", stdin );
  string str = "";
  string res = "";
  int n;

  getline(cin, str);
  stringstream ssn(str);
  ssn >> n;

  vector<vector<int>> t(n + 1, vector<int>(n + 1, M));
  vector<vector<int>> memo(n + 1, vector<int>(n + 1, M));
  int i, j;
  for (i = 0; i < n; ++i) {
    int from, num, to;
    getline(cin, str);
    stringstream ss(str);

    ss >> from >> num;
    for (j = 0; j < num; ++j) {
      ss >> to;
      t[from][to] = 1;
    } // end for
  }   // end for

  for (i = 1; i <= n; ++i) {
    for (j = 1; j <= n; ++j) {
      if (i != j) {
        memo[i][j] = dijkstra(i, j, t);
      } // end if
    }   // end for
  }     // end for

  int p;
  getline(cin, str);
  stringstream ssp(str);
  ssp >> p;

  for (i = 0; i < p; ++i) {
    int from, to, ttl;
    getline(cin, str);
    stringstream ss(str);
    ss >> from >> to >> ttl;

    int res = -1;
    if (memo[from][to] <= ttl) {
      res = memo[from][to];
    } else {
      res = -1;
    } // end if

    if (res == -1) {
      cout << "NA" << endl;
    } else {
      cout << res << endl;
    } // end if
  }   // end for

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
#define M 1000
typedef pair<pair<int, int>, int> P;

void disp_p(P p) {
  int from = p.first.first;
  int to = p.first.second;
  int ttl = p.second;

  cout << "from: " << from << " to: " << to << " ttl: " << ttl << endl;
}

// ツパツケツッツトツつェツ経ツ由ツつキツづゥツδ仰ーツタツづ個催短ツ仰猟猟」ツづーツダツイツクツスツトツδ可づ仰づ淞づゥツ。
// ttl >= ツ個仰嘉環づ按づァ ツ個仰嘉環づーツ陛板つキ
// ttl < ツ個仰嘉環づ按づァ -1 ツづーツ陛板つキ
int dijkstra(P packet, vector<vector<int>> t) {
  int from = packet.first.first;
  int to = packet.first.second;
  int ttl = packet.second;
  int res = 0;
  int size = t.size();
  vector<int> d(size, M);
  vector<bool> v(size, false);
  int i, p;

  v[0] = true;
  d[from] = 1;

  // for debug
  //	disp_p (packet );

  do {
    int mind = M;
    for (i = 1; i < size; ++i) {
      if (!v[i] && mind > d[i]) {
        mind = d[i];
        p = i;
      } // end if
    }   // end for

    v[p] = true;

    for (i = 1; i < size; ++i) {
      if (d[p] + t[p][i] < d[i]) {
        d[i] = d[p] + t[p][i];
      } // end if
    }   // end for
  } while ((int)count(v.begin(), v.end(), true) != size);

  // for debug
  /*
          for (i = 0; i < size; ++i ){
                  cout << d[i] << ' ';
          } // end for
          cout << endl;
  */

  if (d[to] <= ttl) {
    res = d[to];
  } else {
    res = -1;
  } // end if

  return res;
}

template <class T> void disp_t(vector<T> t) {
  int size = t.size();
  int i, j;

  for (i = 0; i < size; ++i) {
    for (j = 0; j < size; ++j) {
      if (t[i][j] == M) {
        cout << " 0";
      } else {
        cout << setw(2) << t[i][j];
      } // end if
    }   // end for
    cout << endl;
  } // end for
}

int main() {
  //	cut here before submit
  //	freopen ("testcase.packet", "r", stdin );
  string str = "";
  string res = "";
  int n;

  getline(cin, str);
  stringstream ssn(str);
  ssn >> n;

  vector<vector<int>> t(n + 1, vector<int>(n + 1, M));

  int i, j;
  for (i = 0; i < n; ++i) {
    int from, num, to;
    getline(cin, str);
    stringstream ss(str);

    ss >> from >> num;
    for (j = 0; j < num; ++j) {
      ss >> to;
      t[from][to] = 1;
    } // end for
  }   // end for

  // for debug
  //	disp_t (t );

  int p;
  getline(cin, str);
  stringstream ssp(str);
  ssp >> p;

  for (i = 0; i < p; ++i) {
    P packet;
    int from, to, ttl;
    getline(cin, str);
    stringstream ss(str);
    ss >> from >> to >> ttl;

    packet.first.first = from;
    packet.first.second = to;
    packet.second = ttl;

    int res = dijkstra(packet, t);

    if (res == -1) {
      cout << "NA" << endl;
    } else {
      cout << res << endl;
    } // end if
  }   // end for

  return 0;
}