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;
    }
  • 相关阅读:
    Git入门
    基于sendmail的简单zabbix邮件报警
    zabbix agentd安装
    我整理的一份来自于线上的Nginx配置(Nginx.conf),希望对学习Nginx的有帮助
    【转载】Spring Boot引起的“堆外内存泄漏”排查及经验总结
    lodop+art-template实现web端漂亮的小票样式打印
    《阿里巴巴Java开发手册》改名《Java开发手册》,涵盖史无前例的三大升级
    Spring Boot的学习之路(02):和你一起阅读Spring Boot官网
    Spring Boot的学习之路(01):缘起
    『 效率工具 』Spring Boot版的轻量级代码生成器,减少70%以上的开发任务
  • 原文地址:https://www.cnblogs.com/joeylee97/p/6590545.html
Copyright © 2011-2022 走看看