HDU 1233 or expedite project report solving the minimum spanning tree

HDU 1233 or engineering problem-solving flow report

Problem-solving ideas: the basic minimum spanning tree problem, I wrote about both methods, see the comments now.
1.prim algorithm. The core idea of this algorithm is the beginning of a contribution from the first point, find the nearest point from each tree to join the tree.
Here Insert Picture Description

#include<iostream>
#include<math.h>
#include<iomanip>
#include<algorithm>
#include<iostream>
#include<math.h>
#include<iomanip>
#include<algorithm>
#include<queue>
#include<cstring>
#include<string>
#include<map>
#include<stack>
#include<stdio.h>
#include<cstdio>
#include<stdlib.h>
#include<fstream>
#include<iomanip>
#include<vector>
#pragma warning(disable:4996)
#define INF 0x3f3f3f3f
#define ll long long
#define PI acos(-1.0)
const int N = 200010;
const int maxn = 1e5+10;
using namespace std;
int  d[105];//放每个点到树的最短距离
int visit[105];
int edge[105][105];
void prim(int n)
{
	for (int i = 1; i <= n; i++)//一开始的树就是一个点
		d[i] = edge[1][i];
	int ans = 0;
	d[1] = 0;
	visit[1] = 1;
	for (int i = 2; i <= n; i++)//进行n-1次操作,i只是计数用
	{
		int u = INF;
		int pos;
		for (int j = 1; j <= n; j++)
		{
			if (!visit[j] && u > d[j])//每次都在没用过的点中找与已建成的树距离最短的点
			{
				u = d[j];
				pos = j;
			}
		}
		visit[pos] = 1;
		ans += u;
		for (int j = 1; j <= n; j++)
		{
			if (!visit[j] && d[j] > edge[pos][j])
			{
				d[j] = edge[pos][j];//更新没用过的点到树的最短距离
			}
		}
	}
	printf("%d\n", ans);
}
int main()
{
	int n;
	while (~scanf("%d", &n) && n)
	{
		memset(visit, 0, sizeof(visit));
		int m = (n - 1) * n / 2;
		int u, v, w;
		while (m--)
		{
			scanf("%d%d%d", &u, &v, &w);
			edge[u][v] = edge[v][u] = w;//注意是无向图
		}
		prim(n);
	}
	return 0;
}



2.kruskal algorithm. The core idea of ​​this algorithm is that each forest the last two points of connection does not communicate, and finally into a tree.
Here Insert Picture Description

#include<iostream>
#include<math.h>
#include<iomanip>
#include<algorithm>
#include<iostream>
#include<math.h>
#include<iomanip>
#include<algorithm>
#include<queue>
#include<cstring>
#include<string>
#include<map>
#include<stack>
#include<stdio.h>
#include<cstdio>
#include<stdlib.h>
#include<fstream>
#include<iomanip>
#include<vector>
#pragma warning(disable:4996)
#define INF 0x3f3f3f3f
#define ll long long
#define PI acos(-1.0)
const int N = 200010;
const int maxn = 1e5+10;
using namespace std;
int father[maxn];
struct node {
	int u, v, val;
}edge[maxn];
bool cmp(node a, node b)
{
	return a.val < b.val;
}
int find(int x)
{
	if (x == father[x])
		return x;
	else
		return father[x] = find(father[x]);
}
void kruskal(int n, int m)
{
	sort(edge + 1, edge + m + 1, cmp);
	for (int i = 1; i <= n; i++)
		father[i] = i;
	int ans = 0;
	for (int i = 1; i <= m; i++)
	{
		int a = find(edge[i].u);
		int b = find(edge[i].v);
		if (a != b)//用并查集的方式判断两个点是否连通
		{
			father[a]=b;
			ans += edge[i].val;
		}
	}
	printf("%d\n", ans);
}
int main()
{
	int n;
	while (~scanf("%d", &n) && n)
	{
		int m = (n - 1) * n / 2;
		for (int i = 1; i <= m; i++)
		{
			scanf("%d%d%d", &edge[i].u, &edge[i].v, &edge[i].val);
		}
		kruskal(n, m);
	}
	return 0;
}



Published 64 original articles · won praise 0 · Views 1443

Guess you like

Origin blog.csdn.net/weixin_45566331/article/details/104928733