题意:给出一个20个点的无向图,求任意两点间的最短路。
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=22152
——>>算是Floyd算法的模版题吧~
#include <cstdio>
#include <cstring>
#include <algorithm>using namespace std;const int maxn = 20 + 10;int d[maxn][maxn], n, kase;void init(){memset(d, 0x3f, sizeof(d));for(int i = 1; i <= 20; i++) d[i][i] = 0;
}bool read(){init();int n, v, i, j;if(scanf("%d", &n) == EOF) return 0;for(i = 0; i < n; i++){scanf("%d", &v);d[1][v] = 1;d[v][1] = 1;}for(i = 2; i <= 19; i++){scanf("%d", &n);for(j = 0; j < n; j++){scanf("%d", &v);d[i][v] = 1;d[v][i] = 1;}}return 1;
}void Floyd(){for(int k = 1; k <= 20; k++)for(int i = 1; i <= 20; i++)for(int j = 1; j <= 20; j++)d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}void solve(){printf("Test Set #%d\n", kase++);scanf("%d", &n);int u, v;while(n--){scanf("%d%d", &u, &v);printf("%2d to %2d: %d\n", u, v, d[u][v]);}puts("");
}int main()
{kase = 1;while(read()){Floyd();solve();}return 0;
}