zoukankan      html  css  js  c++  java
  • 最小生成树 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项目上传Github预览
    vue数据绑定源码
    构建工具是如何用 node 操作 html/js/css/md 文件的
    prop 和 attr 中一些羞羞的事情
    用element-ui的走马灯carousel轻松实现自适应全屏banner图
    Vue实例方法之事件的实现
    初学微信小程序 TodoList
    一个页面从输入URL到加载显示完成,发生了什么?
  • 原文地址:https://www.cnblogs.com/joeylee97/p/6590545.html
Copyright © 2011-2022 走看看