HDOJ 1102 Constructing Roads 最小生成树 kruskal && Prim

Constructing Roads

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17211    Accepted Submission(s): 6535


Problem Description
There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected.

We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.
 

Input
The first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 1000]) between village i and village j.

Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.
 

Output
You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum.
 

Sample Input
  
  
3 0 990 692 990 0 179 692 179 0 1 1 2
 

Sample Output
  
  
179
注-此题为: HDOJ 1102 Constructing Roads

题意:
       有N个村庄,给定我们每个村庄之间的距离,而两个村庄可以间接相连。

 矩阵 :横坐标与纵坐标表示两个村子,值为两个村子的距离;

然后告诉我们原本有几个村庄已经相连有路相通。要让我们求还要建多长的路,最小的。
可以用最小生成树,先把给定的已相连的村庄之间的权值赋为0,其余跟求最小生成树一样。

已AC代码:(kruskal

#include<cstdio>
#define MAX 11000
#include<algorithm>
using namespace std;

int n,m,per[125];   // 并查集 

struct node{
	int u,v,w;    //w为距离 
}s[MAX];

bool cmp(node a,node b)
{
	return a.w<b.w;
}

void into()    //初始化 
{
	for(int i=0;i<=n;++i)
		per[i]=i;
}

int find(int x)    // 查找根节点 
{
	return x==per[x]?x:per[x]=find(per[x]);
}

bool join(int a,int b)    //合并根节点,并判断是否成环 
{
	int fa=find(a);
	int fb=find(b);
	if(fa!=fb)
	{
		per[fa]=fb;
		return true;
	}
	return false;
}

int main()
{
	int i,j,a,b,c,k,q;
	while(scanf("%d",&n)!=EOF)
	{
		k=0;
		for(i=1;i<=n;++i)   //读入数据 
		{
			for(j=1;j<=n;++j)
			{
				scanf("%d",&a);
				if(i>j)  //只读取一半 (i>j)的数据 
				{
					s[k].u=i;
					s[k].v=j;
					s[k].w=a;
					k++;
				}
			}
		}
		

		into();  //初始化根节点 
		scanf("%d",&q);
		while(q--)
		{
			scanf("%d%d",&b,&c);
			a=join(b,c);
		}
		int sum=0;
		sort(s,s+k,cmp);  //按距离从小到大排序 
		
		for(i=0;i<k;++i)
		{
			if(join(s[i].u,s[i].v))
			{
				sum+=s[i].w;
			}
		}
		printf("%d\n",sum);
	}
	return 0;
}


已AC代码:(Prim)(已经连通的 权值设为 0 )

#include<cstdio>
#include<cstring>
#include<cmath>
#define INF 0xfffffff

int map[125][125],low[125];
int vis[125]; //map二维数组存图,low记录每2个点间最小权值,vis标记某点是否已访问
int n,sum;

void prim()
{
	int min;
	int i,j,pos;
	memset(vis,0,sizeof(vis));
	vis[1]=1;	low[1]=0;
	for(i=2;i<=n;++i)  	//从某点开始,分别标记vis和记录该点pos
		low[i]=map[1][i];  //第一次给low数组赋值 map的第一行 
	
	for(i=1;i<n;++i)   //再运行n-1次,一次找一个最小 
	{
		min=INF;
		for(j=1;j<=n;++j)  // 找出最小值min,记录位置pos
		{
			if(vis[j]==0&&low[j]<min)
			{
				min=low[j];
				pos=j;
			}
		}
		
		vis[pos]=1;   //标记该点已访问 
		sum+=min;  //最小权值累加
		for(j=1;j<=n;++j)     //更新权值low 把 map的 pos 行中比对应的 low 小的赋给low 
			if(vis[j]==0&&low[j]>map[pos][j])
				low[j]=map[pos][j];
	}
	return ;
}

int main()
{
	int i,j,q;
	while(scanf("%d",&n)!=EOF)
	{
		memset(map,0,sizeof(map));
		for(i=1;i<=n;++i)  //建图 
		{
			for(j=1;j<=n;++j)
			{
				scanf("%d",&map[i][j]);
			}
		}

		scanf("%d",&q);
		while(q--)
		{
			scanf("%d%d",&i,&j);
			map[i][j]=map[j][i]=0;  //已连通
		}
		
		sum=0;
		prim();
		printf("%d\n",sum);
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值