Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"strings"
	"math"
	"fmt"
)

var scanner = bufio.NewScanner(os.Stdin)

func main() {
	dd := make([][]int, 1000)
	for i := range dd {
		dd[i] = make([]int, 1000)
	}
	num := make([]int,1000)
	inv := make([]int, 1001)
	x := make([]int, 1000)
	y := make([]int, 1000)
	vv := make([]int, 1000)
	nowt := make([]float64, 1000)
	rev := make([]int, 1000)
	for scanner.Scan() {
		n, _ := strconv.Atoi(scanner.Text())
		if n == 0 {
			break
		}
		for i := 0; i < n; i++ {
			scanner.Scan()
			buf1 := strings.Split(scanner.Text(), " ")
			num[i], _ = strconv.Atoi(buf1[0])
			x[i], _ = strconv.Atoi(buf1[1])
			y[i], _ = strconv.Atoi(buf1[2])
			inv[num[i]] = i
		}
		for i := 0; i < n; i++ {
			for j := 0; j < n; j++ {
				dd[i][j]=(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])
			}
		}
		scanner.Scan()
		m, _ := strconv.Atoi(scanner.Text())
		for i := 0; i < m; i++ {
			scanner.Scan()
			buf2 := strings.Split(scanner.Text(), " ")
			c, _ := strconv.Atoi(buf2[0])
			d, _ := strconv.Atoi(buf2[1])
			c = inv[c]
			d = inv[d]
			for j := 0; j < n; j++ {
				vv[j] = 0
				nowt[j]=999999999
				rev[j]=-1
			}
			nowt[c]=0
			for j := 0; j < n; j++ {
				M := 99999999
				at := 0
				for k := 0; k < n; k++ {
					if vv[k] == 0 && float64(M) > nowt[k] {
						M=int(nowt[k])
						at = k
					}
				}
				if M > 90000000 {
					break
				}
				vv[at]=1
				for k := 0; k < n; k++ {
					if vv[k] == 0 && dd[at][k]<=2500&&nowt[k]>nowt[at]+math.Sqrt(float64(dd[at][k])) {
						nowt[k]=nowt[at]+math.Sqrt(float64(dd[at][k]))
						rev[k] = at
					}
				}
			}
			if vv[d] == 0 {
				fmt.Println("NA")
			} else {
				stack := make([]int, 0)
				V := d
				for V != c {
					stack = append(stack, V)
					V = rev[V]
				}
				fmt.Print(num[c])
				for len(stack) > 0 {
					fmt.Printf(" %d", num[stack[len(stack)-1]])
					stack = stack[:len(stack)-1]
				}
				fmt.Println()
			}
		}
	}
}

C++: #include <iostream>
#include <cmath>
#include <map>
#include <queue>
#include <climits>
#include <algorithm>
using namespace std;
typedef pair<double,double> P;
typedef pair<double,int> NODE;
#define X first
#define Y second
#define DST first
#define POS second
double ecd(P A,P B){
	A.X = A.X-B.X;
	A.Y = A.Y-B.Y;
	return sqrt(A.X*A.X+A.Y*A.Y);
}


int main(){
	int n,a,b,m;
	double x,y;
	while(cin >> n && n){
		map<int,P> coord;
		map<int,int> from;
		map<int,vector< pair<int,double> > > info;
		map<int,double> memo,tmp;
		for(int i=0;i<n;i++){
			cin >> a >> x >> y;
			coord[a] = make_pair(x,y);
		}
		
		for(map<int,P>::iterator it1 = coord.begin();it1 != coord.end();it1++){
			for(map<int,P>::iterator it2 = coord.begin();it2 != coord.end();it2++){
				if(!(ecd((*it1).second,(*it2).second) > 50.0)){
					info[(*it1).first].push_back( make_pair((*it2).first,ecd((*it1).second,(*it2).second)));
				}
			}
			tmp[(*it1).first] = (1<<25);
		}

		cin >> m;
		
		for(int i=0;i<m;i++){
			cin >> a >> b;
			vector<int> route;
			priority_queue<NODE,vector<NODE>,greater<NODE> > Q;
			Q.push(make_pair(0,a));
			memo = tmp;
			memo[a]=0;
			from[a]=-1;
			while(Q.size()){
				NODE q = Q.top();Q.pop();
				if(q.POS == b){
					int p = q.POS;
					while(~p){
						route.push_back(p);
						p = from[p];
					}
					break;
				}
				for(int i=0;i<info[q.POS].size();i++){
					if(memo[info[q.POS][i].first] > q.DST + info[q.POS][i].second){
						memo[info[q.POS][i].first] = q.DST + info[q.POS][i].second;
						from[info[q.POS][i].first] = q.POS;
						Q.push(make_pair(memo[info[q.POS][i].first],info[q.POS][i].first));
					}
				}
			}
			if(route.size()){
				reverse(route.begin(),route.end());
				for(int i=0;i<route.size();i++){
					if(i)cout << " ";
					cout << route[i];
				}
			}else{
				cout << "NA";
			}
			cout << endl;
		}
	}
}

C++: #include<iostream>
#include<cmath>
#include<vector>
#include<cstdlib>
#include<algorithm>
#define INF 1000000000

using namespace std;

void init(int);
int find(int);
void unite(int,int);
bool same(int,int);
void dijkstra(int);
vector<int>get_path(int);

int par[1001],rank[1001];
int V,prev[1001];
double cost[1001][1001],d[1001];
bool used[1001];

int main(void){

  int b[1001],m,s,g;
  double x[1001],y[1001];
  while(cin >> V,V){
    for(int i=0;i<V;i++){
      cin >> b[i] >> x[i] >> y[i];
    }

    init(V);

    for(int i=0;i<=V;i++)
      for(int j=0;j<=V;j++)
	cost[i][j]=INF;

    for(int i=0;i<V;i++){
      for(int j=0;j<V;j++){
	if(i!=j){
	  double a=sqrt(pow(x[i]-x[j],2)+pow(y[i]-y[j],2));
	  if(50.-a>=0){
	    cost[b[i]][b[j]]=cost[b[j]][b[i]]=a;
	    unite(b[i],b[j]);
	  }
	}
      }
    }

    cin >> m;
    for(int i=0;i<m;i++){
      cin>> s >> g;
      if(!same(s,g)){
	cout << "NA" << endl;
      }
      else {
	dijkstra(s);
	vector<int>p=get_path(g);
	for(int j=0;j<p.size();j++){
	  cout << p[j];
	  if(j!=p.size()-1)cout <<" ";
	  else cout << endl;
	}
      }
      
    }

  }

  return 0;
}

void init(int n){
  for(int i=0;i<n;i++){
    par[i]=i;
    rank[i]=0;
  }
}

int find(int x){
  if(par[x]==x)return x;
  else return par[x]=find(par[x]);
}

void unite(int x,int y){
  x=find(x);
  y=find(y);
  if(x==y)return;

  if(rank[x]<rank[y])par[x]=y;
  else{
    par[y]=x;
    if(rank[x]==rank[y])rank[x]++;
  }
}

bool same(int x,int y){
  return find(x)==find(y);
}

void dijkstra(int s){
  fill(d,d+V+1,INF);
  fill(used,used+V+1,false);
  fill(prev,prev+V+1,-1);
  d[s]=0;

  while(true){
    int v=-1;
    for(int u=1;u<=V;u++){
      if(!used[u]&&(v==-1||d[u]<d[v]))v=u;
    }

    if(v==-1)break;
    used[v]=true;

    for(int u=1;u<=V;u++){
      if(d[u]>d[v]+cost[v][u]){
	d[u]=d[v]+cost[v][u];
	prev[u]=v;
      }
    }
  }
}

vector<int>get_path(int t){
  vector<int>path;
  for(;t!=-1;t=prev[t])path.push_back(t);
  reverse(path.begin(),path.end());
  return path;
}

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

const double EPS = 1e-4;

struct Building{
	int x, y;
};

struct Edge{
	int to;
	double cost;
	Edge(){}
	Edge(const int &_to, const double &_cost){
		to = _to; cost = _cost;
	}
};

struct Data{
	int n;
	double cost;
	Data(){}
	Data(const int &_n, const double &_cost){
		n = _n; cost = _cost;
	}
	bool operator < (const Data &a) const{
		return cost > a.cost;
	}
};

double GetDistance(const Building &b1, const Building &b2){
	double dx = b1.x - b2.x, dy = b1.y - b2.y;
	return sqrt(dx*dx + dy*dy);
}

int main(){
	int N, M, B, S, G;

	while(cin >> N, N){
		Building v[1000];
		vector<Edge> e[1000];

		for(int i = 0; i < N; i++){
			cin >> B; B--;
			cin >> v[B].x >> v[B].y;
		}

		for(int i = 0; i < N; i++){
			for(int j = i+1; j < N; j++){
				double dist = GetDistance(v[i], v[j]);
				if(dist <= 50){
					e[i].push_back( Edge(j, dist) );
					e[j].push_back( Edge(i, dist) );
				}
			}
		}

		cin >> M;
		for(int j = 0; j < M; j++){
			cin >> S >> G; S--; G--;

			priority_queue<Data> pq;
			Data pq_top;
			pq.push( Data(S, 0) );
			double memo[1000];
			fill_n(memo, N, -1);
			bool na = true;

			while( !pq.empty() ){
				pq_top = pq.top(); pq.pop();
			
				if(memo[pq_top.n] > -0.5) continue;
				memo[pq_top.n] = pq_top.cost;
				if(pq_top.n == G){
					na = false; break;
				}

				for(int i = 0; i < e[pq_top.n].size(); i++){
					pq.push( Data( e[pq_top.n][i].to, pq_top.cost + e[pq_top.n][i].cost ) );
				}
			}

			if(na){
				puts("NA"); break;
			}

			stack<int> ans;
			while(true){
				ans.push(G);
				if(G == S) break;
				for(int i = 0; i < e[G].size(); i++){
					if( abs(memo[G] - memo[ e[G][i].to ] - e[G][i].cost) < EPS){
						G = e[G][i].to; break;
					}
				}
			}
		
			printf("%d", ans.top()+1 ); ans.pop();
			while( !ans.empty() ){
				printf(" %d", ans.top()+1 ); ans.pop();
			}puts("");
		}
	}
	return 0;
}