Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=1869
Test instructions Analysis: a relatively simple shortest-path algorithm, the last only to determine the maximum distance of two points is greater than 7.
/*Six degree separation time limit:5000/1000 MS (java/others) Memory limit:32768/32768 K (java/others) total submission (s): 4992 Ac cepted Submission (s): 2010Problem Description1967 years, the famous American sociologist Stanley Milgram proposed a famous hypothesis called "The Small World phenomenon (small earth phenomenon)", The main idea is that any 2 people who do not have a single person can be linked together by a maximum of 6 people, that is, only 6 people, so his theory is also known as the "Six Degrees Separation" theory (six Degrees of separation). Although Milgram's theory has often been fulfilled, there have been many sociologists interested in it, but in more than 30 years, it has never been a rigorous proof, but a legendary hypothesis. Lele was quite interested in the theory, so he investigated n individuals in the HDU. He has got the relationship between them, now ask you to help him to verify the "Six Degrees of separation" is the establishment of it. Input This topic contains multiple sets of tests, please handle to the end of the file. For each set of tests, the first line contains two integers, n,m (0<n<100,0<m<200), representing the number of people in the HDU (0~n-1 numbers respectively), and their relationship. Next there are m lines, each line of two integers, a, B (0<=a,b<n), which indicates that the people who are numbered a in HDU are acquainted with each other. In addition to this m-group relationship, no other two people are acquainted. Output for each set of tests, if the data conforms to the "Six degree separation" theory, it Outputs "Yes" in one line, otherwise the output "No". Sample INPUT8-------------------------------------------- 0>*///Floyd algorithm Dp[i][j] = min (Dp[i][j], dp[i][k] + dp[k][j])#include <cstdio>#include<cstring>#include<iostream>using namespacestd;Const intMAXN = -+Ten;intD[MAXN][MAXN], N, m;#defineINF 10000001voidinit () { for(inti =0; i < MAXN; i++) for(intj =0; J < Maxn; J + +) if(i = = j) D[i][j] =0; ElseD[I][J] =INF;}intJudge () { for(inti =0; I < n; i++) for(intj =0; J < N; J + +) if(D[i][j] >7)return 0; return 1;}intMain () {intA, B; while(~SCANF ("%d%d", &n, &m)) {init (); for(inti =0; I < m; i++) {scanf ("%d%d", &a, &b); D[A][B]= D[b][a] =1; } for(intK =0; K < n; k++) for(inti =0; I < n; i++) for(intj =0; J < N; J + +) D[i][j]= Min (D[i][j], d[i][k] +D[k][j]); if(Judge ()) printf ("yes\n"); Elseprintf"no\n"); } return 0;}
HDU Six degree separation Floyd