最小生成樹prim(優先隊列優化)算法+Kruskal算法

最小生成樹

1.prim算法

算法思想:從任意一點出發,記錄點的最小權值,每一次將最小邊的結點標記一下,直到所有的點都被加到樹裏面。優先隊列將邊按從小到大的順序排列,隊首爲最小的邊。
板子題:HUD-1863

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
using namespace std;
const int N=1e5;
int head[N];
int cnt=0;
struct node{
    int to,next,val;
}mp[N];
struct nod{               //優先隊列排序;
    int pos,val;
    bool friend operator <(nod a, nod b)
    {
        return a.val>b.val;
    }
};
void add(int x, int y, int val)  //鏈式向前星建邊;
{
    mp[cnt].to=y;
    mp[cnt].val=val;
    mp[cnt].next=head[x];
    head[x]=cnt++;
}
int prime(int m)
{
    bool p[200];
    priority_queue<nod >q;
    memset(p,false,sizeof(p));   //初始化標記數組;
    int i,to,val,ans=0,tot=0;
    q.push(nod{1,0});            //壓入起點值;
    while(!q.empty()){
        nod are=q.top();
        q.pop();
        int u=are.pos; 
        if(p[u]) continue;
        p[u]=true;
        ans+=are.val;                //權值加入答案;
        ++tot;                      //記錄個數;
        for(i=head[u];~i;i=mp[i].next){
            to=mp[i].to;
            val=mp[i].val;
            q.push(nod{to,val});
        }
    }
    if(tot==m) return ans;
    return 0;
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF&&n){
        int x,y,val;
        cnt=0;
        memset(head,-1,sizeof(head));
        for(int i=0;i<n;++i){
            scanf("%d%d%d",&x,&y,&val);
            add(x,y,val);
            add(y,x,val);
        }
        int ans=prime(m);
        if(ans){
            printf("%d\n",ans);
        }
        else{
            printf("?\n");
        }
    }
    return 0;
}

2.Kruskal算法;

算法思想:
1.先對所有的邊排序;
2.依次從小到大將邊加入到樹中,在維護結點的時候運用並查集判斷是否已經在相同集合中,若不再相同結點中則將邊加入到樹中;

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <queue>
#include <vector>
#include <map>
using namespace std;
const int N=1e3;
int pre[N];
struct node{
    int x,y,val;
}mp[N*10];
bool cpu(node a, node b)    //對邊排序;
{
    return a.val<b.val;
}
int find(int x)           //並查集查找根節點+路徑壓縮;
{
    if(pre[x]==x) return pre[x];
    return pre[x]=find(pre[x]);
}
int kruskal(int n, int m)
{
    int tot=0,ans=0,fx,fy,x,y,val;
    for(int i=0;i<n;++i){
        x=mp[i].x;
        y=mp[i].y;
        val=mp[i].val;
        fx=find(x);
        fy=find(y);
        if(tot==m-1) break;
        if(fx!=fy){
            pre[fx]=fy;
            ans+=val;
            ++tot;
        }
    }
    if(tot==m-1 ) return ans;
    return 0;
}
int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF&&n){
        for(int i=0;i<n;++i) {
         scanf("%d%d%d",&mp[i].x,&mp[i].y,&mp[i].val);
        }
        sort(mp,mp+n,cpu);
        for(int i=1;i<=m;++i) pre[i]=i;      //並查集初始化前導節點數組;
        int ans=kruskal(n,m);
        if(ans) printf("%d\n",ans);
        else printf("?\n");
    }
    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章