Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)
const INF = 1<<31

func main() {
	scanner.Scan()
	n, _ := strconv.Atoi(scanner.Text())
	arr := make([][]int, n)
	for i := range arr {
		arr[i] = make([]int, n)
	}
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			arr[i][j] = INF
		}
	}
	for i := 0; i < n; i++ {
		scanner.Scan()
		buf := strings.Split(scanner.Text(), " ")
		input := make([]int, len(buf))
		for j := range input {
			input[j], _ = strconv.Atoi(buf[j])
		}
		x, k := input[0]-1, input[1]
		for j := 0; j < k; j++ {
			y := input[j+2]-1
			arr[x][y] = 1
		}
	}
	for k := 0; k < n; k++ {
		arr[k][k] = 0
		for i := 0; i < n; i++ {
			if arr[i][k] >= INF {
				continue
			}
			for j := 0; j < n; j++ {
				if arr[k][j] >= INF {
					continue
				}
				arr[i][j] = min(arr[i][j], arr[i][k]+arr[k][j])
			}
		}
	}
	scanner.Scan()
	p, _ := strconv.Atoi(scanner.Text())
	for i := 0; i < p; i++ {
		scanner.Scan()
		buf := strings.Split(scanner.Text(), " ")
		input := make([]int, len(buf))
		for j := range input {
			input[j], _ = strconv.Atoi(buf[j])
		}
		s, d, v := input[0]-1, input[1]-1, input[2]
		if arr[s][d] < v {
			fmt.Println(arr[s][d]+1)
		} else {
			fmt.Println("NA")
		}
	}
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

C++: #include <iostream>
#include <vector>
#include <algorithm>

using namespace std;
static const int INF = 1e9;

void solve()
{
	int n;
	cin >> n;
	vector< vector<int> > d(n + 1, vector<int>(n + 1));
	for (int i = 0; i < n + 1; ++i)
	{
		for (int j = 0; j < n + 1; ++j)
		{
			d[i][j] = INF;
		}
	}
	for (int i = 0; i < n; ++i)
	{
		int r, k;
		cin >> r >> k;
		for (int j = 0; j < k; ++j)
		{
			int t;
			cin >> t;
			d[r][t] = 1;
		}
	}
	for (int k = 1; k < n + 1; ++k)
	{
		for (int i = 1; i < n + 1; ++i)
		{
			for (int j = 1; j < n + 1; ++j)
			{
				d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
			}
		}
	}
	int p;
	cin >> p;
	for (int i = 0; i < p; ++i)
	{
		int start, dist, v;
		cin >> start >> dist >> v;
		if (d[start][dist] + 1 <= v)
		{
			cout << d[start][dist] + 1 << endl;
		}
		else
		{
			cout << "NA" << endl;
		}
	}
}

int main()
{
	solve();
	return(0);
}

C++: #include <iostream>
#include <iomanip>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>
#include <sstream>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <numeric>
#include <cmath>
#include <climits>
#include <limits>
#include <cfloat>
#include <fstream>
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;
}

C++: #include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<map>
#include<set>
#include<queue>
#include<cstdio>
#include<climits>
#include<cmath>
#include<cstring>
#include<string>
#include<sstream>

#define f first
#define s second
#define mp make_pair

#define REP(i,n) for(int i=0; i<(int)(n); i++)
#define FOR(i,c) for(__typeof((c).begin()) i=(c).begin(); i!=(c).end(); i++)
#define ALL(c) (c).begin(), (c).end()

using namespace std;

typedef unsigned int uint;
typedef long long ll;

#define MAX 100000

int main(){
  int n;
  int dist[100][100];
  scanf("%d",&n);
  REP(i,n){
    int k, m;
    scanf("%d%d",&k,&m);
    REP(j,n) dist[k-1][j] = MAX;
    dist[k-1][k-1] = 0;
    REP(j,m){
      int d;
      scanf("%d",&d);
      dist[k-1][d-1] = 1;
    }
  }

  REP(k,n) REP(i,n) REP(j,n)
    dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);

  int m;
  scanf("%d",&m);
  REP(i,m){
    int a, b, c;
    scanf("%d%d%d",&a,&b,&c);
    if(dist[a-1][b-1] == MAX || dist[a-1][b-1] + 1 > c)
      puts("NA");
    else
      printf("%d\n",dist[a-1][b-1]+1);
  }
  return 0;
}

C++: #include <iostream>
#include <cstdio>
#include <vector>
#include <queue>
using namespace std;

#define loop(i, n) for (int i = 0; i < n; i++)
#define pb push_back 

int bfs(int s, int t, int c);

vector<vector<int> > g(100001);

int main()
{
    int n;
    cin >> n;
    loop(i, n) {
        int r, k, t;
        cin >> r >> k;
        loop(j, k) {
            cin >> t;
            g[r].pb(t);
        }
    }
    int p;
    cin >> p;
    loop (i, p) {
        int s, d, v;
        cin >> s >> d >> v;
        int ans = bfs(s, d, v);
        if (ans != -1) {
            cout << v-ans << endl;
        } else {
            cout << "NA" << endl;
        }
    }
    return 0;
}

int bfs(int s, int t, int c)
{
    vector<bool> check(100001, false);
    queue<int> fq, nq, sq;
    int cnt = 0;
    fq.push(s);
    if (c == 0) {
        return -1;
    }
    while (1) {
        c--;
        while (!fq.empty()) {
            int pos = fq.front(); fq.pop();
            if (check[pos]) {
                continue;
            } else {
                check[pos] = true;
            }
            //cout << pos << endl;
            if (pos == t) {
                return c;
            }
            for (int i: g[pos]) {
                nq.push(i);
            }
        }
        if (c == 0 || nq.empty()) {
            return -1;
        }
        fq = nq;
        nq = sq;
        //cout << c << "====" << endl;
    }
}