Hdu1869 six-degree Separation

Source: Internet
Author: User
 

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;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.