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 <queue>
#include <map>
#define MAX 10000000
using namespace std;

typedef pair<int,int> P;
struct edge{
  int to,cost; edge(){}
  edge(int cost,int to):to(to),cost(cost){}
};
vector< vector<edge> > info(101);
int n,m,co[101],ttl;

void init(){
  for(int i=0;i<=100;i++) co[i] = MAX;
}

int Dijkstra(){
  init();
  int s,d;
  cin >> s >> d >> ttl;
  priority_queue<P,vector<P>,greater<P> > que;
  que.push(P(0,s));
  co[s] = 0;
  while(!que.empty()){
    P p = que.top(); que.pop();
    int now = p.second,c = p.first;
    if(now == d) return c + 1;
    for(int i=0;i<info[now].size();i++){
      edge e = info[now][i];
      if(e.cost + c < co[e.to]){
	que.push(P(e.cost + c,e.to));
	co[e.to] = e.cost + c;
      }
    }
  }
}

int main(){
  cin >> n;
  for(int i=0;i<n;i++){
    int r,k,tmp;
    cin >> r >> k;
    for(int j=0;j<k;j++){
      cin >> tmp;
      info[r].push_back(edge(1,tmp));
    }
  }
  cin >> m;
  while(m--){
    int d = Dijkstra();
    if(d > ttl) cout << "NA" << endl;
    else cout << d << endl;
  }
}

C++: #include <stdio.h>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <fstream>
#include <sstream>
#include <math.h>
#include <queue>
#include <stack>
#include <cmath>
#include <string.h>
#include <set>


using namespace std;



int main(){
    int n;
    while(cin>>n){
        int graph[101][101]={0};
        for(int roop=0; roop<n; roop++){
            int r;
            cin>>r;
            int k;
            cin>>k;
            for(int roop2=0; roop2<k; roop2++){
                int t;
                cin>>t;
                graph[r][t]=1;
            }
        }
        
        int q;
        cin>>q;
        for(int roop=0; roop<q; roop++){
            int s,d,v;
            cin>>s>>d>>v;
            int visit[101]={0};
            visit[s]=1;
            queue<pair<int,int> > qu;
            pair<int,int> now;
            now.first=s;
            now.second=v;
            
            qu.push(now);
            bool find=false;
            
            
            while(!qu.empty()){
                
                now=qu.front();
                //cout<<now.first<<" "<<now.second<<endl;
                qu.pop();
                if(now.first==d){
                    find=true;
                    break;
                }
                if(now.second>1){
                    for(int i=1; i<=n; i++){
                        if(graph[now.first][i]==1&&visit[i]==0){
                            visit[i]=1;
                            pair<int,int> temp;
                            temp.first=i;
                            temp.second=now.second-1;
                            qu.push(temp);
                        }
                    }
                }
            }
            if(find)cout<<v-now.second+1<<endl;
            else cout<<"NA"<<endl;
            
        }
    }
}

C++: #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<cctype>
#include<complex>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<functional>
#include<vector>
#include<string>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<bitset>
#include<numeric>
#define INF 1e+8
#define EPS 1e-9
#define PB push_back
#define fi first
#define se second
#define reps(i,j,k) for(int i = j; i < k; i++)
#define rep(i,j) reps(i,0,j)
using namespace std;
const int dx[]={0,1,0,-1},dy[]={1,0,-1,0};
typedef long long ll;
typedef pair<int,int> Pii;
int D[128][128];
int main(){
	int n;
	scanf("%d",&n);
	rep(i,128)rep(j,128)D[i][j] = INF;
	rep(i,n){
		int index;
		scanf("%d",&index);
		int sub;
		scanf("%d",&sub);
		rep(j,sub){
			int tmp;
			scanf("%d",&tmp);
			D[index][tmp] = 1;
		}
	}
	rep(k,101){
		rep(i,101){
			rep(j,101){
				D[i][j] = min(D[i][j],D[i][k]+D[k][j]);
			}
		}
	}
	int m;
	scanf("%d",&m);
	rep(i,m){
		int a,b,cost;
		scanf("%d%d%d",&a,&b,&cost);
		if(D[a][b]+1 <= cost )printf("%d\n",D[a][b]+1);
		else puts("NA");
	}
	return 0;
}

C++: #include <vector>
#include <algorithm>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <cstring>
#include <deque>
#include <cstdio>
#include <cmath>
#include <complex>

using namespace std;

#define reep(i,f,t) for(int i=f ; i<int(t) ; ++i)
#define rep(i,n) reep(i, 0, n) 

typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int,int> pii;

const int INF = 1<<29;

int main()
{
	int n;
	scanf("%d", &n);
	vvi dist(n, vi(n, INF));
	
	rep(i, n){
		int r, k;
		scanf("%d%d", &r, &k);
		--r;
		rep(j, k){
			int a;
			scanf("%d", &a);
			dist[r][a-1] = 1;
		}
	}
	
	rep(k, n) rep(i, n) rep(j, n)
		dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
	
	int p;
	scanf("%d", &p);
	rep(i, p){
		int s, d, v;
		scanf("%d%d%d", &s, &d, &v);
		--s;
		--d;
		if(dist[s][d]+1 <= v)
			printf("%d\n", dist[s][d]+1);
		else
			puts("NA");
	}
	
	return 0;
}

C++: #include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
const int N_MAX = 100;
const int INF = 0x7fffff;

class Node {
public:
	int pos, ttl;
	Node(int p, int t) : pos(p), ttl(t) {}
};

int n;
// adj[from][to] from->toの辺があるか
bool adj[N_MAX + 1][N_MAX + 1];
int visited[N_MAX + 1];

void bfs(int src, int dst, int limit) {
	queue<Node> que;
	fill(visited, visited + n + 1, INF);
	que.push(Node(src, limit - 1));
	while (!que.empty()) {
		Node current = que.front();
		que.pop();
		if (current.pos == dst) {
			cout << limit - current.ttl << endl;
			return;
		}
		if (visited[current.pos] <= current.ttl) continue;
		visited[current.pos] = current.ttl;
		if (current.ttl == 0) continue;
		for (int next = 1; next <= n; next++) {
			if (adj[current.pos][next] && visited[next] > current.ttl) {
				que.push(Node(next, current.ttl - 1));
			}
		}
	}
	cout << "NA" << endl;
}

int main() {
	cin >> n;
	for (int i = 0; i < n; i++) {
		int r, k;
		cin >> r >> k;
		fill(adj[r], adj[r] + n + 1, false);
		for (int j = 0; j < k; j++) {
			int t;
			cin >> t;
			adj[r][t] = true;
		}
	}
	int p;
	cin >> p;
	for (int i = 0; i < p; i++) {
		int s, d, v;
		cin >> s >> d >> v;
		bfs(s, d, v);
	}
	return 0;
}