标签:
给你20个城市的相邻关系,求给定任意两个城市的最短距离
求任意两个城市最短距离,就是用floyd算法,我脑残忘记了k是写在最外层的。
#include<stdio.h> #include<algorithm> #define N 22 using namespace std; int n,x,a,b,test,d[N][N],from,to; void read() { for(int j=1; j<=x; j++) { scanf("%d",&b); d[a][b]=d[b][a]=1; } } int main() { while(~scanf("%d",&x)) { test++; for(int i=1; i<=20; i++)for(int j=1; j<=20; j++)d[i][j]=50; a=1;read(); for(a=2; a<20; a++) { scanf("%d",&x); read(); } for(int k=1; k<=n; k++)//k写在外面 for(int i=1; i<=n; i++) for(int j=1; j<=n; j++) d[i][j]=min(d[i][j],d[i][k]+d[k][j]); scanf("%d",&n); printf("Test Set #%d\n",test); for(int i=1; i<=n; i++) { scanf("%d%d",&from,&to); printf("%d to %d: %d\n",from,to,d[from][to]); } printf("\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/flipped/p/5188499.html