poj1258

依旧是最小生成树的基础题~

#include<iostream>
#include<algorithm>
using namespace std;
int father[101];
struct n1
{
	int s,e,w;
};
n1 path[6000];
bool cmp(n1 a,n1 b)
{
	return a.w<b.w;
}
int find(int i)
{
	while(father[i]!=i)
	{
		i=father[i];
	}
	return i;
}
int kruskal(int v,int e)
{
	int temp1,temp2,i,sum,num_bian;
	for(i=1;i<=v;i++)
	{
		father[i]=i;
	}
	sort(path+1,path+e+1,cmp);
	i=sum=num_bian=0;
	v--;
	while(num_bian!=v)
	{
		i++;
		temp1=find(path[i].s);
		temp2=find(path[i].e);
		if(temp1!=temp2)
		{
			sum+=path[i].w;
			num_bian++;
			if(temp1>temp2)
			{
				temp1^=temp2^=temp1^=temp2;
			}
			father[temp2]=temp1;
		}
	}
	return sum;
}
int main()
{
		int t,i,j,k,map[101][101],n;
		while(scanf("%d",&n)!=EOF){

		k=0;
		for(i=1;i<=n;i++)
		for(j=1;j<=n;j++)
		{
			scanf("%d",&map[i][j]);
			if(j>i)
			{
				k++;
				path[k].s=i;
				path[k].e=j;
				path[k].w=map[i][j];
			}
		}
		printf("%d\n",kruskal(n,k));
	}
}

poj1258

时间: 2024-10-18 02:50:38

poj1258的相关文章

poj1258 Agri-Net (prim+heap)

题目链接:poj1258 Agri-Net 这题我上个月做过,是个大水题,今天看见有人用prim+heap做的,就学习了下. 1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<queue> 5 #include<vector> 6 #include<set> 7 #define CLR(a,b) memset((a),(b),sizeof((

POJ1258最小生成树(prim算法)

POJ1258 思路:首先把第一个结点加入树中,每次往树中加入一个结点,加入的结点必须是与当前树中的结点距离最小那个点,这样每次把结点加入树中选取的都是最小权值,循环n-1次后把所有结点都加入树中. #include<iostream> #include<cstdio> #include<cstring> using namespace std; const int MAXN = 1e9; //创建map二维数组储存图表,low数组记录每2个点间最小权值,vis数组标记

poj1258 最小生成树 普林姆

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

poj1258(Agri-net)

解题思路: 最小生成树. 代码: 1 #include <algorithm> 2 #include <iostream> 3 #include <sstream> 4 #include <cstdlib> 5 #include <cstring> 6 #include <cstdio> 7 #include <string> 8 #include <bitset> 9 #include <vector&

最小生成树Prim poj1258 poj2485

poj:1258 Agri-Net Time Limit: 1000 MS Memory Limit: 10000 KB 64-bit integer IO format: %I64d , %I64u Java class name: Main [Submit] [Status] [Discuss] Description Farmer John has been elected mayor of his town! One of his campaign promises was to bri

POJ1258 基础最小生成树

本文出自:http://blog.csdn.net/svitter 题意:给出一个数字n代表邻接矩阵的大小,随后给出邻接矩阵的值.输出最小生成树的权值. 题解: prime算法的基本解法: 1.选择一个点,然后不停的向其中加入权值最小的边,边的一端在已经生成的部分生成树中,另一端在未生成的生成树中. 2.利用优先队列维护边,将加入的点所包含的边加入到队列中去,随后按照边的权值弹出. 简单理解方法:一个人可以拉很多人,新被拉进来的人,把能拉的人(有边,且未被访问)排队,找最好拉的人拉进来,循环.

最小生成树问题(prim算法)POJ-1258 Agri-Net

/* 这个题很水,但是,莫名其妙runtime error一晚上,重写了一遍就又没了,很伤心! 题意很简单,大致为n个村庄,连光缆,要求连上所有村庄的长度最短. 输入n,接着是n*n的矩阵,直接用prim算法写就行: */ #include<iostream> #include<cstdlib> #include<cstdio> #include<cstring> #include<algorithm> #include<cmath>

POJ1258 Agri-Net

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

Prim算法POJ1258

http://poj.org/problem?id=1258 这道题是最简单的一个啦,,,, 1 #include<stdio.h> 2 #include<iostream> 3 #include<string.h> 4 #include<algorithm> 5 using namespace std; 6 const int N=110; 7 #define M 0x3f3f3f3f///设置无穷大的数M,当两点之间无法联通时设两者之间距离为M 8 in