• 最小生成树 I


    Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course. 
    Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms. 
    Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm. 
    The distance between any two farms will not exceed 100,000. 

    Input

    The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.

    Output

    For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.

    Sample Input

    4
    0 4 9 21
    4 0 8 17
    9 8 0 16
    21 17 16 0
    

    Sample Output

    28

    Kruskal 算法,借助并查集
    #include<iostream>
    #include<string>
    #include<algorithm>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<vector>
    #include<iomanip>
    #include<iostream>
    using namespace std;
    #define MAXN 101
    #define INF 0x3f3f3f3f
    int pre[MAXN];
    struct Edge
    {
        int u,v,w;
    }edge[MAXN*MAXN/2];
    int tol;
    void addedge(int u,int v,int w)
    {
        edge[tol].u = u;
        edge[tol].v = v;
        edge[tol++].w = w;
    }
    bool cmp(Edge a,Edge b)
    {
        return a.w<b.w;
    }
    int find(int x)
    {
        if(pre[x]==-1)
            return x;
        else
            return pre[x] = find(pre[x]);
    }
    int Kruskal(int n)
    {
        memset(pre,-1,sizeof(pre));
        sort(edge,edge+tol,cmp);
        int cnt = 0;
        int ans = 0;
        for(int i=0;i<tol;i++)
        {
            int u = edge[i].u;
            int v = edge[i].v;
            int w = edge[i].w;
            int t1 = find(u),t2 = find(v);
            if(t1!=t2)
            {
                ans+=w;
                pre[t1] = t2;
                cnt++;
            }
            if(cnt==n-1) break;
        }
        if(cnt<n-1) return -1;
        else return ans;
    }
    int main()
    {
        int n,d;
        while(cin>>n)
        {
        //if(n==0) break;
        tol = 0;
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                cin>>d;
                if(j>i)
                    addedge(i,j,d);
            }
        }
        int ans = Kruskal(n);
        cout<<ans<<endl;
        }
        return 0;
    }
  • 相关阅读:
    Vue.nextTick()的介绍和使用场景
    JS实现数据双向绑定
    JS对象的可枚举属性和不可枚举属性
    JS中对象转数组方法总结
    Vmware虚拟机安装XP系统
    javascript和c#的深度拷贝的一种通用方法
    c# SQLServer导入大批量数据
    PowerDesigner逆向工程,从SQL Server数据库生成Physical Model -----数据源方式
    虚拟机和主机ping不通,SQL Server无法远程连接的解决方法
    c#生成试卷。。。
  • 原文地址:https://www.cnblogs.com/joeylee97/p/6590545.html
Copyright © 2020-2023  润新知