1258:Agri-Net Prime()水题

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"
using namespace std; 
#define Max 105
#define Sky 9999999
int Map[Max][Max],Top,Cost[Max];
bool Move[Max];

int  Prime()
{ 
	int sum=0;
    int i,j;
	for(i=1;i<=Top;i++)
	{
		Cost[i]=Map[1][i];
		Move[i]=false;
	
	}
    Move[1]=true;

    for(i=1;i<=Top-1;i++)
	{
        
		int Min=Sky,k;
		for(j=1;j<=Top;j++)
		{
           if(!Move[j] && Cost[j]<Min)
		   {
			   k=j;
			   Min=Cost[j];
		   }
		}

         Move[k]=true;
		 for(j=1;j<=Top;j++)
		 {
			 if(Cost[j]>Map[k][j]  && !Move[j])
			 {
				  Cost[j]=Map[k][j];
			 }
		 }
	}
	for(i=1;i<=Top;i++)
	sum+=Cost[i];
	return sum;
 
}



int main()
{   

    //freopen("1.txt","r",stdin);
	
	
    while(cin>>Top)
	{
	int i,j;
	for(i=1;i<=Top;i++)
	{
		for(j=1;j<=Top;j++)
		{
			cin>>Map[i][j];
		}
		 
	}
   
	cout<<Prime()<<endl;
    }
    return 0;
}


你可能感兴趣的:(1258:Agri-Net Prime()水题)