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++: //59
#include<iostream>
#include<vector>
#include<queue>
#include<utility>

using namespace std;

int main(){
  int n;
  cin>>n;
  vector<int> e[101];
  for(int i=0;i<n;i++){
    int r,k;
    cin>>r>>k;
    while(k--){
      int t;
      cin>>t;
      e[r].push_back(t);
    }
  }
  int p;
  cin>>p;
  while(p--){
    int s,d,v;
    cin>>s>>d>>v;
    queue<pair<int,int> > que;
    que.push(make_pair(1,s));
    while(!que.empty()){
      int c=que.front().second;
      if(c==d)break;
      for(int i=0;i<e[c].size();i++){
	que.push(make_pair(que.front().first+1,e[c][i]));
      }
      que.pop();
    }
    if(que.empty()||que.front().first>v){
      cout<<"NA"<<endl;
    }else{
      cout<<que.front().first<<endl;
    }
  }
  return 0;
}

C++: #include <iostream>
#include <algorithm>
using namespace std;
int main(){
	int d[101][101];
	for(int i=1;i<=100;i++)for(int j=1;j<=100;j++)d[i][j]=10000000;
	int n;
		int a,b,c;
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>c;
		for(cin>>a;a>0;a--){
			cin>>b;
			d[c][b]=1;
		}
	}
	/*
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(d[i][j]<1000)cout<<d[i][j]<<" ";
			else cout<<"+ ";
		}
		cout<<endl;
	}
	cout<<endl;
	*/
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			for(int k=1;k<=n;k++){
				if(j!=k)if(d[j][k]>d[j][i]+d[i][k])d[j][k]=d[j][i]+d[i][k];
			}
		}
	}
	/*
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			
			if(d[i][j]<1000)cout<<d[i][j]<<" ";
			else cout<<"+ ";
		}
		cout<<endl;
	}
	cout<<endl;
	*/
	for(cin>>n;n>0;n--){
		cin>>a>>b>>c;
		if(d[a][b]<c)cout<<d[a][b]+1<<endl;
		else cout<<"NA"<<endl;
		
	}
	
	return 0;
}

C++: #include<iostream>
#include<algorithm>
using namespace std;
int main(){
  int n,r,k,t1,t2;
  cin >> n;
  int graph[n+1][n+1];
  for(int i=0;i<n+1;i++){
    for(int j=0;j<n+1;j++){
      graph[i][j] = 1000000;
    }
  }

  for(int i=0;i<n;i++){
    cin >> r >> k;
    int t;
    for(int j=0;j<k;j++){
      cin >> t;
      graph[r][t] = 1;
    }
  }


  //ワーシャルフロイド
  for(int k=1;k<n+1;k++){
    for(int i=1;i<n+1;i++){
      for(int j=1;j<n+1;j++){
	graph[i][j] = min( graph[i][j] , graph[i][k] + graph[k][j]);
      }
    }
  }


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

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

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;
  set<int> done;
  set<int> done2;
  que.push_back(s);
  done.insert(s);

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

    done2.clear();

    int len = que.size();
    for(int i=0;i<len;i++){

      int v = que.front();
      que.pop_front();

      for(int j=0;j<nodes[v].length;j++){
        if(nodes[v].ks[j]==e){
          return c+1;
        }else{
          int k = nodes[v].ks[j];
          if(done.find(k) == done.end()){
            que.push_back(k);
            done2.insert(k);
          }
        }
      }
    }
    done.insert(done2.begin(), done2.end());
  }
}

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 <stdio.h>
#include <cmath>
#include <algorithm>
#include <queue>

using namespace std;



int main(){

	int n,r,k,to,table[101][101],num_of_packet,start,goal,t_limit,P[101],D[101],num,min,min_index,current,Distance[101][101];

	for(int i=1; i < 101; i++){
		for(int k=1; k < 101; k++){
			table[i][k] = 0;
			Distance[i][k] = 1000000000;
		}
	}

	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;
		}
	}

	for(int i = 1; i<= n; i++){
		for(int k=1; k <= n; k++){
			if(i != k){
				start = i, goal = k;

				for(int index=1; index <= n; index++){
					P[index] = 0;
					D[index] = 1000000000;
				}

				P[start] = 1;
				num = 1;

				for(int index = 1; index <= n; index++){
					if(index != start && table[start][index] == 1){
						D[index] = 1;
					}
				}

				while(num < n){
					min = 1000000000;
					for(int index=1;index <= n; index++){
						if(P[index] == 0 && D[index] < min){
							min = D[index];
							min_index =index;
						}
					}

					if(min == 1000000000)break;

					P[min_index] = 1;
					current = min_index;
					num++;
					for(int index=1; index <= n; index++){
						if(table[current][index] == 1){
							D[index] = (D[index] <= (D[current]+1))?D[index]:(D[current]+1);
						}
					}
				}

				Distance[start][goal] = D[goal];
			}
		}
	}

	scanf("%d",&num_of_packet);

	for(int i=0; i < num_of_packet; i++){
		scanf("%d %d %d",&start,&goal,&t_limit);

		if(Distance[start][goal] >= t_limit){
			printf("NA\n");
		}else{
			printf("%d\n",Distance[start][goal]+1);
		}

	}

    return 0;
}

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

using namespace std;


static const int NO = -2;
static const int N = 300;


int graph[N + 1][N + 1] = {{}};
int dis[N + 1] = {};
int n;

void bfs(int v){
  queue<int> Q;

  Q.push(v);

  for(int i = 1; i <= N; i++){
    dis[i] = NO;
  }

  dis[v] = 1;

  int u;
  while(!Q.empty()){
    u = Q.front();
    Q.pop();

    for(int i = 1;i <= n; i++){
      if(graph[u][i]==0) continue;
      if(dis[i]!=NO)continue;
      dis[i] = dis[u] + 1;
      Q.push(i);

    }
  }
}

int main(){

  int r[N+1] = {};
  int d[N+1] = {};
  int k[N+1] = {};
  int f[N+1] = {};
  int p,s[N+1] = {};
  int v[N+1] = {};


  //in
  cin >> n;

  for(int i = 1;i <= n; i++){

    cin >> r[i] >> k[i];

    for(int j = 1;j <= k[i];j++){

      cin >> f[j];
      graph[i][f[j]] = 1;
    }
  }

  cin >> p;
  for(int i = 1;i <= p; i++){
    cin >> s[i] >> d[i] >> v[i];
    bfs(s[i]);

    if(v[i] >= dis[d[i]]) cout << dis[d[i]] << endl;
    else cout << "NA" << endl;
  }
}