博主自己写的java实现代码:
public class dijkstra {
public static void main(String[] args) {
int[][] map = new int[6][6];
initmap(map);
for(int i=0;i<6;i++)
{
for (int j = 0; j < 6; j++) {
System.out.print(String.format("%3d", map[i][j]));
}
System.out.println("");
}
//长度序列
int[] len = new int[6];
//是否计算过队列,贪心,1为确认过
int[] bool = new int[6];
// 初始化len数组,起点到自身距离为0,到其他点设为极大值
for (int i = 0; i < len.length; i++) {
len[i] = Integer.MAX_VALUE;
}
len[0] = 0;
for (int i = 0; i < bool.length; i++) {
//记录每行的最小值坐标
int minRow = -1;
int min = Integer.MAX_VALUE;
// 找到距离起点最近的未确定节点
//找此行,哪一列最小
for (int j = 0; j < map[i].length; j++) {
if (bool[j] == 0 && len[j] < min) {
minRow = j;
min = len[j];
}
}
if (-1 == minRow) {
break;
}
bool[minRow] = 1;
//minRow 转移到第几行计算总距离
for (int k = 0; k < map[minRow].length; k++) {
if (map[minRow][k] > 0 && bool[k] == 0 && len[minRow] + map[minRow][k] < len[k]) {
len[k] = len[minRow] + map[minRow][k];
}
}
for (int num : len) {
System.out.print(num + " ");
}
System.out.println("");
}
for(int i=0;i<6;i++)
{
System.out.print(String.format("%20d", bool[i]));
}
System.out.println();
for(int i=0;i<6;i++)
{
System.out.print(String.format("%20d", len[i]));
}
}
private static void initmap(int[][] map) {
map[0][1]=2;map[0][2]=3;map[0][3]=6;
map[1][0]=2;map[1][4]=4;map[1][5]=6;
map[2][0]=3;map[2][3]=2;
map[3][0]=6;map[3][2]=2;map[3][4]=1;map[3][5]=3;
map[4][1]=4;map[4][3]=1;
map[5][1]=6;map[5][3]=3;
}
}
//结果:0 2 3 5 6 8
博主自己找的java实现代码:
import java.util.ArrayDeque;
import java.util.Comparator;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
public class dijkstra {
static class node
{
int x; //节点编号
int lenth;//长度
public node(int x,int lenth) {
this.x=x;
this.lenth=lenth;
}
}
public static void main(String[] args) {
int[][] map = new int[6][6];//记录权值,顺便记录链接情况,可以考虑附加邻接表
initmap(map);//初始化
boolean bool[]=new boolean[6];//判断是否已经确定
int len[]=new int[6];//长度
for(int i=0;i<6;i++)
{
len[i]=Integer.MAX_VALUE;
}
Queue<node>q1=new PriorityQueue<node>(com);
len[0]=0;//从0这个点开始
q1.add(new node(0, 0));
int count=0;//计算执行了几次dijkstra
while (!q1.isEmpty()) {
node t1=q1.poll();
int index=t1.x;//节点编号
int length=t1.lenth;//节点当前点距离
bool[index]=true;//抛出的点确定
count++;//其实执行了6次就可以确定就不需要继续执行了 这句可有可无,有了减少计算次数
for(int i=0;i<map[index].length;i++)
{
if(map[index][i]>0&&!bool[i])
{
node node=new node(i, length+map[index][i]);
if(len[i]>node.lenth)//需要更新节点的时候更新节点并加入队列
{
len[i]=node.lenth;
q1.add(node);
}
}
}
}
for(int i=0;i<6;i++)
{
System.out.println(len[i]);
}
}
static Comparator<node>com=new Comparator<node>() {
public int compare(node o1, node o2) {
return o1.lenth-o2.lenth;
}
};
private static void initmap(int[][] map) {
map[0][1]=2;map[0][2]=3;map[0][3]=6;
map[1][0]=2;map[1][4]=4;map[1][5]=6;
map[2][0]=3;map[2][3]=2;
map[3][0]=6;map[3][2]=2;map[3][4]=1;map[3][5]=3;
map[4][1]=4;map[4][3]=1;
map[5][1]=6;map[5][3]=3;
}
}
//结果:0 2 3 5 6 8
下文内容转载自:https://blog.csdn.net/lbperfect123/article/details/84281300
Dijkstra算法算是贪心思想实现的,首先把起点到所有点的距离存下来找个最短的,然后松弛一次再找出最短的,所谓的松弛操作就是,遍历一遍看通过刚刚找到的距离最短的点作为中转站会不会更近,如果更近了就更新距离,这样把所有的点找遍之后就存下了起点到其他所有点的最短距离。
问题引入:
指定一个点(源点)到其余各个顶点的最短路径,也叫做“单源最短路径”。例如求下图中的1号顶点到2、3、4、5、6号顶点的最短路径。




下面我们来模拟一下:


这就是Dijkstra算法的基本思路:
接下来是代码:
已经把几个过程都封装成了基本模块:
-
#include<cstdio>
-
#include<cstring>
-
#include<algorithm>
-
#include<iostream>
-
#define Inf 0x3f3f3f3f
-
-
using
namespace
std;
-
-
int
map[
1005][
1005];
-
-
int vis[
1005],dis[
1005];
-
int n,m;
//n个点,m条边
-
-
void Init ()
-
{
-
memset(
map,Inf,
sizeof(
map));
-
for(
int i=
1;i<=n;i++)
-
{
-
map[i][i]=
0;
-
}
-
}
-
-
void Getmap()
-
{
-
int u,v,w;
-
for(
int t=
1;t<=m;t++)
-
{
-
scanf(
"%d%d%d",&u,&v,&w);
-
if(
map[u][v]>w)
-
{
-
map[u][v]=w;
-
map[v][u]=w;
-
}
-
}
-
-
}
-
-
void Dijkstra(int u)
-
{
-
memset(vis,
0,
sizeof(vis));
-
for(
int t=
1;t<=n;t++)
-
{
-
dis[t]=
map[u][t];
-
}
-
vis[u]=
1;
-
for(
int t=
1;t<n;t++)
-
{
-
int minn=Inf,temp;
-
for(
int i=
1;i<=n;i++)
-
{
-
if(!vis[i]&&dis[i]<minn)
-
{
-
minn=dis[i];
-
temp=i;
-
}
-
}
-
vis[temp]=
1;
-
for(
int i=
1;i<=n;i++)
-
{
-
if(
map[temp][i]+dis[temp]<dis[i])
-
{
-
dis[i]=
map[temp][i]+dis[temp];
-
}
-
}
-
}
-
-
}
-
-
int main()
-
{
-
-
scanf(
"%d%d",&m,&n);
-
Init();
-
Getmap();
-
Dijkstra(n);
-
printf(
"%d\n",dis[
1]);
-
-
-
return
0;
-
}
<li class="tool-item tool-active is-like "><a href="javascript:;"><svg class="icon" aria-hidden="true"> <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#csdnc-thumbsup"></use> </svg><span class="name">点赞</span> <span class="count">51</span> </a></li> <li class="tool-item tool-active is-collection "><a href="javascript:;" data-report-click="{"mod":"popu_824"}"><svg class="icon" aria-hidden="true"> <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-csdnc-Collection-G"></use> </svg><span class="name">收藏</span></a></li> <li class="tool-item tool-active is-share"><a href="javascript:;"><svg class="icon" aria-hidden="true"> <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#icon-csdnc-fenxiang"></use> </svg>分享</a></li> <!--打赏开始--> <!--打赏结束--> <li class="tool-item tool-more"> <a> <svg t="1575545411852" class="icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5717" xmlns:xlink="http://www.w3.org/1999/xlink" width="200" height="200"><defs><style type="text/css"></style></defs><path d="M179.176 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5718"></path><path d="M509.684 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5719"></path><path d="M846.175 499.222m-113.245 0a113.245 113.245 0 1 0 226.49 0 113.245 113.245 0 1 0-226.49 0Z" p-id="5720"></path></svg> </a> <ul class="more-box"> <li class="item"><a class="article-report">文章举报</a></li> </ul> </li> </ul> </div> </div> <div class="person-messagebox"> <div class="left-message"><a href="https://blog.csdn.net/lbperfect123"> <img src="https://profile.csdnimg.cn/6/D/A/3_lbperfect123" class="avatar_pic" username="lbperfect123"> <img src="https://g.csdnimg.cn/static/user-reg-year/2x/2.png" class="user-years"> </a></div> <div class="middle-message"> <div class="title"><span class="tit"><a href="https://blog.csdn.net/lbperfect123" data-report-click="{"mod":"popu_379"}" target="_blank">lbperfect123</a></span> </div> <div class="text"><span>发布了327 篇原创文章</span> · <span>获赞 354</span> · <span>访问量 10万+</span></div> </div> <div class="right-message"> <a href="https://im.csdn.net/im/main.html?userName=lbperfect123" target="_blank" class="btn btn-sm btn-red-hollow bt-button personal-letter">私信 </a> <a class="btn btn-sm bt-button personal-watch" data-report-click="{"mod":"popu_379"}">关注</a> </div> </div> </div>
Dijkstra算法详解
本文深入讲解Dijkstra算法的原理及其实现,通过实例演示如何利用贪心策略寻找单源最短路径,适用于加权图的最短路径问题解决。
17万+

被折叠的 条评论
为什么被折叠?



