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;
    }
  • 相关阅读:
    API接口认证
    接口测试怎么做
    负载均衡(Load Balance)是分布式系统架构设计中必须考虑的因素之一,它通常是指,将请求/数据【均匀】分摊到多个操作单元上执行,负载均衡的关键在于【均匀】。常见互联网分布式架构如上,分为客户端层、反向代理nginx层、站点层、服务层、数据层。
    软件安全测试的几个原则
    9.22
    9.20
    9.19
    9.15
    9.12作业
    9.8作业
  • 原文地址:https://www.cnblogs.com/joeylee97/p/6590545.html
Copyright © 2011-2022 走看看