-
Notifications
You must be signed in to change notification settings - Fork 0
/
567-Risk.cpp
94 lines (77 loc) · 1.59 KB
/
567-Risk.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include<iostream.h>
#include<stdio.h>
#define INF 9999
int c[30];
int d[30];
int q[100];
int head,tail;
int adj[30][30];
int empty(){
if(head == tail ) return 1;
else return 0;
}
void init(){
head=tail=0;
}
void addq(int val){
q[++tail]=val;
}
int delq(){
return q[++head];
}
int bfs(int s,int k){
int u,v;
for(u = 1 ; u < 21 ;u++ ){
c[u]=0;
d[u]=INF;
}
init();
c[s]=1;
d[s]=0;
addq(s);
while(empty()==0){
u = delq();
for( v = 1 ; v < 21 ; v++ ){
if( c[v] == 0 && adj[u][v] < INF ){
c[v]=1;
d[v] = d[u]+1;
addq(v);
}
}
c[u]=1;
}
return d[k];
}
int main(){
int N,i,s,j,k,d,l,temp,val,start,dest,need,count;
count=1;
while(scanf("%d",&N)== 1){
for(i = 1 ; i < 21 ; i++ )
for( j = 1 ; j < 21 ; j++ )
adj[i][j]=INF;
s=1;
for(i = 0 ; i < N ; i++ ){
scanf("%d",&d);
adj[s][d]=1;
adj[d][s]=1;
}
for( s = 2 ; s < 20 ; s++ ){
scanf("%d",&N);
for(i = 0 ; i < N ; i++ ){
scanf("%d",&d);
adj[s][d]=1;
adj[d][s]=1;
}
}
printf("Test Set #%d\n",count);
scanf("%d",&val);
for( i = 0 ; i < val ; i++ ){
scanf("%d%d",&start,&dest);
need = bfs(start,dest);
printf("%2d to %2d: %d\n",start,dest,need);
}
printf("\n");
count++;
}
return 0;
}