The Unique MST(判断最小生成树是否唯一)C语言

Given a connected undirected graph, tell if its minimum spanning tree is unique.

Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of G is a subgraph of G, say T = (V', E'), with the following properties:
1. V' = V.
2. T is connected and acyclic.

Definition 2 (Minimum Spanning Tree): Consider an edge-weighted, connected, undirected graph G = (V, E). The minimum spanning tree T = (V, E') of G is the spanning tree that has the smallest total cost. The total cost of T means the sum of the weights on all the edges in E'.

Input
The first line contains a single integer t (1 <= t <= 20), the number of test cases. Each case represents a graph. It begins with a line containing two integers n and m (1 <= n <= 100), the number of nodes and edges. Each of the following m lines contains a triple (xi, yi, wi), indicating that xi and yi are connected by an edge with weight = wi. For any two nodes, there is at most one edge connecting them.
Output
For each input, if the MST is unique, print the total cost of it, or otherwise print the string ‘Not Unique!’.
Sample Input

2
3 3
1 2 1
2 3 2
3 1 3
4 4
1 2 2
2 3 2
3 4 2
4 1 2

Sample Output

3
Not Unique!

prim算法判断最小生成树是否唯一:如果已经加入生成树的点中有两个或者以上的点,到即将加入生成树的点的最小权值相等,则生成树不唯一。

#include<stdio.h>
int main()
{
	int t, i, j;
	int dis[200];
	int map[105][105];
	int n, m,a,b,c;
	int min;
	scanf("%d", &t);
	while (t--)
	{
		int count=0, sum = 0, f=0;
		int book[200] = { 0 };
		scanf("%d%d", &n, &m);
		for (i = 1; i <= n; i++)
		{
			for (j = 1; j <= n; j++)
			{
				if (i == j)
					map[i][j] = 0;
				else
					map[i][j] = 9999999;
			}
		}
		for(i=1; i<=m; i++)
		{
			scanf("%d%d%d",&a, &b, &c);
			map[a][b] = map[b][a] = c;
		}
		for (i = 1; i <= n; i++)
			dis[i] = map[1][i];
		count++;
		book[1] = 1;
		while (count < n)
		{
			min = 9999999;
			for (i = 1; i <= n; i++)
			{
				if (book[i] == 0 && dis[i] < min)
				{
					min = dis[i];
					j = i;
				}
			}
			int s = 0;
			for (i = 1; i <= n; i++)
				if (book[i] == 1 && min == map[i][j])//如果已经加入生成树的点中,到即将加入生成树的点的最小权值相等
					s++;
			if (s > 1)//当有两个或者以上的时候
			{
				f = 1;
				break;
			}
			count++, sum += dis[j], book[j]=1;
			int k;
			for (k = 1; k <= n; k++)
			{
				if (book[k] == 0 && dis[k] > map[j][k])
					dis[k] = map[j][k];
			}
		}
		if (f == 1)
			printf("Not Unique!\n");
		else
			printf("%d\n", sum);
	}
}
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Prim算法是一种用于求解最小生成树的经典算法,它的基本思想是从一个顶点开始,逐步扩展生成树的边,直到覆盖所有的顶点。下面是Prim算法的C语言实现: ```c #include <stdio.h> #include <stdbool.h> #define INF 9999 #define V 5 int minKey(int key[], bool mstSet[]) { int min = INF, min_index; for (int v = 0; v < V; v++) { if (mstSet[v] == false && key[v] < min) { min = key[v]; min_index = v; } } return min_index; } void printMST(int parent[], int graph[V][V]) { printf("Edge \tWeight\n"); for (int i = 1; i < V; i++) { printf("%d - %d \t%d \n", parent[i], i, graph[i][parent[i]]); } } void primMST(int graph[V][V]) { int parent[V]; int key[V]; bool mstSet[V]; for (int i = 0; i < V; i++) { key[i] = INF; mstSet[i] = false; } key[0] = 0; parent[0] = -1; for (int count = 0; count < V - 1; count++) { int u = minKey(key, mstSet); mstSet[u] = true; for (int v = 0; v < V; v++) { if (graph[u][v] && mstSet[v] == false && graph[u][v] < key[v]) { parent[v] = u; key[v] = graph[u][v]; } } } printMST(parent, graph); } int main() { int graph[V][V] = { {0, 2, 0, 6, 0}, {2, 0, 3, 8, 5}, {0, 3, 0, 0, 7}, {6, 8, 0, 0, 9}, {0, 5, 7, 9, 0} }; primMST(graph); return 0; } ``` 这段代码实现了Prim算法的逻辑。首先定义了一个`minKey`函数,用于找到key值最小的顶点。然后定义了`printMST`函数,用于打印最小生成树的边和权重。接着是`primMST`函数,其中初始化了key、mstSet和parent数组,并进行了主要的Prim算法逻辑。最后,在`main`函数中定义了一个图的邻接矩阵,并调用`primMST`函数求解最小生成树

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值