Dijskra算法

邻接表建图

声明

int h[N], e[M], w[M], ne[M], idx; //邻接表 N节点个数,M边个数
 
其中 
h[a] 指向a节点起点的邻接表列表的最后一个元素。
e[idx] 为当前idx编号的边指向的终点节点
w[idx] 为当前idx编号的边权重
ne 存储邻接表链表,当前值对应邻接表中下一个的地址,类似于值是指针。
 
初始化
idx = 0;
memset(h, -1, sizeof h);
 

邻接表构建

void add(int a, int b, int c) { //构建邻接表
    e[idx] = b;
    w[idx] = c;
    ne[idx] = h[a]; 
    h[a] = idx++;  //h[a] 一直指向a邻接表头插法起点,其实是最后一个,指针保留的方式也是向前
}
1. idx一直向前,如果a是第一次出现,则h[a]的值对应ne中位置即是起点。
2. 插入的方式是类似头插法,每次邻接表中的新元素出现,则插入邻接链表的第一个。也可以这样理解,是每次插到最后,让h[a]指向最后一个元素,遍历的时候倒着向前遍历。
3. 如果指向下一个为空时,指针值为-1.

邻接表建立

for(int i = h[vel]; i != -1; i = ne[i]) { 
    //TODO
}
 
i = ne[i] 模拟链表指针的next操作
h[vel] 指向vel链表的最后一个,遍历是从后往前的

Dijskra求最短路

转自:https://www.acwing.com/solution/content/38318/

一般模式:

#include<iostream>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 510, M = 100010;

int h[N], e[M], ne[M], w[M], idx;//邻接表存储图
int state[N];//state 记录是否找到了源点到该节点的最短距离
int dist[N];//dist 数组保存源点到其余各个节点的距离
int n, m;//图的节点个数和边数

void add(int a, int b, int c)//插入边
{
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}

void Dijkstra()
{
    memset(dist, 0x3f, sizeof(dist));//dist 数组的各个元素为无穷大
    dist[1] = 0;//源点到源点的距离为置为 0
    for (int i = 0; i < n; i++)
    {
        int t = -1;
        for (int j = 1; j <= n; j++)//遍历 dist 数组,找到没有确定最短路径的节点中距离源点最近的点t
        {
            if (!state[j] && (t == -1 || dist[j] < dist[t]))
                t = j;
        }

        state[t] = 1;//state[i] 置为 1。

        for (int j = h[t]; j != -1; j = ne[j])//遍历 t 所有可以到达的节点 i
        {
            int i = e[j];
            dist[i] = min(dist[i], dist[t] + w[j]);//更新 dist[j]
        }


    }
}

int main()
{
    memset(h, -1, sizeof(h));//邻接表初始化

    cin >> n >> m;
    while (m--)//读入 m 条边
    {
        int a, b, w;
        cin >> a >> b >> w;
        add(a, b, w);
    }

    Dijkstra();
    if (dist[n] != 0x3f3f3f3f)//如果dist[n]被更新了,则存在路径
        cout << dist[n];
    else
        cout << "-1";
}

优化版(常用):

#include <iostream>
#include <queue>
#include <cstring>
using namespace std;

const int N = 1005, M = 100;
typedef pair<int, int> PII;
bool vis[N];
int dist[N];
int e[N], ne[N], h[N], w[N], idx;
int n, m, a, b, c;

void add(int a, int b, int c)
{
    e[idx] = b; w[idx] = c; ne[idx] = h[a]; h[a] = idx++;
}

void dij()
{
    memset(dist, 0x3f, sizeof(dist));//dist 数组的各个元素为无穷大
    dist[1] = 0;
    priority_queue<PII, vector<PII>, greater<PII>> pq;
    pq.push({0, 1});

    while (pq.size()) 
    {
        auto t = pq.top();
        pq.pop();

        int ver = t.second, lens = t.first;
        // cout << "现在到了" << ver << "距离是" << lens << endl;
        if (vis[ver]) {
            // cout << ver << "已经来过了" << endl;
            continue;
        }
        vis[ver] = true;

        for (int j = h[ver]; j != -1; j = ne[j])
        {
            int i = e[j];
            if (dist[i] > dist[ver] + w[j])
                dist[i] = dist[ver] + w[j];
            pq.push({ dist[i], i});
            // cout << "入队的是 " << i << " 距离是" << dist[i] << endl;
        }
    }
}

int main()
{
    memset(h, -1, sizeof h);
    cin >> n >> m;
    for (int i = 1; i <= m; i++)
    {
        cin >> a >> b >> c;
        add(a, b, c);
    }
    dij();

    if (dist[n] == 0x3f)
        cout << -1;
    else
        cout << dist[n];

    return 0;
}

例题:蓝桥杯作物杂交

#include <iostream>
#include <cmath> 
#include <cstring>
#include <queue>
using namespace std;

const int N = 200005;

typedef pair<int, int> PII;
int e[N], ne[N], g[N], w[N], h[2010], idx;
int t[2010], b[2010];
int dist[2010];
int n, M, K, T;
bool vis[2010];
int ans = 0;

void add(int a, int b, int c)  // 建图
{
    // 类比w[],相当于边上多携带了一个值g[]
    e[idx] = c; w[idx] = max(t[a], t[b]); g[idx] = b; ne[idx] = h[a]; h[a] = idx++;
}

void dij()
{
    priority_queue <PII, vector<PII>, greater<PII>> pq;
    for (int i = 1; i <= M; i++) {
        dist[b[i]] = 0;
        pq.push({ 0 , b[i] });
    }

    while (pq.size())
    {
        auto t = pq.top();
        pq.pop();
        int ver = t.second;

        if (vis[ver]) continue;
        vis[ver] = true;

        for (int j = h[ver]; j != -1; j = ne[j]) // 遍历当前节点ver(包括携带的)可以到达的所有节点
        {
            int i = e[j];
            if (dist[i] > max(dist[ver], dist[g[j]]) + w[j])
                dist[i] = max(dist[ver], dist[g[j]]) + w[j];
            pq.push({ dist[i],  i });
        }
    }

}

int main()
{
    cin >> n >> M >> K >> T;
    for (int i = 1; i <= n; i++)
        cin >> t[i];
    for (int i = 1; i <= M; i++)
        cin >> b[i];
    int a, b, c;
    memset(h, -1, sizeof h);
    for (int i = 1; i <= K; i++)
    {
        cin >> a >> b >> c;
        add(a, b, c);
        add(b, a, c);
    }
    memset(dist, 0x3f, sizeof(dist));
    dij();
    cout << dist[T];

    return 0;
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值