本文转载自: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>