堆优化版本的Prim

  • prim和dijkstra每轮找最小边的松弛操作其实是同源的,因而受dijkstra堆优化的启发,那么prim也可以采用小根堆进行优化。
  • 时间复杂度也由 O ( n 2 ) O(n^2) O(n2)降为 O ( n l o g n ) O(nlogn) O(nlogn)。

测试一下吧:原题链接

cpp 复制代码
#include <iostream>
#include <cstring>
#include <vector>
#include <queue>
using namespace std;
typedef int VertexType;
typedef int Info;
typedef pair<int,int> PII;

const int N = 110;

// 书面形式的邻接表
typedef struct ArcNode{
    int adjvex;
    Info weight;
    struct ArcNode* nextarc;
}ArcNode;
typedef struct VNode{
    VertexType data;        // 这里  结点编号就是结点表的下标  一一映射
    ArcNode* firstarc;
}VNode, AdjList[N];
typedef struct ALGraph{
    AdjList vertices;
    int vexnum, arcnum;
    ALGraph(){for(int i = 0;i < N;i ++)         vertices[i].firstarc = nullptr;}
}ALGraph;

int prim_with_heap(ALGraph& G){
    int sum = 0;
    priority_queue<PII, vector<PII>, greater<PII>> heap;
    int dist[N];
    bool st[N];
    memset(dist, 0x3f, sizeof dist);
    memset(st, 0, sizeof st);
    
    dist[1] = 0;
    heap.push({0, 1});
    while(heap.size()){
        PII t = heap.top();
        heap.pop();
        int vex = t.second, distance = t.first;
        if(st[vex])     continue;
        st[vex] = true;
        sum += distance;
        for(ArcNode* parc = G.vertices[vex].firstarc;parc;parc = parc -> nextarc)
            if((parc -> weight) < dist[parc -> adjvex]){
                dist[parc -> adjvex] = parc -> weight;
                heap.push({parc -> weight, parc -> adjvex});
            }
    }
    return sum;
}

void add(ALGraph& G, VertexType a, VertexType b, Info w){   // a -> b
    VNode* u = &G.vertices[a];
    ArcNode* newarc = new ArcNode;
    newarc -> adjvex = b;
    newarc -> weight = w;
    newarc -> nextarc = u -> firstarc;
    u -> firstarc = newarc;     // 头插法
    G.arcnum ++;
}

int main(){
    ALGraph g;
    cin >> g.vexnum;
    for(int i = 1;i <= g.vexnum;i ++)
        for(int j = 1;j <= g.vexnum;j ++){
            int w;
            cin >> w;
            add(g, i, j, w);
        }
    int sum = prim_with_heap(g);
    cout << sum << endl;
    return 0;
}
相关推荐
TracyCoder1232 小时前
LeetCode Hot100(15/100)——54. 螺旋矩阵
算法·leetcode·矩阵
u0109272713 小时前
C++中的策略模式变体
开发语言·c++·算法
2501_941837263 小时前
停车场车辆检测与识别系统-YOLOv26算法改进与应用分析
算法·yolo
Aevget4 小时前
MFC扩展库BCGControlBar Pro v37.2新版亮点:控件功能进一步升级
c++·mfc·界面控件
探序基因4 小时前
单细胞Seurat数据结构修改分群信息
数据结构
六义义5 小时前
java基础十二
java·数据结构·算法
四维碎片5 小时前
QSettings + INI 笔记
笔记·qt·算法
Tansmjs5 小时前
C++与GPU计算(CUDA)
开发语言·c++·算法
独自破碎E6 小时前
【优先级队列】主持人调度(二)
算法
weixin_445476686 小时前
leetCode每日一题——边反转的最小成本
算法·leetcode·职场和发展