POJ1258(Minimum Spanning Tree,Prim)

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"cstring"
using namespace std;
int main()
{
 int n;                  
 int len[100][100];//下面要用memset的话貌似是不可以用动态分配内存的         
 bool vertix[10000];        
 while(cin>>n)
 {
  memset(len,0,sizeof(len));         
  memset(vertix,false,sizeof(vertix));      
  int min1=100001;              
  int find_x1,find_x2;
  //以下找出所有路径中的最短路径
  for(int i=0;i<n;i++)
   for(int j=0;j<n;j++)        
   {                                     
    cin>>len[i][j];       
    if(len[i][j]>0&&len[i][j]<min1)//寻找最短路径
    {     
     min1=len[i][j];     
     find_x1=i;            
     find_x2=j;            
    }  
   }    
   //cout<<"min1="<<min1<<endl;
  //后期处理,删除已访问路径并标记已访问点
  len[find_x1][find_x2]=0;
  len[find_x2][find_x1]=0;
  vertix[find_x1]=true;
  vertix[find_x2]=true;
  //以下开始prim
  int n_vertix=2;//作为监视哨
  int min2;                
  int find_y1,find_y2;
  int sum=min1;//路径总长度
  while(1)
  {
   min2=100001;//每次min2都要更新为max值
  for(int i=0;i<n;i++)
  {
   if(vertix[i])//若i已访问
    for(int j=0;j<n;j++)
    {
     if(!vertix[j]&&len[i][j]>0&&len[i][j]<min2)//j还没访问过,i->j路径存在,更新min2的限制条件
     {
      min2=len[i][j];   
      find_y1=i;
      find_y2=j;
     }
    }
  }
  //后期处理
        //后期处理,删除已访问路径并标记已访问点
  len[find_y1][find_y2]=0;
  len[find_y2][find_y1]=0;
  vertix[find_y1]=true;
  vertix[find_y2]=true;
  n_vertix++;   
  //cout<<"min2="<<min2<<endl;
  sum+=min2;
  if(n_vertix==n)
   break;
  }//end while
  cout<<sum<<endl;
 }
}

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