HDU1869 six-degree separation Dijkstra algorithm

Source: Internet
Author: User
Tags bool

Six degree separation Time limit:5000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 11099 Accepted Submission (s): 4475


Problem Description in 1967, the famous American sociologist Stanley Milgram proposed a famous hypothesis known as the "Small World phenomenon (small earth phenomenon)", to the effect that No 2 strangers were separated by 6 people, 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 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
Author Linle
Source 2008 Hangzhou Electric Training Team Tryouts--warm-up


Problem Link: HDU1869 Six degree separation.

description of the problem : see above.

Problem Analysis :

This is a connectivity distance problem, which is solved using the Dijkstra algorithm.

For each node, the Dijkstra algorithm is used to calculate one time and then determine whether the distance is exceeded.

There should be other algorithms that can be implemented, and perhaps the dynamic programming algorithm works better.

Judging the connectivity, finding the diameter of the tree may also be a solution.

Program Description :

There are three main forms of graph representation, one is adjacency table, the other is adjacency matrix, and the third is the edge list. The adjacency matrix is not ideal for multiple nodes and fewer edges. It is a kind of dynamic storage using adjacency table to store graph, i.e. g[] in the program. The shortest distance from the single source (node s) to each node is stored in the array dist[]. Priority Queue Q is queued from small to large in terms of the weight of the edge, facilitating the shortest path calculation.

Since the problem is an unauthorized figure, if there is an edge between the two nodes, the distance between the two nodes is 1.


The AC C + + language program is as follows:

/* HDU1869 Six degree separation */#include <iostream> #include <vector> #include <queue> #include <cstdio> usin

G namespace Std;
const int INT_MAX2 = ((unsigned int) ( -1) >> 1);
const int MAXN = 200;

const int MAXDIST = 6 + 1;
    Edge struct _edge {int V, cost;

_edge (int v2, int c) {v=v2; cost=c;}};
    node struct _node {int u, cost;

    _node () {} _node (int u2, int l) {u=u2; cost=l;}
    BOOL operator< (const _node N) const {return cost > n.cost;

}
};
Vector<_edge> g[maxn+1];
int dist[maxn+1];

BOOL Visited[maxn+1];

    void Dijkstra (int start, int n) {priority_queue<_node> q;
        for (int i=0; i<=n; i++) {dist[i] = INT_MAX2;
    Visited[i] = false;

    } Dist[start] = 0;

    Q.push (_node (start, 0));
    _node F;
        while (!q.empty ()) {f = q.top ();

        Q.pop ();
        int u = f.u;

            if (!visited[u]) {Visited[u] = true;
            int len = G[u].size (); FoR (int i=0; i<len; i++) {int v2 = G[U][I].V;

                if (Visited[v2]) continue;
                int tempcost = G[u][i].cost;

                int nextdist = Dist[u] + tempcost;
                    if (Dist[v2] > Nextdist) {dist[v2] = nextdist;
                Q.push (_node (v2, dist[v2]));

    }}}}} int main () {int n, m, SRC, dest;
            input data, build diagram while (scanf ("%d%d", &n,&m)! = EOF && (n + m)) {for (int i=1; i<=m; i++) {

            scanf ("%d%d", &src, &dest);
            G[src].push_back (_edge (dest, 1));
        G[dest].push_back (_edge (SRC, 1));
        }//Starting from all nodes, the Dijkstra algorithm calculates whether the distance exceeds bool Ansflag = true;

            for (int i=0; Ansflag && i<n; i++) {//Dijkstra algorithm Dijkstra (i, n);
     Check distance, output result for (int j=0; j<n; J + +) {if (J! = i)               if (Dist[j] > MAXDIST) {ansflag = false;
                    Break }}} printf ("%s\n", Ansflag?

        "Yes": "No");
    Release storage for (int i=0; i<=n; i++) g[i].clear ();
} return 0; }


Other algorithms can be used to refer to the following links:

HDU-1869 six degree separation

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.