poj 1603 Floyd

来源:互联网 发布:手机下单软件 编辑:程序博客网 时间:2024/06/07 08:40
//求所有节点之间的最短路#include <iostream>#include <cstring>using namespace std;#define MAXN 21int x,j;int n,a,b;int T;int dist[MAXN][MAXN];void Floyd(){  for(int k = 1; k <= 20; k++)    {      for(int i = 1; i <= 20; i++){  for(int j = 1; j <= 20; j++)    {      if(dist[i][j] > dist[i][k] + dist[k][j])dist[i][j] = dist[i][k] + dist[k][j];    }}    }}int main(){  T = 0;  while(cin>>x)    {      memset(dist,10000,sizeof(dist));      while(x--){  cin>>j;  dist[1][j] = 1;  dist[j][1] = 1;}      for(int i = 2; i <= 19; i++ ){  cin>>x;  while(x--)    {      cin>>j;      dist[i][j] = dist[j][i] = 1;    }}      Floyd();      cin>>n;      cout<<"Test Set #"<<++T<<endl;      while(n--){  cin>>a>>b;  cout<<a<<" to "<<b<<": "<<dist[a][b]<<endl;}      cout<<endl;    }  return 0;}