Shortest path Dijkstra algorithm detailed: Dijkstra (graphic) (Detailed __dijkstra

Source: Internet
Author: User

I small white, if there is not the right place to write, but also please point out that the common progress of learning.

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- --------

Talk about the shortest way, for me the most favorite algorithm but Floyd, no brain, simple, good understanding. But for the OJ, the complexity of Floyd is often faced with the possibility of timeouts, which makes the Dijkstra algorithm of the same good understanding more popular. Many of the most short-circuit aspects of the topic, I personally think or see Dijkstra the problem is more, so I here to explain the shortest short-circuit the first algorithm is decided to first say Dijkstra.


The Town Map ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Dixtra. Dixtra May 11, 1930 was born in Rotterdam, the Netherlands, an intellectual family, ranked third among siblings 4 people. His father was a chemist and inventor who served as chairman of the Dutch Chemical Council. His mother is a mathematician. He succeeded in designing and implementing an efficient algorithm for finding a shortest path between two locations with obstructions, this algorithm, named "Dixtra algorithm", solves a very critical problem in robotics, that is, the motion path planning problem, which is still widely used today, is considered to be using the "greedy method" (greedy method) to design a successful example of an algorithm.


Since said is the shortest way, we certainly have to have greedy thought, here Dixtra perfect to do this. In the algorithm, how to use this greedy thought of it. Here we correspond to a graph. (The picture is not good-looking, the light spit Groove T T.) )



We assume that 2 is the starting point, want to go to the end of 4, obviously we have two ways to go, and clearly, walk 2-> 1-> 4 This path is the shortest. We don't want to go 2->4 this way. We pass 1 this point, can complicate the path from the 2->4 (more one step) but can shorten the path time consuming operation, we understood for the relaxation operation, we complete the Dijkstra the entire algorithm process, is the unceasing in the relaxation process. We want to take a short path, then we must go a lot of detours--*

Here we have a map of the complete algorithm to understand:

We are here to define the number of the diagram as:

1 2 3

4 5 6

7 8 9

Figure 1: The initialized diagram, which contains the weights of the edges (time consuming). (The graph here is a direction diagram).

Figure 2: Determine the starting point and then walk to the point where you can go directly, recording the estimated value at this point: 2 6 9.

Figure 3: Find the nearest point to the starting point, which is just east of the point, at which time we consume a weighted value of 2. And then we do a relaxation operation, from the point of origin to its south-east point directly to the weight of the value of 6, but by the point we just selected, we found a way closer to this point, so this time we said that the value of the weights from the start to the south-east point changed from 6 to 5. This time we have finished the first relaxation operation.

Figure 4: It is still the nearest point to find the starting point. And then slack. We found that this time from the beginning to its south-east point of the cost of the weight from 5 to 4. This time we finished the second slack.

The following way: Select the nearest Point v. Then the relaxation operation is done by Point v. We found that we were able to relax the weights by increasing the complexity of the way we went to the destination, making the weight less expensive.

Readers please run the top of the diagram, the approximate algorithm thinking is mastered.

And then we'll go along with the code and the diagram:

void Dij ()//We start here with the number 1th code point. Our d[] represents the weights needed from the start point to this point. W[a][b] Represents the weight of point A to the side of B.
{
	int i,j,k,v,tmp;
	memset (Vis) (vis,0,sizeof);
	for (i=1;i<=n;i++)
	    d[i]=w[1][i];//corresponding diagram is not difficult to understand, for the initialization of the beginning of
	d[1]=0;
	Vis[1]=1;
	for (i=1;i<=n;i++)//control the number of connection points, such as the above figure, nine points, cycle nine times.
	{
		tmp=n;//here N represents infinity. Which is the picture on the top.
		for (j=1;j<=n;j++)
		{
			if (Tmp>d[j]&&!vis[j])
			{
				tmp=d[j];
				V=j
			}
		} Every time we find the nearest point v vis[v]=1 from the beginning
		;
		for (k=1;k<=n;k++)//Then slack operation. <pre name= "code" class= "CPP" > our d[here] means to emphasize the meaning of the weights needed from the starting point to this point.
{if (!vis[k]) d[k]=min (D[k],d[v]+w[v][k]);}}

The graph above is the effect of the relaxation operation. Among them I added two blue Midway points, indicating that our three points are not necessarily three points like the original figure, and they have complex journeys. In this way we can easily see how the relaxation operation of the ~.

The answer to the question of whether the Dijkstra can be solved is clear: yes, because the graph is a special one, and it can be understood as a bidirectional graph. Here we practice the HDU 2544:

Shortest Way Time limit:5000/1000 MS (java/others) Memory limit:32768/32768 K (java/others) total submission (s): 46167 accep Ted Submission (s): 20348

Problem Description every year in the school game, all the students who entered the finals will get a very beautiful T-shirt. But when our staff hundreds clothes from the store back to the game, they are very tired. So now they want to find the shortest route from the store to the arena, can you help them.
Input input includes multiple sets of data. The first row of each group of data is two integers n, M (n<=100,m<=10000), n means that there are several junctions in Chengdu Street, marking the intersection of 1 is the location of the store, marking the intersection of N is the venue, M said there are several roads in Chengdu. N=m=0 indicates the end of the input. Next to line m, each row consists of 3 integers a,b,c (1<=a,b<=n,1<=c<=1000), which means that there is a road between junction A and B, and our staff need C minutes to walk this way. Enter a route that guarantees the presence of at least 1 stores to the arena.
Output a row for each group of inputs, indicating the shortest time a worker walks from the store to the arena
Sample Input

2 1 1 2 3 3 3 1 2 5 2 3 5 3 1 2 0-0
Sample Output
3 2


#include <stdio.h> #include <string.h> #include <iostream> using namespace std;
#define N 0x1f1f1f1f int w[151][151];
int d[155];
int ans,vis[151];
int n,m;
    void Dij () {int i,j,k,v,tmp;
    memset (Vis) (vis,0,sizeof);
    for (i=1;i<=n;i++) d[i]=w[1][i];
    d[1]=0;
    Vis[1]=1;
        for (i=1;i<=n;i++) {tmp=n;
                for (j=1;j<=n;j++) {if (Tmp>d[j]&&!vis[j]) {tmp=d[j];
            V=j;
        }} vis[v]=1;
        for (k=1;k<=n;k++) {if (!vis[k]) d[k]=min (d[k],d[v]+w[v][k));
        {()} int main () {while (~scanf ("%d%d", &n,&m)) {if (n==0&&m==0) break;
            for (int i=1;i<=n;i++) {for (int j=1;j<=n;j++) {w[i][j]=0x1f1f1f1f;
            } for (int i=0;i<m;i++) {int a,b,dis; scanf"%d%d%d", &a,&b,&dis);
        if (W[a][b]>dis) W[a][b]=w[b][a]=dis;
        } dij ();
    printf ("%d\n", D[n]); }
}
--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- --------























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.