Minimum Spanning Tree

struct DisjointSetUnion
{
    static const int __=10005;
    int pre[__];
    DisjointSetUnion() {memset(pre,-1,sizeof(pre));}
    void un(int x,int y)
    {
        x=fd(x),y=fd(y);
        if(x==y)return;
        if(pre[x]

Kruskal

struct Kruskal
{
    static const int __=100005;
    int edge_num;
    struct edge
    {
        int x,y,z;
        bool operator<(const edge& b)const
        {
            return z
struct mst
{
    static const int _n_=1005,_e_=1000005;
    struct edge
    {
        int nex,dis,nex_edge;
        edge() {}
        edge(int x,int y,int z=0):
            nex(x),dis(y),nex_edge(z) {}
        bool operator<(const edge& b)const
        {
            return dis>b.dis;
        }
    }e[_e_<<1];
    int head[_n_],dis[_n_],ne;
    bool vis[_n_];
    mst():ne(0){memset(head,-1,sizeof(head));}
    void add_edge(int x,int y,int dis)
    {
        e[ne]=edge(y,dis,head[x]);
        head[x]=ne++;
    }
    int prim()
    {
        int sum=0;
        memset(vis,false,sizeof(vis));
        memset(dis,0x3f3f3f3f,sizeof(dis));
        priority_queueQ;
        dis[1]=0,Q.push(edge(1,0));
        while(!Q.empty())
        {
            edge t=Q.top();
            Q.pop();
            if(vis[t.nex])continue;
            sum+=t.dis;
            vis[t.nex]=true;
            for(int i=head[t.nex];~i;i=e[i].nex_edge)
                if(!vis[e[i].nex] && e[i].dis

你可能感兴趣的:(Minimum Spanning Tree)