http://acm.hdu.edu.cn/showproblem.php?pid=1869
好吧我承认这道题运用到了Floyd Warshall算法,求任意两点之间的最小路径问题!相识权值为1,不相识权值为INF。
#include <stdio.h> #define MAXN 110 #define INF 100 int mat[MAXN][MAXN]; int main() { int n , m, i, j, k; int max; while( scanf("%d%d", &n, &m) != EOF) { //对存储矩阵mat初始化 for( i = 0; i < n; i++ ) for( j = 0; j < n; j++ ) mat[i][j] = INF; //获取矩阵mat的值 for( k = 0; k < m; k++ ) { scanf("%d%d", &i, &j); mat[i][j] = mat[j][i] = 1; } /*for( i = 0; i < n; i++ ) { for( j = 0; j < n; j++ ) printf( "%d ",mat[i][j] ); printf(" "); }*/ //printf( " " ); //floyd_warshall算法 for( k = 0; k < n; k++ ) for( i = 0; i < n; i++ ) for( j = 0; j < n; j++ ) if( mat[i][j] > mat[i][k] + mat[k][j] ) mat[i][j] = (mat[i][k] + mat[k][j]); /*for( i = 0; i < n; i++ ) { for( j = 0; j < n; j++ ) printf( "%d ",mat[i][j] ); printf(" "); }*/ //遍历mat数组,以辨别最大距离是否大于7 max = 0; for( i = 0; i < n; i++ ) for( j = 0; j < n; j++ ) if( max < mat[i][j] ) max = mat[i][j]; if( max <= 7 ) printf("Yes "); else printf("No "); } return 0; }