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 <stdio.h>
#include <cmath>
#include <algorithm>
#include <queue>

using namespace std;

struct Packet{
	Packet(){}

	Packet(int arg_ttl,int loc){
		ttl = arg_ttl;
		for(int i=0; i < 101; i++)checkTable[i] = 0;
		checkTable[loc] = 1;
		current = loc;
	}

	void set(int loc){
		checkTable[loc] = 1;
		current = loc;
	}

	int ttl,checkTable[101],current;
};

int main(){

	int n,r,k,to,table[101][101],num_of_packet,start,goal,t_limit,memo[101][101];
	Packet tmp;

	for(int i=1; i <= n; i++){
		for(int k=1; k <= n; k++){
			table[i][k] = 0;
			memo[i][k] = 0;
		}
	}

	scanf("%d",&n);

	for(int i = 0; i < n; i++){
		scanf("%d %d",&r,&k);
		for(int p=0;p < k; p++){
			scanf("%d",&to);
			table[r][to] = 1;
		}
	}

	scanf("%d",&num_of_packet);

	queue<Packet> Q;
	bool FLG;

	for(int i=0; i < num_of_packet; i++){
		scanf("%d %d %d",&start,&goal,&t_limit);

		FLG = false;

		if(t_limit <= 1){
			printf("NA\n");
			FLG = true;
		}

		if(!FLG){
			if(memo[start][goal] != 0){
				if(memo[start][goal] <= t_limit){
					printf("%d\n",memo[start][goal]);
				}else{
					printf("NA\n");
				}
				FLG = true;
			}
		}

		if(!FLG){
		if(memo[start][goal] != 0){
			if(memo[start][goal] <= t_limit){
				printf("%d\n",memo[start][goal]);
			}else{
				printf("NA\n");
			}
			continue;
		}
		}

		if(!FLG){
		for(int i = 1; i <= n; i++){
			if(i != start && table[start][i] == 1){
				if(memo[i][goal] > 0 && memo[i][goal] < t_limit){
					printf("%d\n",memo[i][goal]+1);
					memo[start][goal] = memo[i][goal]+1;
					FLG = true;
					break;
				}
				Packet p(t_limit-1,start);
				p.set(i);
				Q.push(p);
			}
		}
		}


		while(Q.empty() == false && FLG == false){
			tmp = Q.front();
			Q.pop();

			if(tmp.current == goal){
				printf("%d\n",t_limit - tmp.ttl+1);
				FLG = true;

				while(!Q.empty())Q.pop();

				memo[start][goal] = t_limit - tmp.ttl+1;

				break;
			}

			if(tmp.ttl > 1){
				for(int i=1; i <= n; i++){
					if(i != tmp.current && tmp.checkTable[i] == 0 && table[tmp.current][i] == 1){
						if(memo[i][goal] > 0 && (memo[i][goal]+1) < tmp.ttl){
							printf("%d\n",t_limit-tmp.ttl+memo[i][goal]+2);
							memo[start][goal] = t_limit-tmp.ttl+memo[i][goal]+2;
							FLG = true;
							while(!Q.empty())Q.pop();
							break;
						}

						Packet p = tmp;
						p.ttl--;
						p.set(i);
						Q.push(p);
					}
				}
			}
		}

		if(!FLG){
			printf("NA\n");
		}

	}

    return 0;
}

C++: #include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>

#define rep(i, j) FOR(i, 0, j)
#define FOR(i, j, k) for(int i = j; i < k; ++i)

using namespace std;
int a[100][100];
int main(){
  int n, r, k, t;
  scanf("%d", &n);
  rep(i, n) rep(j, n) a[i][j] = 1<<16;
  rep(i, n){
    scanf("%d%d", &r, &k);r--;
    rep(j, k){
      scanf("%d", &t);t--;
      a[r][t] = 1;
    }
  }

  // solve
  rep(i, n) rep(j, n) rep(k, n)
    a[j][k] = min(a[j][k], a[j][i] + a[i][k]);

  int p, s, d, v;
  scanf("%d", &p);
  rep(i, p){
    scanf("%d%d%d", &s, &d, &v);s--;d--;
    if(v - a[s][d] > 0) printf("%d\n", a[s][d] + 1);
    else puts("NA");
  }

  return 0;
}

C++: #include<iostream>
#include<cstring>
#include<vector>
using namespace std;
#define MAX_N 110
#define INF (1<<30)
int rooter_num;
struct Node
{
  vector<int> edges_to;
  vector<int> edges_cost;
  
  bool done;
  int cost;
};
int dijkstra(int,int);
vector<Node> A;
int main()
{
  int n,startNode,goalNode,TTL;
  int r,k,t;
  int a;
  Node node;
  /*------------input---------------*/
  cin>>rooter_num;
  for(int i=0;i<=rooter_num;i++)
    {
      A.push_back(node);
    }
  for(int i=0;i<rooter_num;i++)
    {
      cin>>r>>k;
      for(int j=0;j<k;j++)
	{
	  cin>>t;
	  A[r].edges_to.push_back(t);
	  A[r].edges_cost.push_back(1);
	}
    }
  /*--------------------------------*/


  /*-------------search-------------*/
  cin>>n;
  for(int i=0;i<n;i++)
    {
      cin>>startNode>>goalNode>>TTL;
      a=dijkstra(startNode,goalNode);
      if(TTL>a)cout<<a+1<<endl;
      else cout<<"NA"<<endl;
    }
  /*--------------------------------*/
  return 0;
}
int dijkstra(int start,int goal)
{
  int min;

  /*-----init----*/
  for(int i=0;i<=rooter_num;i++)
    {
      A[i].done=false;
      A[i].cost=INF;
    }
  A[start].cost=0;
  /*-------------*/
  
  
  while(true)
    {
      min=INF;
      for(int i=1;i<rooter_num;i++)
	{
	  if(A[i].done==false&&min>A[i].cost)
	    {
	      min=A[i].cost;
	    }
	}
      if(min==INF)break;
      /*
	確定ノードでなく,かつ現在一番累計コストの低いノードを探した
      */


      for(int currentNode=1;currentNode<rooter_num;currentNode++)
	{
	  if(A[currentNode].cost==min)
	    {
	      A[currentNode].done=true;
	      /*
		上で確定したノードを見つけ出し,マークした。
	      */

	      for(int i=0;i<A[currentNode].edges_to.size();i++)
		{
		  int nextNode=A[currentNode].edges_to[i];
		  
		  if(A[nextNode].cost>A[currentNode].edges_cost[i]+A[currentNode].cost)
		    {
		      A[nextNode].cost=A[currentNode].edges_cost[i]+A[currentNode].cost;
		    }  
		}
	      /*
		確定したノードから飛び,現在までの最小累計コストより
		低くなる場合は最小累計コストを更新する
	      */
	    }
	  
	}
      
    }
  
  return A[goal].cost;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<queue>
#include<set>
#include<functional>
#include<unordered_map>
using namespace std;

vector<int>rinsetu[100]{};
unordered_map<int, int>taiou;
int main() {
	int a;
	cin >> a;
	for (int b = 0; b < a; b++) {
		int c, d; scanf("%d%d", &c, &d);
		taiou[c] = b;
		for (int e = 0; e < d; e++) {
			int f; scanf("%d", &f);
			rinsetu[b].push_back(f);
		}
	}
	int b; cin >> b;
	for (int c = 0; c < b; c++) {
		int g, h, i; scanf("%d%d%d", &g, &h, &i);
		int e[100]{};
		memset(e, -1, sizeof(e));
		queue<int>Q;
		e[taiou[g]] = 1;
		Q.push(g);
		while (Q.size()) {
			int o = Q.front(); Q.pop();
			o = taiou[o];
			if (e[o] > i)break;
			for (int p : rinsetu[o]) {
				if (e[taiou[p]] == -1) {
				 e[taiou[p]] = e[o] + 1; Q.push(p);
				}
			}
		}
		if (e[taiou[h]] == -1||e[taiou[h]]>i)puts("NA");
		else cout << e[taiou[h]] << endl;
	}
}