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<string>
#include<algorithm>
#include<map>
using namespace std;
int main() {
	vector<int> a[100];
	int b[100];
	bool l[100];
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		int r, k, t;
		cin >> r >> k;
		r--;
		for (int j = 0; j < k; j++) {
			cin >> t;
			t--;
			a[r].push_back(t);
		}
		sort(a[r].begin(), a[r].end());
	}
	int p;
	cin >> p;
	for (int i = 0; i < p; i++) {
		int s, d, v;
		cin >> s >> d >> v;
		s--;
		d--;
		fill(b, b + n, 100000);
		b[s] = 0;
		fill(l, l + n, false);
		l[s] = true;
		int sum = 0;
		multimap<int, int> m;
		for (int j = 0; j < a[s].size(); j++) {
			m.insert(map<int, int>::value_type(1, a[s][j]));
		}
		multimap<int, int>::iterator it = m.begin();
		while (!l[d]) {
			it = m.begin();
			sum=(*it).first+1;
			if (sum > v) break;
			int e = (*it).second;
			m.erase(it);
			for (int j = 0; j < a[e].size(); j++) {
				l[a[e][j]] = true;
				b[a[e][j]] = min(b[a[e][j]],sum);
			}
		}
		if (l[d]) {
			cout << b[d] << endl;
		}
		else {
			cout << "NA" << endl;
		}
	}
}

C++: #include<iostream>
#include<cstring>
#include<queue>
#define MAX_N 110
#define INF 2<<31
bool A[MAX_N][MAX_N];
int rooter_num;
using namespace std;
int bfs(int,int);
int main()
{
  int n,startNode,goalNode,TTL;
  int r,k,t;
  int a;
  memset(A,false,sizeof(A));
  cin>>rooter_num;
  /*------------input---------------*/
  for(int i=0;i<rooter_num;i++)
    {
      cin>>r>>k;
      for(int j=0;j<k;j++)
	{
	  cin>>t;
	  A[r][t]=true;
	}
    }
  cin>>n;
  /*-------------search--------------*/
  for(int i=0;i<n;i++)
    {
      cin>>startNode>>goalNode>>TTL;
      a=bfs(startNode,goalNode);
      //cout<<"TTL="<<TTL<<";"<<"a-1="<<a<<endl;
      /*
	 TTLの減少値 = (それまでに通ったルータの数) - 1
	 直前のNodeでTTLが1以上ならばパケットが通るので
	 goal地点でのTTLの減少値がTTLと同等,またはそれ以下ならば
	 パケットが通る事になる。
       */
      if(a<=TTL)cout<<a<<endl;
      else cout<<"NA"<<endl;
       
    }
  return 0;
}
int bfs(int startNode,int goalNode)
{
  queue<int>Q;
  int visited[rooter_num+1];
  /*
    visitedは既に通過しているかの判定と経由したルータの数を保持する
  */
  memset(visited,0,sizeof(visited));
  /*
    memsetによって全てを０で初期化する
  */

  int currentNode=startNode;
  Q.push(currentNode);
  visited[currentNode]=1;

  /*-------BFS--------*/  
  while(!Q.empty())
    {
      currentNode=Q.front();
      Q.pop();
      for(int nextNode=1;nextNode<=rooter_num;nextNode++)
	{

	  if(A[currentNode][nextNode]==0)continue;
	  /*
	    接続されていない場合を切り取った
	  */
	  if(nextNode==goalNode)return visited[currentNode]+1;
	  /*
	    goalNodeの接続を確認したので探索を打ち切る
	  */
	  if(visited[nextNode]<=0)
	    {
	      visited[nextNode]=visited[currentNode]+1;
	      Q.push(nextNode);
	    }
	  /*
	    まだ訪れていない(かつ接続出来るNodeを)キューに追加する
	   */
	}
    }
  return INF;
  /*
    接続出来ない場合無限大を返す
   */
}

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(101, false);
    queue<int> fq, nq, sq;
    fq.push(s);
    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;
            } else {
                for (int i: g[pos]) {
                    nq.push(i);
                }
            }
        }
        if (c == 0 || nq.empty()) {
            return -1;
        }
        fq = nq;
        nq = sq;
    }
}

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

int net[SUB][SUB],n;

typedef pair<int,int> BVB;


int ans(int,int,int);

void debug();

int main(){

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

  int 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<SUB;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,mem=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<=SUB;i++){//¢¯éêðL[Éüêé
      if(net[next.first][i] == 1)mem++;  

    
      
      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));
      
      if(mem == n)break;
    }
      
    
  }

  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;

  }



}