poj 1258 MST模板题(用PRIM+堆实现)

题意:求图的MST。

思路:PRIM算法,其中找最小边用priority_queue实现。注意不加堆的Prim 算法适用于密集图(O(V^2)),加堆的适用于稀疏图O(ElogV)。

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <cstdlib>
using namespace std;
#define N 105
struct edge{
    int x,y,next,w;
    bool operator<(const struct edge &b)const{
        return w>b.w;
    }
}e[N*N*2];
int first[N],top,n,used[N];
priority_queue<struct edge> h;
void add(int x,int y,int w){
    e[top].x = x;
    e[top].w = w;
    e[top].y = y;
    e[top].next = first[x];
    first[x] = top++;
}
int prim(){
    int i,j,res=0;
    used[1] = 1;//点1作为第一个加入的点
    for(i = first[1];i!=-1;i=e[i].next)
        h.push(e[i]);
    for(i = 1;i<n;){
        struct edge now = h.top();
        h.pop();
        if(used[now.y])//如果这条边的两个端点已经加入了,那么这条边不能用
            continue;
        used[now.y] = 1;
        res += now.w;
        for(j = first[now.y];j!=-1;j=e[j].next)//这个地方也可以像不加堆写法那样弄一个dis数组,对未加入MST的点仅当其邻接的最小边更新时才加入
            if(!used[e[j].y])
                h.push(e[j]);
        i++;
    }
    return res;
}
int main(){
    while(scanf("%d",&n)!=EOF){
        int i,j,w;
        top = 0;
        while(!h.empty())
            h.pop();
        memset(first, -1, sizeof(first));
        memset(used, 0, sizeof(used));
        for(i = 1;i<=n;i++)
            for(j = 1;j<=n;j++){
                scanf("%d",&w);
                if(i!=j)
                    add(i,j,w);
            }
        printf("%d\n",prim());
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值