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 <queue>

using namespace std;

typedef struct {
  int *ks;
  int length;
} node;

node *nodes;

int dikstra(int s, int e){
  int c=0;
  queue<int> stk;
  stk.push(s);

  while(1){
    c++;
    int len = stk.size();
    for(int i=0;i<len;i++){

      int v = stk.front();
      stk.pop();

      for(int j=0;j<nodes[v].length;j++){
        if(nodes[v].ks[j]==e) return c+1;
        stk.push(nodes[v].ks[j]);
      }
    }
  }
}

int main(){
  int n;
  scanf("%d", &n);

  nodes = new node[n];

  int i,j;
  int t,u,v;
  for(i=0;i<n;i++){
    scanf("%d%d", &t,&u);
    nodes[t-1].ks = new int[u];
    nodes[t-1].length = u;
    for(j=0;j<u;j++){
      scanf("%d", &v);
      nodes[t-1].ks[j] = v-1;
    }
  }

  int m;
  scanf("%d", &m);
  for(i=0;i<m;i++){
    scanf("%d%d%d", &t, &u, &v);
    int r;
    if(v >= (r=dikstra(t-1, u-1))) printf("%d\n", r);
    else puts("NA");
  }
}

C++: #include <stdio.h>
#include <list>

using namespace std;

typedef struct {
  int *ks;
  int length;
} node;

node *nodes;


int dikstra(int s, int e, int deep){
  int c=0;

  list<int> que;
  que.push_back(s);

  int flag = 1;

  while(flag){
    c++;
    if(c >= deep) return -1;

    int len = que.size();
    for(int i=0;flag&&i<len;i++){
      int v = que.front();
      que.pop_front();
      for(int j=0;flag&&j<nodes[v].length;j++){
        if(nodes[v].ks[j]==e){
	  flag=0;
        }else{
	  que.push_back(nodes[v].ks[j]);
        }
      }
    }
  }
  return c+1;
}

int main(){
  int n;
  scanf("%d", &n);

  nodes = new node[n];

  int i,j;
  int t,u,v;
  for(i=0;i<n;i++){
    scanf("%d%d", &t,&u);
    nodes[t-1].ks = new int[u];
    nodes[t-1].length = u;
    for(j=0;j<u;j++){
      scanf("%d", &v);
      nodes[t-1].ks[j] = v-1;
    }
  }

  int m;
  scanf("%d", &m);
  for(i=0;i<m;i++){
    scanf("%d%d%d", &t, &u, &v);
    int r;
    if((r=dikstra(t-1, u-1,v)) != -1) printf("%d\n", r);
    else puts("NA");
  }
}

C++: #include <iostream>
#include <vector>

void show(int** a, int n)
{
	for (int i=0; i<n; i++) {
		for (int j=0; j<n; j++) std::cout << a[i][j] << " ";
		std::cout <<std::endl;
	}
}

int main()
{
	int n; std::cin >> n;
	int** D = new int*[n];
	for (int i=0; i<n; i++) D[i] = new int[n];
	for (int i=0; i<n; i++) {
		for (int j=0; j<n; j++) D[i][j] = (i==j) ? 0 : n;
	}
	for (int i=0; i<n; i++) {
		int s; std::cin >> s;
		int k; std::cin >> k;
		for (int j=0; j<k; j++) {
			int t; std::cin >> t;
			D[s-1][t-1] = 1;
		}
	}
	
	//show(D, n);
	// Floyd-Warshall
	for (int k=0; k<n; k++) {
		for (int i=0; i<n; i++) {
			for (int j=0; j<n; j++) {
				D[i][j] = std::min(D[i][j], D[i][k] + D[k][j]);
			}
		}
		//std::cout << k << std::endl;
		//show(D, n);
	}
	
	int p; std::cin >> p;
	for (int i=0; i<p; i++) {
		int s, t, v;
		std::cin >> s >> t >> v;
		if (D[s-1][t-1] < n && D[s-1][t-1]+1 <= v) std::cout << D[s-1][t-1]+1 << std::endl;
		else std::cout << "NA" << std::endl;
	}
	
	for (int i=0; i<n; i++) delete[] D[i];
	delete[] D;
		
	return 0;
}

C++: #include<queue>
#include <map>
#include<iostream>
#include<cstring>
using namespace std;
typedef pair<int,int> P;

queue<P> qu;

int main()
{
  int table[200][200];
  int n;
  cin>>n;
  for(int x=0;x<n;x++) // ノード数
    {
      int r;   // ノード番号
      int k;   // rにつながるノード数
      cin>>r>>k;
      for(int i=0;i<k;i++)
	{
	  int t; // rにつながるノード番号
	  cin>>t;
	  table[r][t]=1;
	}
    }
  int p;
  cin>>p;
  for(int x=0;x<p;x++)
    {
      int s,d,v;
      cin>>s>>d>>v;
      int ans = -1;
      queue<P> qu;
      int c[200];
      memset(c,-1,sizeof(c));  //経路到達情報の初期化
      if(v>1)                   //TTLが1のときは追加しない
	for(int i=0;i<200;i++)
	  if(table[s][i]==1)    //s->iの経路があればキューに追加
	    qu.push(P(i,2));
       
      while(!qu.empty()) {     //キューが空になるまで続ける
	P a = qu.front(); qu.pop(); //キューの先頭を取出
	//	cout<<a.first<<"  "<<a.second<<endl;
	//a.firstがゴールなら、終了
	if(a.first==d) { ans=a.second;break; }
	if(a.second+1<=v) { // パケットのTTLが0にならないとき
	  for(int i=0;i<200;i++) {
	    //隣接かつ今までそのノードに到達していなければキューに追加
	    if(table[a.first][i]==1 && c[i]==-1) {
	      c[i] = 1;
	      qu.push(P(i,a.second+1));
	    }
	  }
	}
	
      }
      if(ans!=-1)
	cout<<ans<<endl;
      else
	cout<<"NA"<<endl;
      
    }
  

  

}

C++: #include <algorithm>
#include <iomanip>
#include <iostream>

#define rep(i,n) for(int i=0;i<(int)(n);i++)
const int INF = 10000000;

using namespace std;

/** Problem0144 : Packet Transportation **/
int G[101][101];
int V;

int main()
{
	int r, k, t;
	
	rep(i, 101) rep(j, 101) G[i][j]=INF;
	
	
	cin >> V;
	rep(i, V) {
		cin >> r >> k;
		rep(j, k) {
			cin >> t;
			G[r-1][t-1]=1;
		}
	}
	
	// Warshall-Floyd法
	rep(k, V)
		rep(i, V)
			rep(j, V)
				G[i][j] = min(G[i][j], G[i][k]+G[k][j]);
			
	
	int p, s, d, v;
	cin >> p;
	
	rep(i, p) {
		cin >> s >> d >> v;
		if (G[s-1][d-1] < v)
			cout << G[s-1][d-1]+1 << endl;
		else
			cout << "NA" << endl;
	}
	return 0;
}

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] >= pket)
			cout << "NA" << endl;
		else{
			cout << d[a-1][b-1]+1 << endl;
		}
	}
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
vector <int> ro[1000];
int s,d,v;
int s2,d2;
int kk,n,r,md;
bool flg[10000] ;


void visit(int sn,int dis) {
  //  for(int i=1;i<=n;i++)cout << flg[i];
  // cout <<" ";
  
  if(sn == d-1 && dis <=v)  md = min(md,dis);
  else if(dis < v && dis < md) {
    for(int i=0;i<ro[sn].size();i++) {
      if(flg[ro[sn][i]] == 0){
	//	cout <<sn+1 << "to"<<ro[sn][i] << endl;
	flg[ro[sn][i]] = 1;
	visit(ro[sn][i]-1,dis+1);
	flg[ro[sn][i]] = 0;
      }
    } 
  }
}
  
int main() {

  cin >> n;
    
  for(int i=0;i<n;i++) {
    cin >> r >> kk;
    ro[r-1].resize(kk);
    for(int j=0;j<kk;j++) cin >> ro[r-1][j];
  }
  
  int p;
  cin >> p;
  for(int i=0;i<p;i++) {
    cin >> s >> d >> v;
    
    for(int j=1;j<=n;j++) flg[j] = 0;
    md = 10000000;
    flg[s]  = 1;   
    visit(s-1,1);
    
    if(md != 10000000) cout << md <<endl;
    else cout << "NA" <<endl;
  }
  
  return 0;
  
}