POJ-1258 Agri-Net(模板prim算法)

POJ-1258 Agri-Net

Time Limit: 1000MS
Memory Limit: 10000K
Total Submissions: 81114
Accepted: 33478

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

描述
农民约翰被选为镇长!他的竞选承诺之一是为该地区所有农场提供互联网连接。当然,他需要你的帮助。
农民约翰下令为他的农场高速连接,并打算与其他农民分享他的连接。为了最大限度地降低成本,他想铺设最少的光纤,将农场与所有其他农场连接起来。
如果列出连接每个农场需要多少光纤,您必须找到将它们连接在一起所需的最小纤量。每个农场必须连接到其他某个农场,以便数据包可以从任何一个农场流到任何其他农场。
任何两个农场之间的距离不会超过100,000。
输入
输入包括几个案例。对于每个案例,第一行包含农场数量,N(3 <=N <=100)。以下行包含 N x N 锥度矩阵,其中每个元素显示从农场到另一个元素的距离。从逻辑上讲,它们是 N 线的 N 空间分离整数。物理上,它们的长度限制在 80 个字符以内,因此某些行继续到其他字符上。当然,对角线将是0,因为从农场i到本身的距离对这个问题不感兴趣。
输出
对于每个案例,输出单个整数长度,即连接整个农场所需的最小长度的总和。

Sample Input

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

Sample Output

28
#include<cstdio>
const int N = 110, INF = 1e9;
int e[N][N],vis[N],dis[N];
int n,min,sum;
void prim()
{
	for(int i=1;i<=n;i++) 
	{
		vis[i]=0;
		dis[i]=e[1][i];//从第一组开始 
	}
	dis[1]=0;
	vis[1]=1;
	for(int i=1;i<n;i++) //n个点,需要n-1条边 
	{
		int minn=INF,k; 
		for(int j=1;j<=n;j++) 
		if(vis[j]==0&&dis[j]<minn) 
		{
			k=j;//记录你这个点 
			minn=dis[j];//记录最小值 
		}
		vis[k]=1;
		sum+=minn;
		for(int j=1;j<=n;j++) 
		{ 
			if(vis[j]==0&&dis[j]>e[k][j]) //松弛 
			dis[j]=e[k][j];
		} 
	}
}
int main()
{
	while(scanf("%d",&n)!=EOF) 
	{
		sum=0;
		for(int i=1;i<=n;i++) 
			for(int j=1;j<=n;j++) 
				scanf("%d",&e[i][j]);
		prim();
		printf("%d\n",sum);
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Wa_Automata

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值