#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;const int inf = 999999999;int map[30][30];void floyd(){ int i,j,k; for(k = 1; k<=20; k++) for(i = 1; i<=20; i++) for(j = 1; j<=20; j++) { if(map[i][j]>map[i][k]+map[k][j]) map[i][j] = map[i][k]+map[k][j]; }}int main(){ int i,j,n,x,y,cas = 1; while(~scanf("%d",&n)) { for(i = 0; i<=22; i++) { for(j = 0; j<=22; j++) map[i][j] = inf; } for(i = 1; i<=n; i++) { scanf("%d",&x); map[1][x] = map[x][1] = 1; } for(i = 2; i<=19; i++) { scanf("%d",&n); while(n--) { scanf("%d",&x); map[i][x] = map[x][i] = 1; } } floyd(); printf("Test Set #%dn",cas++); scanf("%d",&n); while(n--) { scanf("%d%d",&x,&y); printf("%d to %d: %dn",x,y,map[x][y]); } printf("n"); } return 0;}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)