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<algorithm>
#define M 10000

using namespace std;

bool TransportPacket(const vector< vector<int> >& data,
		     int s, int g, int ttl, int* ans_){
  vector<bool> is_reached(data.size(), false);
  vector<int> ans(data.size(), M);
  int i, j;
  ans[s] = 0;
  i = s;
  while(i != g){
    is_reached[i] = true;
    for(j=0; j<data[i].size(); ++j){
      ans[j] = min(ans[j], ans[i] + data[i][j]);
    }
    for(i=-1, j=0; j<is_reached.size(); ++j){
      if(!is_reached[j] && (i < 0 || ans[i] > ans[j])) i = j;
    }
  }
  *ans_ = ans[g] + 1;
  if(ttl - *ans_ >= 0) return true;
  else return false;
}

int main(){
  int n, r, k, t;
  int s, d, v, ans;

  cin >> n;
  vector< vector<int> > data(n, vector<int>(n, M));

  for(int i=0; i<n; ++i){
    cin >> r >> k;
    for(int j=0; j<k; ++j){
      cin >> t;
      data[r-1][t-1] = 1;
    }
  }
  cin >> n;
  for(int i=0; i<n; ++i){
    cin >> s >> d >> v;
    if(TransportPacket(data, s-1, d-1, v, &ans)) cout << ans << endl;
    else cout << "NA" << endl;
  }
  return 0;
}

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

using namespace std;

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

node *nodes;

list<int> que;

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

  que.clear();
  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<algorithm>
using namespace std;
#define INF (1<<29)

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

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

const int inf = 1<<24;
const int size = 101;
int n, p;
int CN[size][size];
int CNcnt[size];
int MC[size];

typedef pair <int, int> Node;
typedef priority_queue <Node, vector <Node>, greater <Node> > QUEUE;

void dijkstra( int start, int goal, int ttl ) {
    QUEUE Q;
    Node start_node( 1, start );
    MC[start] = 1;
    Q.push( start_node );

    while ( !Q.empty() ) {
        Node node = Q.top();
        Q.pop();

        int router = node.second;
        int cost = node.first;

        if ( router == goal ) {
            cout << cost << endl;
            return;
        }

        for ( int i = 0; i < CNcnt[router]; i++ ) {
            int next_router = CN[router][i] - 1;
            int next_cost = cost + 1;
            
            if ( next_cost >= ttl + 1 ) continue;
            if ( next_cost >= MC[next_router] ) continue;
            MC[next_router] = next_cost;
            Node next_node( next_cost, next_router );
            Q.push( next_node );
        }
    }

    cout << "NA" << endl;
}

void init() {
    for ( int i = 0; i < size; i++ ) {
        MC[i] = inf;
    }
}

void solve() {
    cin >> p;
    for ( int i = 0; i < p; i++ ) {
        int start, goal, ttl;
        cin >> start >> goal >> ttl;
        init();
        dijkstra( start-1, goal-1, ttl );
    }
}

void input() {
    cin >> n;
    for ( int i = 0; i < n; i++ ) {
        int r, k;
        cin >> r >> k;
        CNcnt[r-1] = k;
        for ( int j = 0; j < k; j++ ) {
            cin >> CN[r-1][j];
        }
    }
}

int main( void ) {
    input();
    solve();
    return 0;
}

C++: #include <iostream>
#include <set>
#include <string>
#include <string.h>
#include <vector>
using namespace std;

const int SIZE = 101;


class Packet
{
public:
	Packet( int _now, set< int > _via ) : now( _now ), via( _via ){}
	bool isPast( int p )
	{
		if( via.find( p ) != via.end() ){
			return true;
		}
		return false;
	}

	int now;
	set< int > via;
};

int solve( int table[ SIZE ][ SIZE ], int s, int e, int initTtl, int numRooter )
{
	if( initTtl < 2 ){
		return 0;
	}
	vector< Packet > pList;
	for( int i = 1; i <= numRooter; ++i ){
		set< int > via;
		via.insert( s );
		if( table[ s ][ i ] ){
			if( i == e ){
				return 2;
			}
			set< int > via2 = via;
			via2.insert( i );
			pList.push_back( Packet( i, via2 ) );
		}
	}

	int ttl = initTtl;
	while( pList.size() > 0 ){
		--ttl;
		if( ttl < 1 ){
			return 0;
		}
		vector< Packet > pList2;
		for( int i = 0; i < pList.size(); ++i ){
			Packet& p = pList[ i ];
			if( p.now == e ){
				return p.via.size();
			}
			for( int j = 1; j <= numRooter; ++j ){
				if( table[ p.now ][ j ] && !p.isPast( j ) ){
					set< int > via = p.via;
					via.insert( j );
					pList2.push_back( Packet( j, via ) );
				}
			}
		}
		pList = pList2;
	}

	return 0;
}

int main()
{
	int table[ SIZE ][ SIZE ];

	int n;
	while( cin >> n ){
		cin.ignore( 100, '\n' );
		memset( table, 0, sizeof(table) );
		string s;
		for( int i = 0; i < n; ++i ){
			getline( cin, s );
			int o = s[ 0 ] - '0';
			for( int j = 2; j < s.size(); j += 2 ){
				table[ o ][ s[ j ] - '0' ] = true;
			}
		}
		int p;
		cin >> p;
		for( int i = 0; i < p; ++i ){
			int from, to, ttl;
			cin >> from >> to >> ttl;
			int result = solve( table, from, to, ttl, n );
			if( result > 0 ){
				cout << result << endl;
			}else{
				cout << "NA" << endl;
			}
		}
	}

	return 0;
}