Skip to content

Instantly share code, notes, and snippets.

@wasabili
Created September 6, 2010 14:31
Show Gist options
  • Save wasabili/567096 to your computer and use it in GitHub Desktop.
Save wasabili/567096 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <list>
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;
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");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment