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 <climits>
#include <algorithm>
using namespace std;
int main(){
  int n;
  int r,k,t;
  int p;
  int s,d,b;
  int ans=0;
  int box[111][111];
  cin >> n;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      box[i][j]=INT_MAX;
    }
  }
  for(int i=0;i<n;i++){
    cin >> r >> k;
    for(int j=0;j<k;j++){
      cin >> t;
      box[r-1][t-1]=1;
    }
  }
  for(int z=0;z<n;z++){
    for(int i=0;i<n;i++){
      for(int j=0;j<n;j++){
	box[i][j]=min(box[i][j],box[i][z]+box[z][j]);
      }
    }
  }
  cin >> p;
  for(int i=0;i<p;i++){
    cin >> s >> d >> b;
    ans=box[s-1][d-1]+1;
    if(ans<=b){
      cout << box[s-1][d-1]+1 << endl;
    }else{
      cout << "NA" << endl;
    }
  }
}

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);
	}
    }
  cout<<"edges_to="<<A[1].edges_to[0]<<endl;
  /*--------------------------------*/


  /*-------------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>
using namespace std;
const int maxv=1<<32;
int main()
{
	int root[101][101];
	for(int i=0;i<10;i++){
		for(int i2=0;i2<10;i2++){
			root[i][i2]=maxv;
		}
	}
	int n;
	cin>>n;
	for(int i=0;i<n;i++){
	int r,k,ch;
	cin>>r>>k;
		for(int i2=0;i2<k;i2++){
			cin>>ch;
			root[r][ch]=1;
		}
	}

	for(int k=1;k<=n;k++){
		for(int i=1;i<=n;i++){
			for(int i2=01;i2<=n;i2++){
				root[i][i2]=min(root[i][i2],root[i][k]+root[k][i2]);
			}
		}
	}

	int m;
	int s,d,v;
	cin>>m;
	for(int i=0;i<m;i++){
		cin>>s>>d>>v;
		if(root[s][d]<v){
			cout<<root[s][d]+1<<endl;
		}
		else{
			cout<<"NA"<<endl;
		}
	}
}

C++: #include <cstdio>
#include <utility>
#include <cctype>
#include <vector>
#include <climits>
#include <algorithm>
using namespace std;

#ifdef __linux
#define _U(s) s##_unlocked
#else
#define _U(s) _##s##_nolock
#define _CRT_DISABLE_PERFCRIT_LOCKS
#endif
#define gcu _U(getchar)
#define pcu _U(putchar)
#define _DEF(r, n, ...) inline r n(__VA_ARGS__) noexcept
#define _T template <typename T>
#define _HT template <typename H,typename... T>
#define _OP(t) inline operator t()
struct _in {
#ifdef _GLIBCXX_STRING
	_OP(string){string s;char c;while(isspace(c = gcu()));do{s+=c;}while(c=gcu(),c!=' '&&c!='\n'&&c!=EOF);return s;}
#define _S
#endif
	_OP(char){char c=gcu();gcu();return c;}
	_OP(double){double d; scanf("%lf",&d); gcu();return d;}
	_T _OP(T){T n{},m{1},c;while(isspace(c = gcu()));if(c=='-')m=-1,c=gcu();do{n=10*n+(c-'0'),c=gcu();}while(c>='0'&&c<='9');return m*n;}
} in;
#define _SCAN(...) _DEF(bool,scan,__VA_ARGS__)
#ifdef _S
_SCAN(string &o) {int c{gcu()};if(c==EOF)return false;else{ungetc(c,stdin);string t=move(in);o=t;return true;}}
#endif
_T _SCAN(T &o) {int c{gcu()};return c==EOF?false:(ungetc(c,stdin),o=in,true);}
_HT _SCAN(H &h,T&&... t){return scan(h)&&scan(t...);}
#define _OUT(...) _DEF(void,out,__VA_ARGS__)
#define _OUTL(...) _DEF(void,outl,__VA_ARGS__)
_OUT(bool b){pcu('0'+b);}
_OUT(const char *s){while(*s)pcu(*s++);}
_OUT(char c){pcu(c);}
#ifdef _S
_OUT(string &s){for(char c:s)pcu(c);}
#endif
_T _OUT(T n){static char b[20];char *p=b;T m=n<0?pcu('-'),-1:1;
	if(!n)*p++='0';else while(n)*p++=(char)(n%10*m+'0'),n/=10;while(p!=b)pcu(*--p);}
_OUTL(){out('\n');}
#ifdef _GLIBCXX_VECTOR
	_T _OUT(vector<T> &v){for(T &x:v)out(&x == &v[0]?"":" "),out(x);}
#endif
	_HT _OUT(H &&h, T... t){out(h);out(t...);}
	template <typename... T> _OUTL(T... t){out(t...);outl();}
	struct range{
		int e,b=0,s=1;range(int _b,int _e,int _s):e(_e),b(_b),s(_s){} range(int _b,int _e): e(_e), b(_b){} range(int _e):e(_e){}
		struct it{int v,s; it(int _v,int _s):v(_v),s(_s){} operator int()const{return v;} operator int&(){return v;}int operator*()const{return v;}
			it& operator++(){v+=s;return *this;} }; it begin(){return {b,s};} it end(){return {e,s};}};
#define times(i,n) for(int i=n;i;i--)

template <typename T>
using V = vector<T>;

int main() {
	int n {in};
	V<V<int>> d(n, V<int>(n, INT_MAX / 2));
	range rn(n);
	times (i, n) {
		int t {(int) in - 1};
		times(j, in)
			d[t][(int) in - 1] = 1;
	}
	for (int k: rn)
		for (int i: rn)
			for (int j: rn)
				d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
	times(i, in) {
		int s {(int) in - 1}, e {(int) in - 1}, v {in};
		if (d[s][e] < v)
			outl(d[s][e] + 1);
		else
			outl("NA");
	}
}