http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1221
The first 19 lines of the problem give a link to the city's connectivity, such as
1 3
2 3 4
3 4 5 6
1 6
1 7
2 12 13
1 8
2 9 10
1 11
1 11
2 12 17
1 14
2 14 15
2 15 16
1 16
1 19
2 18 19
1 20
1 20
5
1 20
2 9
19 5
18 19
16 20
The first line indicates that the first city has 1 cities connected to it, and that the city is 3, then N, asking for the shortest path of u->v = =.
Given that the data is only 20 cities, use the Freud shortest
#include <stdio.h>#include<string.h>#include<iostream>#include<algorithm>using namespacestd;intf[ -][ -];#defineINF 9999999intMain () {intn,m; intCnt=0; while(SCANF ("%d", &n)! =EOF) { for(intI=0; i<= A; i++) { for(intj=0; j<= A; j + +) F[i][j]=inf; } for(intI=0; i<n;i++) {scanf ("%d",&m); f[1][m]=1; f[m][1]=1; } for(intI=2; i<= +; i++) {scanf ("%d",&N); for(intj=0; j<n;j++) {scanf ("%d",&m); F[I][M]=1; F[m][i]=1; } } intt,ans=0, U,v; for(intk=1; k<= -; k++) { for(intI=1; i<= -; i++) { for(intj=1; j<= -; j + +) if(f[i][k]+f[k][j]<F[i][j]) f[i][j]=f[i][k]+F[k][j]; }} scanf ("%d",&t); printf ("Test Set #%d\n",++CNT); while(t--) {scanf ("%d%d",&u,&v); printf ("%d to%d:%d\n", U,v,f[u][v]); } printf ("\ n"); } return 0;}
Zoj (1221