比较简单的算法:但是当点太多需要剪枝,不然很耗时
void Floyd() { for(int k=0;k<n;++k) for(int i=0;i<n;++i) for(int j=0;j<n;++j) dj[i][j] = min(dj[i][j],dj[i][k]+dj[k][j]); }
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define INF (1<<29) #define N (110) int dj[N][N]; int n,m; void Floyd() { for(int k=0;k<n;++k) for(int i=0;i<n;++i) for(int j=0;j<n;++j) dj[i][j] = min(dj[i][j],dj[i][k]+dj[k][j]); } int main() { while(~scanf("%d%d",&n,&m)) { for(int i=0;i<N;++i) for(int j=0;j<N;++j) { if(i != j) dj[i][j] = INF; else dj[i][j] = 0; } for(int i=0;i<m;++i) { int a,b; scanf("%d%d",&a,&b); if(a != b) dj[a][b] = dj[b][a] = 1; } Floyd(); bool flag = true; for(int i=0;i<n;++i) for(int j=0;j<n;++j) if(dj[i][j] > 7) { flag =false; break; } if(flag) printf("Yes\n"); else printf("No\n"); } return 0; }
时间: 2024-11-01 19:53:10