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


     

  • 相关阅读:
    常用的SQL优化
    mysql索引详细介绍
    作业2
    作业1
    python学习笔记(11)文件操作
    python学习笔记(10)函数(二)
    python学习笔记(9)函数(一)
    C#的dictionary使用总结
    常用的类型转化
    我的动态库“情节”
  • 原文地址:https://www.cnblogs.com/Staceyacm/p/10781735.html
Copyright © 2011-2022 走看看