[usaco]Agri-Net(使用最小生成树算法)

Agri-Net
Russ Cox
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.

PROGRAM NAME: agrinet
INPUT FORMAT
Line 1:  The number of farms, N (3 <= N <= 100). 
Line 2..end:  The subsequent lines contain the N x N connectivity 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. 

SAMPLE INPUT (file agrinet.in)
4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0

OUTPUT FORMAT
The single output contains the integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.

SAMPLE OUTPUT (file agrinet.out)
28

------------------
这个问题就是让求最小生成树。
利用prim 算法,或者kruskal算法即可。
鉴于节点的总个数小于等于100,可以使用一个常量数组
我的解法
----------------

/*
ID:yunleis2
PROG:agrinet
LANG:C++
*/
#include<iostream>
#include<fstream>

using namespace std;
/************************************************************************/
/* minimal spinning tree                                                                     */
/************************************************************************/
int metri[101][101];
int destance[101]; 
bool intree[101];
int main()
{
	fstream fin("agrinet.in",ios::in);
	int N;
	fin>>N;
	for(int i=0;i<N;i++)
	{
		for(int j=0;j<N;j++)	
		{
			fin>>metri[i][j];
		}
		destance[i]=0;
		intree[i]=false;
	}
	int sum=0;
	for(int i=0;i<N;i++)
	{
		if(!intree[i])
		{
			intree[i]=true;
			for(int j=0;j<N;j++)
			{
				destance[j]=metri[i][j];
			}
			while(true)
			{
				int ptr=-1;
				for(int j=0;j<N;j++)
				{
					if(ptr==-1||destance[ptr]==0||(intree[ptr]))
						ptr=j;
					else if((!intree[j])&&destance[j]<destance[ptr])
						ptr=j;
				}
				if(intree[ptr])
					break;
				intree[ptr]=true;
				sum+=destance[ptr];
				for(int j=0;j<N;j++)
				{
					if((!intree[j])&&(destance[j]>metri[ptr][j]))
					{
						destance[j]=metri[ptr][j];
					}
				}
			}
		}
	}
	fstream fout("agrinet.out",ios::out);
	fout<<sum<<endl;
	//system("pause");

}

测试:

USER: Ma yunlei [yunleis2]
TASK: agrinet
LANG: C++

Compiling...
Compile: OK

Executing...
   Test 1: TEST OK [0.000 secs, 3064 KB]
   Test 2: TEST OK [0.000 secs, 3064 KB]
   Test 3: TEST OK [0.000 secs, 3064 KB]
   Test 4: TEST OK [0.000 secs, 3064 KB]
   Test 5: TEST OK [0.000 secs, 3064 KB]
   Test 6: TEST OK [0.000 secs, 3064 KB]
   Test 7: TEST OK [0.000 secs, 3064 KB]
   Test 8: TEST OK [0.000 secs, 3064 KB]
   Test 9: TEST OK [0.000 secs, 3064 KB]
   Test 10: TEST OK [0.000 secs, 3064 KB]

All tests OK.
YOUR PROGRAM ('agrinet') WORKED FIRST TIME!  That's fantastic
-- and a rare thing.  Please accept these special automated
congratulations.


 

上一篇:NetApp凭借领先的混合云数据与服务把握数字化转型机遇


下一篇:android中include和merge标记