POJ 题目1258 Agri-Net(最小生成树)

Agri-Net

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 42310   Accepted: 17287

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

无聊切水题

ac代码

#include<stdio.h>
#include<string.h>
#define INF 0xfffffff
int n;
int v[110],map[110][110];
int prim()
{
	int i,j,ans=0;
	memset(v,0,sizeof(v));
	for(i=1;i<=n;i++)
	{
		int min=INF;
		int flag=-1;
		for(j=1;j<=n;j++)
		{
			if(!v[j]&&map[1][j]<min)
			{
				flag=j;
				min=map[1][j];
			}
		}
		if(flag==-1)
			break;
		ans+=min;
		v[flag]=1;
		for(j=1;j<=n;j++)
		{
			if(!v[j]&&map[1][j]>map[flag][j])
			{
				map[1][j]=map[flag][j];
			}
		}
	}
	return ans;
}
int main()
{
	//int n;
	while(scanf("%d",&n)!=EOF)
	{
		int i,j;
		for(i=1;i<=n;i++)
		{
			for(j=1;j<=n;j++)
			{
				scanf("%d",&map[i][j]);
			}
		}
		printf("%d\n",prim());
	}
}
时间: 2024-10-28 21:15:40

POJ 题目1258 Agri-Net(最小生成树)的相关文章

poj 1258 Agri-Net(最小生成树果题)

题目链接:http://poj.org/problem?id=1258 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

poj 1258 Agri-Net (最小生成树 prim)

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39499   Accepted: 16017 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 nee

POJ 1258 Agri-Net (prim最小生成树)

最小生成树模板题 #include<bits/stdc++.h> using namespace std; int n,a; int dist[120],m[120][120]; void prim() {     bool p[1020];     for(int i=2;i<=n;i++)     {         p[i]=false;         dist[i]=m[1][i];     }     dist[1]=0,p[1]=true;     for(int i=1;

ACM训练方案-POJ题目分类

ACM训练方案-POJ题目分类 博客分类: 算法 ACM online Judge 中国: 浙江大学(ZJU):http://acm.zju.edu.cn/ 北京大学(PKU):http://acm.pku.edu.cn/JudgeOnline/ 杭州电子科技大学(HDU):http://acm.hdu.edu.cn/ 中国科技大学(USTC):http://acm.ustc.edu.cn/ 北京航天航空大学(BUAA)http://acm.buaa.edu.cn/oj/index.php 南京

【转】POJ题目分类

初级:基本算法:枚举:1753 2965贪心:1328 2109 2586构造:3295模拟:1068 2632 1573 2993 2996图:最短路径:1860 3259 1062 2253 1125 2240最小生成树:1789 2485 1258 3026拓扑排序:1094二分图的最大匹配:3041 3020最大流的增广路算法:1459 3436数据结构:串:1035 3080 1936排序:2388 2299哈希表和二分查找等高效查找法:3349 3274 2151 1840 2002

poj 3026 Borg Maze (bfs + 最小生成树)

链接:poj 3026 题意:y行x列的迷宫中,#代表阻隔墙(不可走),空格代表空位(可走),S代表搜索起点(可走) A代表外星人站(可走),现在要从S出发,将S和所有的A之间都连通,求路线总距离最小值 分析:可以先用bfs将所有的A,S两两之间的最短距离,题目的目的是将S与所有的A连通,使得总距离最小, 所有任选一点开始按最小生成树的算法做就行,并非非要从S点开始 注:题目输入x,y后可能有很多空格,可以用gets将多余的空格取走,开数组是尽量开大点,之前虽然开的比题目数据     稍大,但一

POJ题目(转)

http://www.cnblogs.com/kuangbin/archive/2011/07/29/2120667.html 初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法:     (

Poj 题目分类

初期:一.基本算法:     (1)枚举. (poj1753,poj2965)     (2)贪心(poj1328,poj2109,poj2586)     (3)递归和分治法.     (4)递推.     (5)构造法.(poj3295)     (6)模拟法.(poj1068,poj2632,poj1573,poj2993,poj2996)二.图算法:     (1)图的深度优先遍历和广度优先遍历.     (2)最短路径算法(dijkstra,bellman-ford,floyd,hea

POJ 题目1659 Frogs&#39; Neighborhood(度数还原无向图)

Frogs' Neighborhood Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 8348   Accepted: 3538   Special Judge Description 未名湖附近共有N个大小湖泊L1, L2, ..., Ln(其中包括未名湖),每个湖泊Li里住着一只青蛙Fi(1 ≤ i ≤ N).如果湖泊Li和Lj之间有水路相连,则青蛙Fi和Fj互称为邻居.现在已知每只青蛙的邻居数目x1, x2, ..