模板:int dijkstra(int v) {
dist[v] = 0;
for (int i = 1; i <= n; i++) {
int t = -1;
for (int j = 1; j <= n; j++)
{
if (!visit[j] && dist[t] < dist[j])
t = j;
}
for (int i = 1; i <= n; i++)
dist[i] = min(dist[i],dist[i]+a[t][i]);
visit[t] = true;
}
if (dist[n] = 0x3f3f3f3f)return -1;
return dist[n];
}
堆优化后的
树与图的存储
树是一种特殊的图,与图的存储方式相同。
对于无向图中的边ab,存储两条有向边a->b, b->a。
因此我们可以只考虑有向图的存储。
(1) 邻接矩阵:g[a][b] 存储边a->b
(2) 邻接表:
// 对于每个点k,开一个单链表,存储k所有可以走到的点。h[k]存储这个单链表的头结点
int h[N], e[N], ne[N], idx;
// 添加一条边a->b
void add(int a, int b)
{
e[idx] = b, ne[idx] = h[a], h[a] = idx ++ ;
}
// 初始化
idx = 0;
memset(h, -1, sizeof h);
作者:yxc
链接:https://www.acwing.com/blog/content/405/
来源:AcWing
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。
-----------------fengexian----------------------------------------------------------------------------------------
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
const int N = 2010, M = 20010;
typedef pair<int, int> PII;
int n, m,tot=1;
bool st[N];
int dist[N];
int head[N], w[M], ne[M], idx;
struct edge {
int to, next, w;
}e[N*4];
void add(int a, int b, int c)
{
e[tot].to = b;
e[tot].w = c;
e[tot].next = head[a];
head[a] = tot++;
}
int dijkstra()
{
memset(dist, 0x3f, sizeof dist);
dist[1] = 0;
priority_queue<PII, vector<PII>, greater<PII> > q;
q.push({ 0, 1 });
while (q.size())
{
PII t = q.top();
q.pop();
int u = t.second, distance = t.first;
if (st[u]) continue;
st[u] = true;
for (int i = head[u]; i; i = e[i].next)
{
int j = e[i].to, w = e[i].w;
if (dist[j] > distance +w)
{
dist[j] = dist[u] + w;
q.push({ dist[j], j });
}
}
}
if (dist[n] == 0x3f3f3f3f) return -1;
return dist[n];
}
int main()
{
cin >> n >> m;
memset(e, 0, sizeof(e));
memset(st, false, sizeof(st));
while (m--)
{
int a, b, c;
cin >> a >> b >> c;
add(a, b, c), add(b, a, c);
}
cout << dijkstra() << endl;
return 0;
}
---------------------分割线——————————————————————————
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
using namespace std;
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > >q;
int fir[500001], to[500001], val[500001], nex[500001], n, m, s, u, v, w, cnt;
const int inf = 2147483647;
long long dis[10001];
bool book[100001];
void add_edge(int a, int b, int c)
{
cnt++;
to[cnt] = b;
val[cnt] = c;
nex[cnt] = fir[a];
fir[a] = cnt;
}
void dijkstra(int v) {
dis[s] = 0;//初始化dis数组
q.push(make_pair(0, s));
while (q.size())//当堆中还有元素
{
int x = q.top().second;
q.pop();
if (book[x]) continue;
book[x] = 1;//标记一下
for (int i = fir[x]; i; i = nex[i])
{
int distance = val[i],d=to[i];
if (dis[d] > dis[x] + distance)
{
dis[d] = dis[x] + distance;
q.push(make_pair(dis[d], d));
}
}
}
}
int main()
{
cin >> n >> m >> s;
for (int i = 1; i <= m; i++)
{
cin >> u >> v >> w;
add_edge(u, v, w);
}
for (int i = 1; i <= n; i++)
dis[i] = inf;
dijkstra(s);
for (int i = 1; i <= n; i++)
cout << dis[i] << " ";
return 0;
}
P3371 【模板】单源最短路径(弱化版) - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
spfa
#include<iostream>
#include<vector>
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
const int inf = 0x3f3f3f3f;
int n, m;
struct node
{
int next;
int to, dis;
}s[3200000];
int head[320000];
int cnt;
void add_edge(int from, int to, int dis)
{
s[++cnt].next = head[from];
s[cnt].to = to;;
s[cnt].dis = dis;
head[from] = cnt;
}
int dis[250000];
int vis[250000];
void spfa()
{
memset(dis, inf, sizeof(dis));
memset(vis, 0, sizeof(vis));
queue<int> q;
dis[1] = 0; vis[1] = 1;
q.push(1);
while (!q.empty()) {
int u = q.front();
q.pop();
vis[u] = 0;
for (int i = head[u]; i; i = s[i].next) {
int v = s[i].to, w = s[i].dis;
if (dis[v] > dis[u] + w) {
dis[v] = dis[u] + w;
if (vis[v] == 0) {
vis[v] = 1;
q.push(v);
}
}
}
}
}
int main()
{
cin >> n >> m;
memset(head, 0, sizeof(head));
for (int i = 0; i < m; i++) {
int a, b, c;
cin >> a >> b >> c;
add_edge(a, b, c);
add_edge(b, a, c);
}
spfa();
printf("%d\n", dis[n]);
return 0;
}
spfa利用边所以可以重复寻找边修改边的vis访问数组
(65条消息) 最短路算法总结(超详细~)_wmy0217_的博客-CSDN博客_最短路算法
堆优化的
#include<iostream>
#include<queue>
#include<string.h>
#include<vector>
using namespace std;
typedef pair<int, int> pii;
int fr[100010], to[200010], nex[200010], v[200010], tl, d[100010];
bool b[100010]; int n, m, x, y, z, s;
void add(int x, int y, int w) {
to[++tl] = y;
v[tl] = w;
nex[tl] = fr[x];
fr[x] = tl;
}
priority_queue< pair<int, int> ,vector<pii>,greater<pii>> q;
void dijkstra(int u) {
memset(d, 0x3f3f3f, sizeof(d));
memset(b, 0, sizeof(b));
d[u] = 0;
q.push(make_pair(0, u));
while (!q.empty()) {
int x = q.top().second;
q.pop();
if (b[x]) continue;
b[x] = 1;
for (int i = fr[x]; i; i = nex[i]) {
int y = to[i], l = v[i];
if (d[y] > d[x] + l) {
d[y] = d[x] + l;
q.push(make_pair(d[y], y));
}
}
}
}
int main() {
cin >> n >> m >> s;
for (int i = 1; i <= m; i++) {
cin >> x >> y >> z;
add(x, y, z);
}
dijkstra(s);
for (int i = 1; i <= n; i++)
printf("%d ", d[i]);
return 0;
}