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

简介: <p>Agri-Net<br> Russ Cox <br> 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 cou

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.


 

目录
打赏
0
0
0
0
3124
分享
相关文章
C# .NET面试系列九:常见的算法
#### 1. 求质数 ```c# // 判断一个数是否为质数的方法 public static bool IsPrime(int number) { if (number < 2) { return false; } for (int i = 2; i <= Math.Sqrt(number); i++) { if (number % i == 0) { return false; } } return true; } class Progr
151 1
.NET 平台 SM2 国密算法 License 证书生成深度解析
授权证书文件的后缀通常取决于其编码格式和具体用途。本文档通过一个示例程序展示了如何在 .NET 平台上使用国密 SM2 算法生成和验证许可证(License)文件。该示例不仅详细演示了 SM2 国密算法的实际应用场景,还提供了关于如何高效处理大规模许可证文件生成任务的技术参考。通过对不同并发策略的性能测试,开发者可以更好地理解如何优化许可证生成流程,以满足高并发和大数据量的需求。 希望这段描述更清晰地传达了程序的功能和技术亮点。
121 13
.NET 平台 SM2 国密算法 License 证书生成深度解析
使用贪心算法解决最小生成树问题
大家好,我是V哥。今天聊聊贪心算法解决最小生成树问题。面试中遇到此类题目,需掌握Prim和Kruskal算法。Prim适合稠密图,Kruskal适合稀疏图。两者时间复杂度分别为O(m log n)和O(m log m),各有优缺点。应用场景广泛,包括图像处理、传感器网络、社交网络分析等。关注V哥,全栈之路一起走。
基于prim算法求出网络最小生成树实现网络社团划分和规划
该程序使用MATLAB 2022a版实现路线规划,通过排序节点权值并运用Prim算法生成最小生成树完成网络规划。程序基于TSP问题,采用遗传算法与粒子群优化算法进行路径优化。遗传算法通过编码、选择、交叉及变异操作迭代寻优;粒子群优化算法则通过模拟鸟群觅食行为,更新粒子速度和位置以寻找最优解。
C#.NET逃逸时间算法生成分形图像的毕业设计完成!晒晒功能
该文介绍了一个使用C#.NET Visual Studio 2008开发的程序,包含错误修复的Julia、Mandelbrot和优化过的Newton三种算法,生成色彩丰富的分形图像。作者改进了原始算法的效率,将内层循环的画点操作移至外部,提升性能。程序提供五种图形模式,支持放大缩小及颜色更新,并允许用户自定义画布大小以调整精度。还具备保存为高质JPG的功能。附有四张示例图片展示生成的分形效果。
Java数据结构与算法:贪心算法之最小生成树
Java数据结构与算法:贪心算法之最小生成树
|
9月前
|
数据结构与算法——最小生成树问题(什么是最小生成树、Prim算法、Kruskal算法)
数据结构与算法——最小生成树问题(什么是最小生成树、Prim算法、Kruskal算法)
75 0
上机实验三 图的最小生成树算法设计 西安石油大学数据结构
上机实验三 图的最小生成树算法设计 西安石油大学数据结构
132 1
一些算法的复习(最短路径、最小生成树、dp)
一些算法的复习(最短路径、最小生成树、dp)

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等