hdu 1102 prime算法

Constructing Roads

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

Total Submission(s): 15475    Accepted Submission(s): 5907

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

//考查知识点:prime算法
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define inf 0xfffffff
int n;
int map[110][110],visit[110],dis[110];
void prime()
{
	int i,j,sum=0,pos=1,min;
	for(i=1;i<=n;++i)
	{
		dis[i]=map[1][i];
	}
	visit[1]=1;
	dis[1]=0;
	for(i=1;i<n;++i)
	{
		min=inf;
		for(j=1;j<=n;++j)
		{
			if(!visit[j]&&dis[j]<min)
			{
				min=dis[j];
				pos=j;
			}
		}
		sum+=min;
		visit[pos]=1;
		for(j=1;j<=n;++j)
		{
			if(!visit[j]&&dis[j]>map[pos][j])
			dis[j]=map[pos][j];
		}
	}
	printf("%d\n",sum);
}
int main()
{
	int i,j;
	while(~scanf("%d",&n))
	{
		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",&map[i][j]);
			}
		}
		int m,a,b;
		scanf("%d",&m);
		while(m--)
		{
			scanf("%d%d",&a,&b);
			map[a][b]=map[b][a]=0;
		}
		memset(visit,0,sizeof(visit));
		prime();
	}
	return 0;
} 

时间: 2024-10-08 19:42:36

hdu 1102 prime算法的相关文章

HDU 1102 Kruscal算法

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

HDU 1102 Constructing Roads【简单最小生成树,Prime算法+Kruskal算法】

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

hdu 1102 Constructing Roads(Prime算法)

本题链接:点击打开链接 本题采用的是另一种算法(Prime算法)算法是采用一个二维数组map其下标分别表示该条路所连接的两个村庄的标号,存放的内容是该路的长度,即权值.使用一个mark数组标记已连过的村庄,一个一维数组lowcost存放是以下标为终点的权值,有些路已连通不再需要修建,便将对应map数组的值标为0(一条路对应两个map).具体请参见代码: #include<stdio.h> #include<string.h> #define INF 0xffffff int map

HDU 1389 继续畅通工程【最小生成树,Prime算法+Kruskal算法】

继续畅通工程 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 21871    Accepted Submission(s): 9356 Problem Description 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).现得到城镇道路统计表,表中列

HDU 3371 Connect the Cities 【最小生成树,Prime算法+Kruskal算法】

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

HDU 1863 畅通工程【最小生成树,Prime算法+Kuruskal算法】

畅通工程 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 25784    Accepted Submission(s): 11234 Problem Description 省政府"畅通工程"的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可).经过调查评估,得到的统计表中列

HDU 1162 Eddy&#39;s picture【最小生成树,Prime算法+Kruskal算法】

Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 9334    Accepted Submission(s): 4711 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to

HDU 1301 &amp;POJ 1215 Jungle Roads【最小生成树,Prime算法+Kruskal算法】

Jungle Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 6737    Accepted Submission(s): 4893 Problem Description The Head Elder of the tropical island of Lagrishan has a problem. A burst o

HDU 1875 畅通工程再续【最小生成树,Prime算法+Kruskal算法】

畅通工程再续 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 24151    Accepted Submission(s): 7808 Problem Description 相信大家都听说一个"百岛湖"的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府决定大力发展百岛湖,发展首先