Six Degrees separation hdu Hangzhou electric 1869 "Dijkstra algorithm"

Source: Internet
Author: User

http://acm.hdu.edu.cn/showproblem.php?pid=1869

Problem Description1967, the famous American sociologist Stanley Milgram proposed a famous hypothesis known as the "Small World phenomenon (small earth phenomenon)", to the effect that there are 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.
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 Input
8 70 11 22 33 44 55 66 78 80 11 22 33 44 55 66 77 0

Sample Output
Yesyes



#include <stdio.h> #include <string.h> #define INF 0x3f3f3f3fint map[110][110];int Dis[220];bool used[220]; int N;bool Dijkstra (int u) {int I,j;memset (used,0,sizeof (used)); memset (dis,inf,sizeof (DIS)); int Pos=u;for (i=0;i<n ; ++i)//For the first time dis assignment {dis[i]=map[u][i];} Dis[u]=0;used[u]=1;for (i=1;i<n;++i)//Find n-1 point {int min=inf;for (J=0;J&LT;N;++J) {if (!used[j]&&dis[j]< min) {min=dis[j];p os=j;}} if (min==inf| | DIS[POS]&GT;7) break;//as long as two people through the other person's still not connected can not be separated by six degrees used[pos]=1;dis[pos]=min;for (J=0;J&LT;N;++J)//To update the DIS array, also known as relaxation { if (!used[j]&&dis[j]>map[pos][j]+dis[pos]) {Dis[j]=map[pos][j]+dis[pos];}}} if (i<n) return 0;else return 1;} int main () {int m;int u,v;int i,j;while (~scanf ("%d%d", &n,&m)) {for (i=0;i<n;i++) for (j=0;j<n;j++) m Ap[i][j]=inf;while (m--) {scanf ("%d%d", &u,&v), if (u!=v)//u and V are equal map[u][v]=0; map[u][v]=map[v][u]=1;} int flag=0; for (I=0;i<n;++i) {if (!dijkstra (i))//each must be judged by {flag=1;break;}} if (flag) printf ("no\n"), Else printf ("yes\n");} return 0;} 


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

Six Degrees separation hdu Hangzhou electric 1869 "Dijkstra algorithm"

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.