POJ1258(Minimum Spanning Tree,Kruskal)

1258:Agri-Net

时间限制:

1000ms

内存限制:

65536kB

描述          

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.

输入

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.

输出

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.

样例输入

4

0 4 9 21

4 0 8 17

9 8 0 16

21 17 16 0

样例输出

28

 

#include"iostream"
#include"stdlib.h"
using namespace std;
class side
{
public:
 int x,y;
 int w;
};
int cmp(const void *a,const void *b)//升序排序,记得要加const啊
{
 return (*(side*)a).w-(*(side*)b).w;
}
side edge[100*99/2];//路径               
int origin[100];//当前根节点    
int n;
int root(int i)
{
 while(i!=origin[i])
  i=origin[i];
 return i;
}
int kruskal()
{
 int total=0;
 //origin[i]用于存当前i的root,初始化为i本身,作为监视哨,监视到达根节点;
 //实际上不同的root就代表了生成森林的各棵仅有一个根节点的自由树
    for(int i=0;i<n;i++)
  origin[i]=i;
 int tempx,tempy;  
 int sub=0;
 for(int i=0;i<n-1;i++)//找出n-1条边
 {                                
  //开头要更新tempx和tempy
  tempx=root(edge[sub].x);   
  tempy=root(edge[sub].y);  
  while(tempx==tempy)//寻找不会产生回路的边,肯定是放在前面,而后面对非回路的处理
  {                                 
           sub++;        
     tempx=root(edge[sub].x);  
     tempy=root(edge[sub].y); 
  }
  if(tempx<tempy)//将较为小的tempx置为tempy的origin,可是其实规则可以是<,也可以是>,由自己决定而已啦      
   origin[tempy]=tempx;   
  else//将tempy置为tempx的origin
   origin[tempx]=tempy;  
  total+=edge[sub].w;
  sub++;
 }//end for
 return total;
}
int main()
{
 int mat[100][100];
 int i;
 int j;
 while(cin>>n)
 {
    for(i=0;i<n;i++)
   for(j=0;j<n;j++)   
    cin>>mat[i][j];   
  int index=0;
  for(int i=0;i<n-1;i++)//对于对称矩阵,按此优化方案读入弧长;否则将会超时,悲催的!
   for(int j=i+1;j<n;j++)       
   {         
    edge[index].x=i;        
    edge[index].y=j;         
    edge[index++].w=mat[i][j];//记住此处index自增     
    }
         qsort(edge,index,sizeof(edge[0]),cmp); 
   cout<<kruskal()<<endl; 
 }
}

原文地址:https://www.cnblogs.com/lzhitian/p/2140066.html