Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 1869
Solution: Floyd algorithm
Floyd algorithm: Find the shortest path of A-> B. The path from a-> B does not care about two situations. 1) directly from a-> B; 2) from a after X points to B, that is, a-> X-> B. in this case, you only need to select the minimum value of the two conditions, that is, the shortest path of A-> B. That is, D (AB) = min (D (AB), D (ax) + d (XB )). The core code is as follows:
For (int K = 0; k <n; k ++)
For (INT I = 0; I <n; I ++)
For (Int J = 0; j <n; j ++)
D [I] [J] = D (Map [I] [k] + d [k] [J], d [I] [J]);
The following is my code:
#include <iostream>#include <algorithm>#include <cstdio>using namespace std;const int INF=0x3f3f3f;int map[205][205];int main(){ int n,m,a,b,flag; while(scanf("%d%d",&n,&m)!=EOF) { for(int i=0;i<n;i++) { for(int j=0;j<n;j++) map[i][j]=INF; map[i][i]=0; } for(int i=0;i<m;i++) { scanf("%d%d",&a,&b); map[a][b]=map[b][a]=1; } for(int k=0;k<n;k++) for(int i=0;i<n;i++) for(int j=0;j<n;j++) map[i][j]=min(map[i][k]+map[k][j],map[i][j]); flag=0; for(int i=0;i<n;i++) { for(int j=0;j<n;j++) if(map[i][j]>7) flag=1; if(flag) break; } if(flag) cout<<"No"<<endl; else cout<<"Yes"<<endl; } return 0;}