简单floyd算法应用,适合刚学floyd练手(比如我)
#include#include #define INF (1<<20)#define NUM 101#define MIN(a,b) ((a)<(b)?(a):(b))#define MAX(a,b) ((a)>(b)?(a):(b))int num;int map[NUM][NUM];int min,last,minnum;void floyd(){ for(int k=1;k<=num;++k) for(int i=1;i<=num;++i) for(int j=1;j<=num;++j) map[i][j]=MIN(map[i][k]+map[k][j],map[i][j]);}int main(){ while(scanf("%d",&num),num) { for(int i=1;i<=num;++i) for(int j=1;j<=num;++j) map[i][j]=(i==j?0:INF); for(int i=1,n;i<=num;++i) { scanf("%d",&n); for(int j=1,a,b;j<=n;++j) { scanf("%d %d",&a,&b); map[i][a]=b; } } floyd(); min=INF; for(int i=1;i<=num;++i) { last=0; for(int j=1;j<=num;++j) last=MAX(last,map[i][j]); if(min>last) { minnum=i; min=last; } } if(min!=INF) printf("%d %d\n",minnum,min); else printf("disjoint\n"); } return 0;}