8 7 0 1 1 2 2 3 3 4 4 5 5 6 6 7 8 8 0 1 1 2 2 3 3 4 4 5 5 6 6 7 7 0
Yes Yes
#include <stdio.h>
#include <string.h>
#define MAX 110
#define INF 100000000
int graph[MAX][MAX];
bool floyd(int n)
{
for(int k = 0 ; k < n ; ++k)
{
for(int i = 0 ; i < n ; ++i)
{
for(int j = 0 ; j < n ; ++j)
{
if(graph[i][j]>graph[i][k]+graph[k][j])
{
graph[i][j]=graph[i][k]+graph[k][j];
}
}
}
}
for(int i = 0 ; i < n ; ++i)
{
for(int j = 0 ; j < n ; ++j)
{
if(graph[i][j]>7)
{
return false ;
}
}
}
return true ;
}
int main()
{
int m,n;
while(~scanf("%d%d",&n,&m))
{
for(int i = 0 ; i <= n ; ++i)
{
for(int j = 0 ; j <= i ; ++j)
{
graph[i][j] = graph[j][i] = INF ;
}
graph[i][i] = 0 ;
}
for(int i = 0 ; i < m ; ++i)
{
int x ,y ;
scanf("%d%d",&x,&y);
if(x == y)
continue ;
graph[x][y] = graph[y][x] = 1 ;
}
if(floyd(n))
{
puts("Yes");
}
else
{
puts("No");
}
}
return 0 ;
}原文:http://blog.csdn.net/lionel_d/article/details/43871437