HDU 1102 最小生成树

Constructing Roads

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 15078    Accepted Submission(s): 5757

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

Source

kicc

还是修路问题。这道题两种解法,一种是贪心+并查集。

一种就是最小生成树了。今天刚学的。

所以用了它做。发现自己无法用语言来描绘这个算法。。

贴下代码吧。

#include <stdio.h>
#include <algorithm>
using namespace std;
#include <string.h>
#define inf 0x6ffffff
int vis[102];//用来标记是否被用过
int map[102][102];
int dis[102];
int n,sum;
void prim()
{
	int i,j,k,min;
	for(i=1;i<=n;i++)
		dis[i]=map[1][i];
	vis[1]=1;
	   for(i=1;i<=n;i++)
	   {
		   min=inf;
		   for(j=1;j<=n;j++)
		   {
			   if(vis[j]==0&&dis[j]<min)
			   {
				   min=dis[j];
				   k=j;
			   }
		   }
		   if(min==inf)
			   break;
		   vis[k]=1;
		   sum+=min;
		   for(j=1;j<=n;j++)
		   {
			   if(vis[j]==0 &&dis[j]>map[k][j])
				   dis[j]=map[k][j];
		   }
	   }
}
int main()
{
	int ll,i,j,str,end,m;
	while(scanf("%d",&n)!=EOF)
	{
		memset(vis,0,sizeof(vis));
		memset(dis,0,sizeof(dis));
		for(i=1;i<=n;i++)
			for(j=1;j<=n;j++)
				map[i][j]=inf;
			for(i=1;i<=n;i++)
				for(j=1;j<=n;j++)
				{
					scanf("%d",&ll);
					if(map[i][j]>ll)
						map[i][j]=map[j][i]=ll;
				}
				scanf("%d",&m);
				for(i=1;i<=m;i++)
				{
					scanf("%d%d",&str,&end);
					map[str][end]=map[end][str]=0;   //这里必须是双向的。不能写成	map[str][end]=0。因为这里WA了两次
				}
				sum=0;
				prim();
				printf("%d\n",sum);
	}
	return 0;
}
时间: 2024-09-26 15:37:48

HDU 1102 最小生成树的相关文章

HDU 1102 最小生成树裸题,kruskal,prim

1.HDU  1102  Constructing Roads    最小生成树 2.总结: 题意:修路,裸题 (1)kruskal //kruskal #include<iostream> #include<cstring> #include<cmath> #include<queue> #include<algorithm> #include<cstdio> #define max(a,b) a>b?a:b using na

HDU 1102 Constructing Roads, Prim+优先队列

题目链接:HDU 1102 Constructing Roads Constructing Roads 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 conne

HDU 1102 &amp;&amp; POJ 2421 Constructing Roads (经典MST~Prim)

链接:http://poj.org/problem?id=2421  或   http://acm.hdu.edu.cn/showproblem.php?pid=1102 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

HDU 1102 Constructing Roads (裸的并查集)

Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13210    Accepted Submission(s): 4995 Problem Description There are N villages, which are numbered from 1 to N, and you should

图论问题(2) : hdu 1102

题目转自hdu 1102,题目传送门 题目大意: 输入一个n*n的邻接矩阵,其中i行j列代表从i到j的路径的长度 然后又m条路已经帮你修好了,求最短要修多长的路才能使所有村庄连接 不难看出,这道题就是标准的最小生成树模板,多水啊 虽然很水,但本人还是调了近1h才把代码调好...... 下面介绍一下解决最小生成树的两个方法: Prim 和 Kruskal 一,Prim(不懂的点这里) Prim的思想和dijkstra的想法很想(如果不知道dijkstra算法的请点这里) 那么Prim的复杂度在为优

hdu 3371 最小生成树prim算法

Connect the Cities Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8992    Accepted Submission(s): 2519 Problem Description In 2100, since the sea level rise, most of the cities disappear. Thoug

HDU 1102 Kruscal算法

题目大意:给定村庄的数量,和一个矩阵表示每个村庄到对应村庄的距离,矩阵主对角线上均为1 在给定一个数目Q,输入Q行之间已经有通道的a,b 计算还要至少修建多少长度的轨道 这道题目用Kruscal方法进行计算,先将已有路径记为0,再进行所有路径长度的排序(只计算一个下三角或一个上三角,还把主对角线去掉的那种),通过并查集相交的方法,来判断二者是否属于同一个连通分量,由小到大不断找到你选取的路径,将其加起来即可 代码如下: 1 #include <iostream> 2 #include<c

hdu 1301 最小生成树prim实现

http://acm.hdu.edu.cn/showproblem.php?pid=1301 Jungle Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4145    Accepted Submission(s): 3020 Problem Description The Head Elder of the tropica

hdu 1102 Constructing Roads (最小生成树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 14172    Accepted Submission(s): 5402 Problem Description There are N villa