。。。啊啊啊。。崩溃啊,,半个月不刷题的下场就是这样么= =!!、?
#include<stdio.h> #include<string.h> int nz,nr; //nz:地区数目 nr:公交线路数目 int zone[10000][10]; //存图 int route[10][20]; //存路径 int res[10000]; //存结果 int visit[10000]; int queue[10000]; void bfs(int p) { int head=0,tail=0; queue[tail]=p; visit[queue[tail++]]=1; res[queue[head]]=1; while (head<tail) { for (int i=0; zone[head][i]!=0 && !visit[zone[head][i]]; i++) { if(res[queue[head]]+1>res[zone[head][i]]) { res[zone[head][i]]=res[queue[head]]+1; queue[tail]=zone[head][i]; visit[queue[tail++]]=1; } } head++; } } void go() { //初始化并输入数据 memset(visit, 0, sizeof(visit)); memset(zone, 0, sizeof(zone)); memset(route, 0, sizeof(route)); memset(res, 0, sizeof(res)); scanf("%d%d",&nz,&nr); for (int i=0; i<nz; i++) { int z,n; scanf("%d%d",&z,&n); for (int j=0; j<n; j++) { scanf("%d",&zone[z][j]); } } for (int i=0; i<nr; i++) { int n; scanf("%d",&n); for (int j=0; j<n; j++) { scanf("%d",&route[i][j]); bfs(route[i][j]); } } int min=1000000,minp; for (int i=0; i<10000; i++) { if (res[i]!=0&&res[i]<min) { min=res[i]; minp=i; } } for (int i=0; i<10000; i++) { if(res[i]!=0) printf("%d %d\n",i,res[i]); } printf("%d %d\n",min,minp); } int main() { int T; scanf("%d",&T); while (T--) { go(); } }