zoukankan      html  css  js  c++  java
  • POJ-1258 Agri-Net(最小生成树)


    Description

    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

    代码:

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #include<set>
    #include<map>
    #include<vector>
    #include<cmath>
    
    const int maxn=1e5+5;
    typedef long long ll;
    using namespace std;
    struct node
    {
    	ll x,y,cost;
    }p[10005];
    
    int pre[maxn];
    int find(int x)
    {
    	if(x==pre[x])
    	{
    		return x;
    	}
    	else
    	{
    		return pre[x]=find(pre[x]);
    	}
    }
    bool Merge(int x,int y)
    {
    	int fx=find(x);
    	int fy=find(y);
    	if(fx!=fy)
    	{
    		pre[fx]=fy;
    		return true;
    	}
    	else
    	{
    		return false;
    	}
    }
    
    bool cmp(node x,node y)
    {
    	return x.cost<y.cost;
    }
    int main()
    {
    	int n;
    	while(cin>>n)
    	{
    	
    	int x;
    	int cnt=0;
    	for(int t=1;t<=n;t++)
    	{
    		pre[t]=t;
    	}
    	for(int t=1;t<=n;t++)
    	{
    		for(int j=1;j<=n;j++)
    		{
    			scanf("%d",&x);
    			p[cnt].x=t;
    			p[cnt].y=j;
    			p[cnt].cost=x;
    			cnt++;
    		}
    	}
    	sort(p,p+cnt,cmp);
    	int c=0;
    	ll sum=0;
    	for(int t=0;t<cnt;t++)
    	{
    		if(c==n-1)
    		{
    			break;
    		}
    		if(Merge(p[t].x,p[t].y))
    		{
    			sum+=p[t].cost;
    			c++;
    		}
    	}
    	cout<<sum<<endl;
        }
    	
    	return 0;
    }


     

  • 相关阅读:
    LaTeX表格紧跟文字 (不影响下方文本对齐)
    latex减少图片和图片解释文字之间的距离、调整公式与正文间距离,调整空白大小:
    请收藏,Linux 运维必备的 40 个命令总结,收好了~
    将公式直接转化为Latex代码的神器-Mathpix Snip
    if __name__ == '__main__':的作用和原理【转】
    PyCharm,Terminal 常用快捷键
    Python包中 __init__.py文件的作用
    pycharm写代码光标变成了黑框
    客户端链接Blog
    Sublime 中文标题乱码
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/10781735.html
Copyright © 2011-2022 走看看