Hdu1102-Constructing Roads

Topic link:
Portal

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

Chinese version:
Title description:
There are a total of N villages, numbered from 1 to N. You should build some roads so that every two villages can be connected. We say that two villages A and B are connected if and only if there is a road between A and B, or there is a village C that has a road between A and C, and C and B are connected.
We know that there are already some roads between some villages. Your job is to build some roads so that all the villages can be connected, and the length of all roads is the smallest.

Input: The
first line is an integer N (3 <= N <= 100), which represents the number of villages. In the next N lines, the i-th line contains N integers, and the j-th integer in the N integers represents the distance between the village i and the village j (the distance should be an integer within [1,1000]).
Then there is an integer Q (0 <= Q <= N * (N + 1) / 2), and then there are Q rows, each row contains two integers a and b (1 <= a <b <= N), this It means that the road between village a and village b has been built.

Output:
You should output a line containing an integer, which is the length of all roads to be built, so that all villages can be connected, this value is the smallest.

Sample input:
3
0 990 692
990 0 179
692 179 0
1
1 2

Sample output:
179

Sample diagram:
Insert picture description here

Kruskal algorithm c reference code:

#include <stdio.h>
#include <string.h>

#define VERTEXMAX 101
#define EDGEMAX 10100
 
int n,q;
int parent[VERTEXMAX];

typedef struct{
    
    
	int v1;
	int v2;
	int w;
}Road,*road;

Road a[EDGEMAX];

int cmp(const void *a,const void *b)
{
    
    
	road pa=(road)a;
	road pb=(road)b;
	int num1=pa->w;
	int num2=pb->w;
	return num1-num2;
}

void init()
{
    
    
	int i;
	for(i=1;i<=n;i++)
	 parent[i]=i;
}

int find(int x)
{
    
    
	if(x==parent[x])
	 return x;
	else
	 return parent[x]=find(parent[x]);
}

void kruskal(int num)
{
    
    
	int i,sum=0;
	
	for(i=0;i<num;i++)
	{
    
    
		int x=a[i].v1;
		int y=a[i].v2;
		int A=find(x);
		int B=find(y);
		if(A!=B)
		{
    
    
		   parent[A]=B;
		   sum+=a[i].w;
		}
	}
	printf("%d\n",sum);
}

int main()
{
    
    
	while(scanf("%d",&n)!=EOF)
	{
    
    
	   memset(a,0,sizeof(a));
	   init();
	   
	   int i,j,k,x,y,num=0;
	   for(i=1;i<=n;i++)
	   {
    
    
	   	for(j=1;j<=n;j++)
	   	{
    
    
	   		scanf("%d",&k);
	   		if(i>=j)
	   		 continue;
			a[num].v1=i;
	   		a[num].v2=j;
	   		a[num++].w=k;
		}
	   }
	   
	   qsort(a,num,sizeof(Road),cmp);
	   
	   scanf("%d",&q);
	   for(i=0;i<q;i++)
	  {
    
    
		scanf("%d%d",&x,&y);
		int A=find(x);
		int B=find(y);
		parent[A]=B;
	  }
	  
	   kruskal(num);
	}
	return 0;
} 

prim algorithm c reference code:

#include <stdio.h>
#include <string.h>

#define MAX 101
#define INF 0x3f3f3f3f
 
int dist[MAX];
int visited[MAX];
int map[MAX][MAX];
int n,q;

void prim(int s)
{
    
    
	memset(visited,0,sizeof(visited));
	int i,j,sum=0;
	for(i=1;i<=n;i++)
	 dist[i]=map[s][i];
	
	visited[s]=1;
	
	for(i=1;i<n;i++)
	{
    
    
		int min=INF,pos;
		for(j=1;j<=n;j++)
		{
    
    
			if(visited[j]==0&&dist[j]<min)
			{
    
    
				min=dist[j];
				pos=j;
			}
		}
		
		visited[pos]=1;
		sum+=min;
		
		for(j=1;j<=n;j++)
		{
    
    
			if(visited[j]==0&&dist[j]>map[pos][j])
			 dist[j]=map[pos][j];
		}
	}
	
	printf("%d\n",sum);
}

int main()
{
    
    
    int i,j,x,y;
	while(scanf("%d",&n)!=EOF)
	{
    
    	
		for(i=1;i<=n;i++)
		{
    
    
			for(j=1;j<=n;j++)
			{
    
    
			  scanf("%d",&map[i][j]);
			}
		}
		
		scanf("%d",&q);
		for(i=0;i<q;i++)
		{
    
    
			scanf("%d%d",&x,&y);
			map[x][y]=map[y][x]=0;
		}
		
		prim(1);
	}
	return 0;
}

Reference materials Portal:
prim algorithm solution:
portal
kruskal algorithm solution:
portal

Guess you like

Origin blog.csdn.net/qq_46139801/article/details/114481391