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<stack>
#include<vector>
#include <map>
#include <queue>
#include<string.h>
using namespace std;
typedef pair<int,int> P;
int node[200][200];
int main()
{
  int n;
  cin>>n;
  memset(node,-1,sizeof(node));
  for(int x=0;x<n;x++)
    {
      int k,t;
      cin>>k>>t;
      for(int i=0;i<t;i++)
	{
	  int tmp;
	  cin>>tmp;
	  node[k][tmp]=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));
      for(int i=0;i<200;i++)
	{
	  if(v-1>0)
	    if(node[s][i]==1)
	      qu.push(P(i,2));
	}
      while(!qu.empty())
	{
	  P a = qu.front();
	  qu.pop();
	  //      cout<<a.first<<" "<<a.second<<endl;
	  if(a.first==d)
	    {
	      ans=a.second;
	      break;
	    }
	  for(int i=0;i<200;i++)
	    {
	      if(node[a.first][i]==1)
		{
		  if(a.second+1<=v)
		    {
		      if(c[i]==-1)
			{
			  c[i]=1;
			  qu.push(P(i,a.second+1));	    
			}
		    }
		}
	    } 
	}
      
      
      if(ans==-1)
	cout<<"NA"<<endl;
      else
	cout<<ans<<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;
  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++){
      for(set<int>::iterator it = done.begin();it!=done.end();it++) que.remove((*it));

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

      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>
#include <utility>
#include <queue>
#define INF 10000000
using namespace std;
typedef pair<int,int> p;
vector<int> V[101];
int cost[101];

void dijkstra(int s,int d,int v1) {
  priority_queue<p,vector<p>, greater<p> > que;
  for(int i=0;i<101;i++) {
    cost[i] = INF;
  }
  cost[s] = 1;
  que.push(p(0,s));

  while(!que.empty()) {
    p v = que.top();que.pop();
    int P = v.second;
    if(cost[P] < v.first) continue;
    for(unsigned int i=0;i<V[P].size();i++) {
      int to = V[P][i];
      if(cost[to] > cost[P] + 1) {
        cost[to] = cost[P] + 1;
        que.push(p(cost[to],to));
      }
    }
  }
  if(v1 - cost[d] >= 0)
    cout << cost[d] << endl;
  else
    cout << "NA" << endl;
}

int main() {
  int n,t,k,r,p,s,d,v;
  string str;
  while(cin >> n) {

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

    cin >> p;
    for(int i=0;i<p;i++) {
      cin >> s >> d >> v;
      dijkstra(s,d,v);
    }
  }
}

C++: #include<vector>
#include<list>
#include<algorithm>
#include<iostream>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
using namespace std;
void f(vector<int> t[],int k[],int b[],int c,int s,int d,int v){
  int i;
  if(s==d){
    if(b[s]==0)
      b[s]=c;
    else
      if(b[s]>c)
	b[s]=c;
  }else if(v==1);
  else if(b[s]==0||b[s]>c){
    b[s]=c;
    for(i=0;i<k[s];i++)
      f(t,k,b,c+1,t[s][i],d,v-1);
  }
  return;
}
int main(){
  int i,j;
  int k[101],b[101],n,p,s,d,v;
  vector<int> t[101],u;
  cin>>n;
  for(i=0;i<n;i++){
    int q,r;
    cin>>r>>q;
    k[r]=q;
    t[r].reserve(k[r]);
    for(j=0;j<k[r];j++)
      cin>>t[r][j];
  }
  cin>>p;
  u.reserve(p);
  for(i=0;i<p;i++){
    cin>>s>>d>>v;
    memset(b,0,sizeof(b));
    f(t,k,b,1,s,d,v);
    u[i]=b[d];
  }
  for(i=0;i<p;i++){
    if(u[i])
      cout<<u[i]<<endl;
    else
      cout<<"NA"<<endl;
  }
  return 0;
}

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

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<int> que;
	fill(visited, visited + n + 1, -1);
	que.push(src);
	visited[src] = 1;
	while (!que.empty()) {
		int current = que.front();
		que.pop();
		if (current == dst) {
			if (visited[dst] > limit) break;
			cout << visited[dst] << endl;
			return;
		}
		for (int next = 1; next <= n; next++) {
			if (adj[current][next] && visited[next] == -1) {
				que.push(next);
				visited[next] = visited[current] + 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, 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;
}