dijkstra算法

dijkstar算法解决的是对有向图求起点到任何一个起点的最短的距离。

将所有的点分为两个集合A,B,集合A代表的点已经和起点连起来的点,B集合代表的是还没有和A集合中的点连起来的点。 

从起点开始,建立队列,先将起点放入队列,并将起点放入集合A,s[i][j]记录的是从i到j的距离,v[i]代表的是从起点到点i的最小的距离

1,当队列不空是取出队列的点为i,如果为空是结束。

2、以此取出点i,遍历所有与i有可以建立关系的点j,如果v[j]>s[i][j]+v[i](即代表可以从起点到i点然后再到j点是当前从起点到j点的最短值的),v[j]=s[i][j]+v[i],点j进队,然后再依次遍历集合A中的点(用k表示),如果v[k]>s[j][k]+v[j],就V[k]=s[j][k]+v[j],再将点j放入集合A

3、重复上面两个步骤。

因为对每个点,程序都是取的是最小值,所以在当队列完成后v[i]就代表从起点开始到i点的最小值了!

本人嘴比较笨,现在画个图便于理解

  dijkstra算法_第1张图片

其中圆圈代表的是点,方框代表的是权值!

首先将给点的权值都附值为最大! 下面是操作步骤以及结果

 步骤

1

2

3

4

5

6

1                       

0                         

无穷               

无穷                  

无穷                     

无穷                        

无穷                      

2

0

2

5

无穷

无穷

无穷

3

0

2

5

7

无穷

无穷

4

0

2

5

7

11

无穷

5

0

2

5

7

9

10

6

0

2

5

7

9

10

7

0

2

5

7

9

10

Description】
Shining Knight is the embodiment of justice and he has a very sharp sword can even cleave
wall. Many bad guys are dead on his sword.
One day,
two evil sorcerer cgangee and Jackchess decided to give him some color
to see.
So them kidnapped Shining Knight's beloved girl--Miss Ice! Them built a M x N
maze with magic and shut her up in it.
Shining Knight arrives at the maze entrance immediately. He can reach any adjacent empty
square of four directions -- up, down, left, and right in 1 second. Or cleave one adjacent wall in 3
seconds, namely, trun it into empty square. It's the time to save his goddess! Notice: Shining
Knight won't leave the maze before he find Miss Ice.
【Input】
The input consists of blocks of lines. There is a blank line between two blocks.
The first line of each block contains two positive integers M <= 20 and N <= 20
separated by one space. In each of the next M lines there is a string of length N contents
O and #.
O represents empty squares. # means a wall.
At last, the location of Miss Ice, ( x, y ).
1 <= x <= M, 1 <= y <= N.
(Shining Knight always starts at coordinate ( 1, 1 ). Both Shining and Ice's location
guarantee to not be a wall.)
【Output】
The least amount of time Shining Knight takes to save his goddess in one line.
Sample Input
3 5
O####
#####
#O#O#
3  4

Sample Output
14

code

 
 
#include<iostream>
#include<queue>
using namespace std;
typedef struct fun
{
      int x,y;
}r;//用来记录进对的坐标的
fun qu[30000];
int a[55][55],lin,len;//数组记录的是最小的值吧; 
int sign[4][2]={0,1,0,-1,1,0,-1,0};
char s[55][55];
bool vis[55][55];
void dijkstra()
{
	int i,w,k,t,x,y;//w指的是权值的
	a[0][0]=0;
	queue<int>q;//建立一个队列的
	k=0;//记录的结构体中的东西的
	qu[k].x=0;qu[k].y=0;
	vis[0][0]=true;
	q.push(k);
	while(q.empty()==false)//不空的时候
	{
        t=q.front();
		q.pop();
		vis[qu[t].x][qu[t].y]=false;
		for(i=0; i<4; i++)
		{
			x=qu[t].x+sign[i][0];
			y=qu[t].y+sign[i][1];//上下标的
			if(x<0 || x>=lin || y<0 || y>=len)
				continue;
            if(s[x][y]=='#')
				w=4;
			else
				w=1;
			if(a[x][y]>w+a[qu[t].x][qu[t].y])
			{
				a[x][y]=w+a[qu[t].x][qu[t].y];
				if(vis[x][y]==false)
				{
				  k++;
				  qu[k].x=x;
				  qu[k].y=y;
				  q.push(k);
				  vis[x][y]=true;
				}
			}
		}

	}

}
int main()
{
    
	int i,j;
	cin>>lin>>len;
	for(i=0; i<lin; i++)//都是从0开始吧
	{
		scanf("%s",s[i]);
		for(j=0; j<len; j++)
		{
			a[i][j]=INT_MAX; 
			vis[i][j]=false;
		}
	}
	dijkstra();
	int m,n;
	cin>>m>>n;
	cout<<a[m-1][n-1]<<endl;
	return 0;
}
 
 

你可能感兴趣的:(dijkstra算法)