POJ 1258 Agri-Net

本文深入探讨了最小生成树的概念,并详细介绍了Prim算法的原理、时间复杂度及其在解决实际问题中的应用。通过代码示例,展示了如何使用Prim算法求解给定无向图中遍历所有顶点的最短路径之和,生成一棵最小生成树。重点分析了两种不同的实现方式,分别在O(V^2)和O(V^3)的时间复杂度内运行,旨在帮助读者理解算法的高效性和实用性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Agri-Net
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 29545 Accepted: 11716

Description

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.

Input

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.

Output

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.

Sample Input

4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0

Sample Output

28

Source

USACO 102

求无向图中遍历所有顶点的最短路径之和,生成一棵最小生成树。用Prim算法或是Kruskal算法均可(但是我还没学Kruskal...= =!)

最初时间复杂度为O(V^3)的算法

#include<iostream>
using namespace std;
#define Inf 100000000
long dis[101][101];
bool visit[101];
int i,j,k,n;
long long ans;
void Prim()
{
	int flag;
	long temp;
	for(k=1;k<n;k++)
	{
		temp=Inf;
		for(i=0;i<n;i++)
		{
			if(visit[i])
			//若该顶点已加入U集中,对这一点的后继点进行搜索,找到以U集中某一顶点为起点的最短路径
		{
			for(j=0;j<n;j++)
				{
					if(i!=j&&!visit[j])
						{if(temp>dis[i][j])
							{
								temp=dis[i][j];
					             flag=j;
					         }
					    }
			    }
		}
	    }
    visit[flag]=true;
	ans+=temp;
	}
}
int main()
{
	while(cin>>n)
	{
		memset(visit,false,sizeof(visit));
		memset(dis,0,sizeof(dis));
		visit[0]=true;
		ans=0;
		for(i=0;i<n;i++)
			for(j=0;j<n;j++)
				cin>>dis[i][j];
		Prim();
		cout<<ans<<endl;
	}
	return 0;
}


 

时间复杂度为(O^2)的算法

#include<iostream>
using namespace std;
#define Inf 100000000
int dis[101][101];
int lowcost[101];
bool visit[101];
int i,j,k,n;
long long ans;
void Prim()
{
	long min;int flag;
	for(i=1;i<n;i++)
		lowcost[i]=dis[0][i];//初始化到所有顶点的最短路径为源点到这些顶点的距离
	for(i=1;i<n;i++)
		{
			min=Inf;
			for(j=0;j<n;j++)
		    {
			if(!visit[j]&&min>lowcost[j]&&lowcost[j]!=0)
			//找到V-U集中的最短路径并记下顶点
				{
					min=lowcost[j];
					flag=j;
			    }
		    }
			visit[flag]=true;//把刚才记下的顶点加入到U集中
			for(j=0;j<n;j++)
			//更新到顶点j的最短路径为U集中元素到j的最短路径
			//注意是每加一个元素就更新一次,始终保持到j点的路径为最优路径
			{
				if(!visit[j]&&dis[flag][j]<lowcost[j])
					lowcost[j]=dis[flag][j];
			}
			ans+=min;//加上本轮循环的最短路径
	    }
}
int main()
{
	while(cin>>n)
	{
		memset(visit,false,sizeof(visit));
		memset(dis,0,sizeof(dis));
		visit[0]=true;
		ans=0;
		for(i=0;i<n;i++)
			for(j=0;j<n;j++)
				cin>>dis[i][j];
		Prim();
		cout<<ans<<endl;
	}
	return 0;
}


 

看来还是要多学多练,每做一题就要掌握题目背后的算法,只有这样才能保持进步!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值