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;
    }


     

  • 相关阅读:
    tpot从elastic search拉攻击数据之三 用于拉取的java程序
    tpot从elastic search拉攻击数据之二 配置端口映射
    wireshark使用
    VS c++ opencv画图
    java maven项目打包
    从es中拉取全部数据/大量数据 使用scroll+scan避免深分页
    java配置文件properties,yml,一般文件
    解决:JQuery "Uncaught ReferenceError: $ is not defined"错误
    java 字符串解析为json 使用org.json包的JSONObject+JSONArray
    easyui最简单的左右布局实现,及tab的右键菜单实现
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/10781735.html
Copyright © 2011-2022 走看看