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 <queue>
#include <algorithm>
#include <vector>

#define rep(i,n) for(int (i)=0;(i)<(n);(i)++)
using namespace std;

typedef vector<int> vi;

const int inf=1<<29;

int graph[110][110];

int main(void){
	rep(i,110)rep(j,110) graph[i][j]=inf;
	int n;
	cin >> n;
	rep(i,n){
		int f;
		cin >> f; f--;
		int num;
		cin >> num;
		rep(j,num){
			int t;
			cin >> t; t--;
			graph[f][t]=1;
		}
	}
	rep(k,n)rep(i,n)rep(j,n) graph[i][j]=min( graph[i][j] , graph[i][k]+ graph[k][j]);
	int p;
	cin >> p;
	rep(i,p){
		int s,t,v;
		cin >> s >> t >> v;
		int res=graph[s-1][t-1]+1;
		if(v>=res)
			cout << res <<endl;
		else
			cout << "NA" << endl;
	}
	return 0;
}

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


int net[110][110];

typedef pair<int,int> BVB;


int ans(int,int,int);

void debug();

int main(){

  for(int k=0;k<110;k++)for(int l=0;l<110;l++)net[k][l]=-1; //Syokika

  int n,i,j,p,num,n1,th;

  cin >> n;

  for(i=0;i<n;i++){ //net's info
    
    cin >> num >> n1;



    for(int k=0;k<110;k++)net[num][k]=0;    //siyoukanou

    for(j=0;j<n1;j++){
      cin >> th;
      net[num][th]=1;
    }

  }//net's info

  int pkn;

  cin >> pkn;

  int st,ed,c,answer;

  // debug();



  for(i=0;i<pkn;i++){//pack's info
    cin >> st >> ed >> c;


    answer = ans(st,ed,c);

    if(answer != -2)cout << answer << endl;
    else cout << "NA" << endl;

  }//pack's info



}



//L[(ÊÌzñÅà¢¢¯Ç)ÉÇñÇñüêÄ¢«TTLªO¢ÉÈéOÉÚInÉBÅ«é©Ç¤©ð²×é
//[^ÍÚInÉBµ½n_ÌTTL©çvZµÄ¾·
int ans(int st,int ed,int c){
  queue<BVB> Nari;
  int i,j,k,l,cal=0;
  BVB next,fr;

  fr.first=st;
  fr.second=c;
  Nari.push(fr);
  /*
  cout << "FIRST next.second = " << c << endl;  
  cout << "FIRST cal = " << cal << endl;
  */  

  if(st == ed && c != 0)return 1;


  while(1){
    next=Nari.front();
    Nari.pop();
    next.second=next.second-1;

    if(fr.second == (next.second+1) && next.second == 0 && next.first != ed)return -2;


    if(next.second < 0)break; //I¹ð
    //cout << "next.second = " << next.second << endl; 
    
    for(i=1;i<=110;i++){//¢¯éêðL[Éüêé
  
    
      
      if(net[next.first][i] == 1 && i == ed ){ //ÚInB
	/*	
	cout << "GOOOOOOOOOL!!! i = " << i <<endl;
	cout << "             cal = " << cal << endl;
	cout << "     next.second = " << next.second << endl; 
	*/

	return (fr.second - next.second)+1;
	
      }
      
      if(net[next.first][i] == 1)Nari.push(BVB(i,next.second));
      
      
    }
      
    
  }

  return -2;
  
  
  
}










void debug(){
  for(int i=1;i<=10;i++){
    if(i==1){
      cout << "   ";
      for(int k=1;k<=10;k++){
	printf("%3d",k);
      }
      cout << endl;
      for(int k=1;k<=10;k++)printf("___");
      cout << endl;
    }

    for(int j=1;j<=10;j++){
      if(j == 1)cout << i << " |";
      printf("%3d",net[i][j]);
    }
    cout << endl;

  }



}

C++: #include<iostream>
#include<queue>
#include<map>
#include<cstring>
using namespace std;

typedef pair<int,int> P;
int bfs(int s,int d,int v);

int k[111];
int t[111][111];
bool flag[111];

int main(){
  int r,n,p;
  cin >> n;
  for(int i=0;i<n;i++){
    cin >> r;
    cin >> k[r];
    for(int j=0;j<k[r];j++){
      cin >> t[r][j];
    }
  }
  cin >> p;
  for(int i=0;i<p;i++){
    int s,d,v;
    memset(flag,0,sizeof(flag));
    cin >> s >> d >> v;
    int ans = bfs(s,d,v);
    if(ans == -1) cout << "NA" << endl;
    else cout << ans << endl;
  }
}

int bfs(int s,int d,int v){
  queue<P> que;
  que.push(P(s,0));
  while(!que.empty()){
    P p = que.front();
    que.pop();
    int now = p.first;
    int cou = p.second;
    cou++;
    if(now == d) return cou;
    if(flag[now] == false && v-cou > 0){
      flag[now] = true;
      for(int i=0;i<k[now];i++){
	que.push(P(t[now][i],cou));
      }
    }
  }
  return -1;
}

C++: #include<iostream>
using namespace std;
const int maxv=100;
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(v-root[s][d]>0){
			cout<<root[s][d]+1<<endl;
		}
		else{
			cout<<"NA"<<endl;
		}
	}
}

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

const int INF = 100;
int d[100][100];
void init()
{
	for(int i=0;i<100;i++){
		for(int j=0;j<100;j++){
			if(i == j){
				d[i][j] = 0;
			}else{
				d[i][j] = INF;
			}
		}
	}
}

int main()
{
	int n;
	cin >> n;
	init();
	for(int i=0;i<n;i++){
		int a,m;
		cin >> a >> m;
		for(int j=0;j<m;j++){
			int b;
			cin >> b;
			d[a-1][b-1] = 1;
		}
	}
	for(int k=0;k<n;k++){
		for(int i=0;i<n;i++){
			for(int j=0;j<n;j++){
				int dis = d[i][k]+d[k][j];
				if(dis < d[i][j]){
					d[i][j] = dis;
				}	
			}
		}
	}
	int p;
	cin >> p;
	for(int j=0;j<p;j++){
		int a,b,pket;
		cin >> a >> b >> pket;
		if(d[a-1][b-1]+1 <= pket)
			cout << d[a-1][b-1]+1  << endl;
		else
			cout << "NA" << endl;
	}
	return 0;
}