
分析
很明显:单源最短路径 + 没有负权边 = dijkstra
1.存图
2.准备两个数组
dis[]:更新源点到各个点的距离
vis[]:标记是否访问
3.从源点开始,更新源点到与其邻接的点的距离,每次选出dis[]min且未访问的点进行重复上述步骤
代码
两种实现方法:
1.链式前向星存图 + prioroty_queue
注意:优先队列默认大根堆,故要重载 < ;且优先队列按优先级排序,对于 < 而言,a < b为true的意思是,右边的优先级大于左边,故应为return a > b;
#include<iostream>
#include<cstring>
#include<queue>
using namespace std;
typedef long long ll;
const int N = 1010,M = 10010;
struct Node{
int u,dis;
//重载 <(优先队列默认大根堆)
bool operator < (const Node &x)const
{
return dis > x.dis;
}
};
//链式前向星
struct edges{
int to;
int ne;
int w;
}e[M*2]

最低0.47元/天 解锁文章
1369

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



