zoukankan      html  css  js  c++  java
  • 第10讲并查集

    课件:(下载

    【POJ1258】Agri-Net

    #include <iostream>
    #include
    <algorithm>
    using namespace std;
    #define N 101

    //边集合,因为是无向图,所以只需要存一半的边
    struct edge{
    int w;
    int v;
    int d;
    }e[N
    *N/2];

    int father[N];
    int n;
    int edgeCount;//边的数量

    //比较两个边的权值,用于排序
    bool Cmp(const edge &v1,const edge &v2)
    {
    return v1.d<v2.d;
    }

    //数据初始化和数据输入
    void Init()
    {
    edgeCount
    =0;
    for( int k=1;k<=n;k++ )
    {
    father[k]
    =k;
    }

    int input;
    for(int i=1;i<=n;i++)
    {
    for( int j=1;j<=n;j++ )
    {
    cin
    >>input;
    if( i<j)
    {
    e[edgeCount].w
    = i;
    e[edgeCount].v
    = j;
    e[edgeCount].d
    = input;
    edgeCount
    ++;
    }
    }
    }

    //所有边按权值大小依次排序,注意第二个参数表示容器的end位置(即最后一个位置的后面一个位置)
    sort(e,e+edgeCount,Cmp);
    }

    int GetFather(int x)
    {
    if(father[x]==x)
    return x;
    else
    {
    father[x]
    = GetFather(father[x]);//路径压缩
    return father[x];
    }
    }

    bool Merge(int x,int y)
    {
    x
    =GetFather(x);
    y
    =GetFather(y);
    if(x==y)
    return false;
    else
    father[x]
    =y;
    return true;
    }

    int Kruskal()
    {
    int sum=0;
    for(int i=0;i<edgeCount;i++)
    {
    if(Merge(e[i].w,e[i].v))
    sum
    +=e[i].d;
    }
    return sum;
    }


    int main()
    {
    while(cin>>n)
    {
    Init();
    cout
    <<Kruskal()<<endl;
    }
    return 0;
    }
  • 相关阅读:
    Django: ModelForm中Meta的fields等成员介绍
    python的random函数
    设置mysql隔离级别
    ubantu 下 修改mysql 默认编码
    jdbc 模板 连接
    sql 注入 与解决
    jdbc 简单连接
    动态代理 例子
    自定义的一个数据输入类
    类加载器 读取配置文件
  • 原文地址:https://www.cnblogs.com/tuty/p/1877435.html
Copyright © 2011-2022 走看看