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(){
		ttl = current = 0;
	}

	void copy(Packet t){
		ttl = t.ttl;
		current = t.current;
		for(int i=0; i < 101; i++)checkTable[i] = t.checkTable[i];
	}

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

		while(!Q.empty()){
			tmp.copy(Q.front());
			Q.pop();

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

						Packet q;
						q.copy(tmp);
						q.ttl--;
						q.set(m);
						Q.push(q);
					}
				}
			}
			if(FLG)break;
		}

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

	}

    return 0;
}

C++: &#65279;#include<iostream>
#include<queue>

using namespace std;

int main(){

  int n;
    //making graph
    cin >> n;
    
    int data[n+1][n+1];
    
    for(int i = 0; i <= n; i++){
      for(int j = 0; j <= n; j++){
	data[i][j] = 0;
      }
    }
    
    int from, to, conesum;

    for(int i = 0; i < n; i++){
      cin >> from >> conesum;
      for(int j = 0; j < conesum; j++){
	cin >> to;
	data[from][to] = 1;
      }
    }
    //making graph end

    //judge

    int sumpake;
    cin >> sumpake;

    queue<int> Q;

    int ttl, myposi, min, sumin, sumina, finish = 0;

    for(int i = 0; i < sumpake; i++){
      min = 0;
      cin >> from >> to >> ttl;
      myposi = from;
      for(int j = 0; j < ttl; j++){
	if(j != 0){
	  for(int l = 0; l < sumina; l++){
	    //cout << "sumina " << sumina << endl;
	    if(Q.front() == to){
	      min = j;
	      finish = 1;
	      //cout << "much" << endl;
	      break;
	    }
	    myposi = Q.front();
	    //cout << "pop  " << Q.front()<<endl;
	    Q.pop();
	    for(int k = 1; k <= n; k++){
	      if(data[myposi][k] == 1) {
		Q.push(k);
		sumin++;
	      }
	    }
	    
	  }
	  if(finish == 1) break;
	}else{
	  sumin = 0;
	  for(int k = 1; k <= n; k++){
	    if(data[myposi][k] == 1) {
	      Q.push(k);
	      sumin++;
	    }
	  }
	  
	}
	//	for(int k = 1; k <= n; k++){
	//	    if(data[myposi][k] == 1) {
	//	      Q.push(k);
	//	      sumin++;
	//	    }
	//	}
	  sumina = sumin;
	  sumin = 0;
      }

      
      if(min == 0) cout << "NA";
      else cout << min+1;
      cout << endl;
      finish = 0;
      while(!Q.empty()) Q.pop();
    }



  return 0;
}

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

int main(){
	int n, p;
	bool route[101][101];
	
	for(int i=0; i<=100; ++i)
	for(int j=0; j<=100; ++j)
	route[i][j]=false;
	
	scanf(" %d", &n);
	for(int i=0; i<n; ++i){
		int r, k;
		scanf(" %d %d", &r, &k);
		for(int j=0; j<k; ++j){
			int t;
			scanf(" %d", &t);
			route[r][t]=true;
		}
	}
	
	scanf(" %d", &p);
	for(int i=0; i<p; ++i){
		int cost[101];
		for(int j=0; j<=100; ++j) cost[j]=1000;
		
		int s, d, v;
		scanf(" %d %d %d", &s, &d, &v);
		
		//bfs
		queue<int> que;
		cost[s]=1;
		que.push(s);
		while( !que.empty() ){
			int v=que.front();
			que.pop();
			for(int j=1; j<=n; ++j){
				if(route[v][j] && cost[j] > cost[v]+1){
					cost[j]=cost[v]+1;
					que.push(j);	
				}
			}	
		}
		
		if(cost[d] <= v) printf("%d\n", cost[d]);
		else printf("NA\n");
	}	
	
	
}