hdoj-1869-Six degree separation (Dijkstra)

Source: Internet
Author: User

Six degree separationTime limit:5000/1000 MS (java/others) Memory limit:32768/32768 K (java/others) Total Submission (s): 5439 Accepted Submission (s): 2205

problem DescriptionIn 1967, Stanley Milgram, a famous American sociologist, proposed a famous hypothesis called "The Small World phenomenon (small worlds phenomenon)", to the effect that there were only 6 people among any 2 strangers, That is, only 6 people can connect them together, so his theory is also known as the "Six degree 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.  
InputThis 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.
 
Outputfor each set of tests, if the data conforms to the "Six degree separation" theory, Output "Yes" in one line, otherwise output "No".  
Sample Input
8 70 11 22 33 44 55 66 78 80 11 22 33 44 55 66 77 0
 
Sample Output
Yesyes
Problem Solving Ideas: is to judge whether the shortest path is <=7 similar to HDOJ-2066, it is a cyclic application of Dijkstra, once again stressed that the circular use of Dijkstra do not worry about the map array, because the map array is stored at the time of the smallest distance. Note to start at your own distance of 0 Code
#include <stdio.h> #include <string.h> #define MAX 9999int map[110][110];int ok[110];int now[110];int Main () {int N,m;int i,j,k;int nowlen,nowx;int a,b;int ans;while (scanf ("%d%d", &n,&m)!=eof) {for (i=0;i<n;i++) for (j    =0;j<n;j++) {if (i==j) map[i][j]=0;    else Map[i][j]=max; }for (i=0;i<m;i++) {scanf ("%d%d", &a,&b); map[a][b]=map[b][a]=1;}    for (i=0;i<n;i++) {memset (ok,0,sizeof (OK));    for (j=0;j<n;j++) Now[j]=max;    now[i]=0;    Ok[i]=1;    for (j=0;j<n;j++) {Nowlen=max;    for (k=0;k<n;k++) {if (!ok[k]&&nowlen>map[i][k]) {nowx=k;    NOWLEN=MAP[I][K];    }} Now[nowx]=nowlen;    Ok[nowx]=1;    for (k=0;k<n;k++) {if (!ok[k]&&map[i][k]>nowlen+map[nowx][k]) map[i][k]=nowlen+map[nowx][k]; }}}ans=0;for (i=0;i<n;i++) for (j=0;j<n;j++) if (map[i][j]>7) ans=1;if (ans==0) printf (" Yes\n "); else printf (" no\n ");} return 0;}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

hdoj-1869-Six degree separation (Dijkstra)

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.