dijkstra和dijkstra堆优化模板

dijkstra和dijkstra堆优化模板

之前QAQ一直没有准备堆优化模板,本例以PAT A1003为例,整理dijkstra和dijkstra堆优化模板

Dijkstra

我们可以发现该篇幅找最小值部分是使用量一个for循环

#include <iostream>
#include <algorithm>
using namespace std;
int n, m, c1, c2;
int edge[510][510], weight[510], dis[510], num[510], w[510];
bool vis[510];
const int inf = 99999999;
int main() {
    scanf("%d%d%d%d", &n, &m, &c1, &c2);
    for(int i = 0; i < n; i++)
        scanf("%d", &weight[i]);
    fill(edge[0], edge[0] + 510 * 510, inf);
    fill(dis, dis + 510, inf);
    int a, b, c;
    for(int i = 0; i < m; i++){
        scanf("%d%d%d", &a, &b, &c);
        edge[a][b] = edge[b][a] = c;
    }
    dis[c1] = 0;
    w[c1] = weight[c1];
    num[c1] = 1;
    while(true) {
        int u = -1, minn = inf;
        for(int j = 0; j < n; j++){
            if(vis[j] == false && dis[j] < minn) {
                u = j;
                minn = dis[j];
            }
        }
        if(u == -1) break;
        vis[u] = true;
        for(int  v = 0; v < n; v++) {
            if(vis[v] == false && edge[u][v] != inf) {
                if(dis[u] + edge[u][v] < dis[v]) {
                    dis[v] = dis[u] + edge[u][v];
                    num[v] = num[u];
                    w[v] = w[u] + weight[v];
                }else if(dis[u] + edge[u][v] == dis[v]) {
                    num[v] = num[v] + num[u];
                    if(w[u] + weight[v] > w[v])
                        w[v] = w[u] + weight[v];
                }
            }
        }
    }
    printf("%d %d", num[c2], w[c2]);
    return 0;
}

dijkstra堆优化

我们在找最小值那部分进行替换成堆即可

#include <iostream>
#include <queue>
#include <algorithm>
using namespace std;
int n, m, c1, c2;
int edge[510][510], weight[510], dis[510], num[510], w[510];
bool vis[510];
const int inf = 99999999;
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<pair<int, int>>> q;
int main() {
    scanf("%d%d%d%d", &n, &m, &c1, &c2);
    for(int i = 0; i < n; i++)
        scanf("%d", &weight[i]);
    fill(edge[0], edge[0] + 510 * 510, inf);
    fill(dis, dis + 510, inf);
    int a, b, c;
    for(int i = 0; i < m; i++){
        scanf("%d%d%d", &a, &b, &c);
        edge[a][b] = edge[b][a] = c;
    }
    dis[c1] = 0;
    w[c1] = weight[c1];
    num[c1] = 1;
    q.push({0, c1});
    while(q.size()) {
        // 改为了弹出
        int u = q.top().second;
        q.pop();
        // 如果已经访问,这边记得进行continue操作
        if(vis[u]) continue;
        vis[u] = true;
        for(int  v = 0; v < n; v++) {
            if(vis[v] == false && edge[u][v] != inf) {
                if(dis[u] + edge[u][v] < dis[v]) {
                    dis[v] = dis[u] + edge[u][v];
                    num[v] = num[u];
                    w[v] = w[u] + weight[v];
                }else if(dis[u] + edge[u][v] == dis[v]) {
                    num[v] = num[v] + num[u];
                    if(w[u] + weight[v] > w[v])
                        w[v] = w[u] + weight[v];
                }
                q.push({dis[v], v});
            }
        }
    }
    printf("%d %d", num[c2], w[c2]);
    return 0;
}
上一篇:三十天挑战数据结构(11)图的最短路径之Dijkstra算法


下一篇:Dijkstra 算法