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 <sstream>
#include <string>
#include <vector>
#include <map>
#include <algorithm>
#include <iostream>
#include <utility>
#include <set>
#include <cctype>
#include <queue>
#include <stack>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
using namespace std;
#define INF 100000000
typedef long long ll;

int d[110][110];

int main(void) {
    int n;
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            d[i][j] = INF;
        }
        d[i][i] = 0;
    }
    for (int i = 0; i < n; i++) {
        int r, k;
        cin >> r >> k;
        --r;
        int t;
        for (int j = 0; j < k; j++) {
            cin >> t;
            d[r][t-1] = 1;
        }
    }
    for (int k = 0; k < n; k++) {
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < n; j++) {
                d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
            }
        }
    }
    int p;
    cin >> p;
    for (int i = 0; i < p; i++) {
        int s, D, v;
        cin >> s >> D >> v;
        if (v >= d[s-1][D-1]) {
            cout << d[s-1][D-1]+1 << endl;
        } else {
            cout << "NA" << endl;
        }
    }
    return 0;
}

C++: #include <bits/stdc++.h>
using namespace std ;

#define pb(n) push_back(n)
#define fi first
#define se second
#define all(r) (r).begin(),(r).end()
#define gsort(st,en) sort((st),(en),greater<int>())
#define vmax(ary) *max_element(all(ary))
#define vmin(ary) *min_element(all(ary))
#define debug(x) cout<<#x<<": "<<x<<endl
#define fcout(n) cout<<fixed<<setprecision((n))
#define scout(n) cout<<setw(n)
#define vary(type,name,size,init) vector< type> name(size,init)

#define rep(i,n) for(int i = 0; i < (int)(n);++i)
#define REP(i,a,b) for(int i = (a);i < (int)(b);++i)
#define repi(it,array) for(auto it = array.begin(),end = array.end(); it != end;++it)
#define repa(n,array) for(auto &n :(array))

using ll = long long;
using vi = vector<int>;
using vl = vector<ll>;
using dict = map<string,int>;
using pii = pair<int,int> ;

constexpr int imax = ((1<<30)-1)*2+1 ;
constexpr int inf = 100000000;
constexpr double PI = acos(-1.0) ;
double eps = 1e-10 ;
const int dy[] = {-1,0,1,0};
const int dx[] = {0,-1,0,1};

inline bool value(int x,int y,int w,int h){
  return (x >= 0 && x < w && y >= 0 && y < h);
}

template<typename T>
void Unique(vector<T> &v){
  sort(all(v));
  v.erase(unique(all(v)),v.end());
}

template<typename T>
T ston(string& str, T n){
  istringstream sin(str) ;
  T num ;
  sin >> num ;
  return num ;
}

void Ans(bool f){
  if(f) cout << "YES"<<endl;
  else cout << "NO"<<endl;
}
struct Edge{
    int to;
      long long cost;
};

struct NODE{
  int pos;
  long long cost;
};
bool operator < (const NODE &a,const NODE &b){
  return a.cost > b.cost;
}
vector<Edge> g[100000],rg[100000];

int N;
const ll INF = 1e15;
vector<ll> dijkstra(vector<Edge> g[100000],int s){
  priority_queue<NODE> Q;
  Q.push({s,0});
  vector<ll> res(N+1,INF);
  while(Q.size()){
    NODE q= Q.top();Q.pop();
    if(res[q.pos] == INF){
      res[q.pos] = q.cost;
    }
    else{
      continue;
    }
    for(auto n : g[q.pos]){
      Q.push({n.to,q.cost+n.cost});
    }
  }
  return res;
}

int main(){
  cin.tie(0);
  ios::sync_with_stdio(false);
  int n,r,k,t,p;
  int s,d,v;
  cin >> n;
  N = n;
  REP(i,1,n+1){
    cin >> r >> k;
    rep(j,k){
      cin >> t;
      g[r].push_back(Edge{t,1});
    }
  }
  cin >> p;
  rep(i,p){
    cin >> s >> d >> v;
    auto rt = dijkstra(g,s);
//     debug(rt[d]);
    if(rt[d] >= v){
      cout << "NA"<<endl;
    }
    else{
      cout << rt[d]+1<<endl;
    }
  }
  return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<limits.h>
using namespace std;

int n;
int d[103][103];
void wf();

int main(){ 
  while(cin >> n)wf();
}




void wf(){
  int i1,m,J2;
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      if(i == j)d[i][j]=0;
      d[i][j]=1000000000;//INT_MAXツつセツづバツグツづゥ
    }
  }

  for(int i=0;i<n;i++){
    cin >> i1 >> m;
    for(int j=0;j<m;j++){
      cin >> J2;
      d[i1-1][J2-1]=1;
    }
  }

  for(int k=0;k<n;k++)for(int i=0;i<n;i++)for(int j=0;j<n;j++)d[i][j] = min(d[i][j],d[i][k]+d[k][j]);
  int oh;

  cin >> oh;
  int st,ed,ttl;
  for(int i=0;i<oh;i++){
    cin >> st >> ed >> ttl;
    if(d[st-1][ed-1]+1 > ttl)cout << "NA" << endl;
    else cout << d[st-1][ed-1]+1 << endl;

  }/*
 for(int i=0;i<n;i++){
     for(int j=0;j<n;j++)cout << d[i][j] << " " ;
     cout << endl;
   }
   */

}

C++: #include <cstdio>
#include <vector>
#include <queue>
#include <utility>
#include <algorithm>
using namespace std;
typedef pair<int, int> P;

const int INF = 1000000000;

int n;
vector<int> way[128];

int mini(int s, int d, int v);

int main()
{
	scanf("%d", &n);
	for (int i = 0; i < n; i++){
		int r, k;
		
		scanf("%d %d", &r, &k);
		while (k-- > 0){
			int t;
			
			scanf("%d", &t);
			way[r].push_back(t);
		}
	}
	
	int p;
	
	scanf("%d", &p);
	while (p-- > 0){
		int s, d, v;
		
		scanf("%d %d %d", &s, &d, &v);
		int cost = mini(s, d, v);
		if (cost == INF) puts("NA");
		else printf("%d\n", cost);
	}
	
	return 0;
}

int mini(int s, int d, int v)
{
	int diff[128];
	queue<P> q;
	
	fill(diff, diff + n + 1, INF);
	q.push(P(s, 1));
	diff[s] = 1;
	while (!q.empty()){
		P p = q.front(); q.pop();
		
		if (diff[p.first] < p.second) continue;
		for (int i = 0; i < way[p.first].size(); i++){
			int tmp = way[p.first][i];
			
			if (diff[tmp] > diff[p.first] + 1){
				diff[tmp] = diff[p.first] + 1;
				q.push(P(tmp, diff[tmp]));
			}
		}
	}
	
	if (diff[d] == INF || diff[d] > v) return INF;
	else return diff[d];
}