标签:
Input
Output
Sample Input
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
Sample Output
Yes Yes
求任意两点之间的最短距离,用Floyd算法(复杂度为n的立方!!!)
推荐博客:http://developer.51cto.com/art/201403/433874.htm
#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <queue> #include <stack> #include <vector> #include <map> #include <cmath> #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define LL long long using namespace std; const int INF=0x3f3f3f3f; const int MX=100+10; int n,m,Map[MX][MX]; void init(){ for (int i=0;i<n;i++) for (int j=0;j<n;j++) if (i==j) Map[i][j]=0; else Map[i][j]=INF; } void Floyd(){ for (int k=0;k<n;k++){ for (int i=0;i<n;i++) for (int j=0;j<n;j++) if (Map[i][j]>Map[i][k]+Map[k][j]) Map[i][j]=Map[i][k]+Map[k][j]; } } int main(){ while (~scanf ("%d %d",&n,&m)){ init(); while (m--){ int u,v; scanf ("%d %d",&u,&v); if (Map[u][v]>1){ Map[u][v]=1; Map[v][u]=1; } } Floyd(); bool sign=true; for (int i=0;i<n&&sign;i++) for (int j=i+1;j<n&&sign;j++) if (Map[i][j]>7) sign=false; printf ("%s\n",sign?"Yes":"No"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/yusize/p/5750964.html