poj 1679 The Unique MST 次小生成树

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!

题意:给出n条边,以及每条边对应的权值,判断是否存在唯一的最小生成树。

思路:先进行一次最小生成树计算,同时记录构建这棵最小生成树的每一条边,然后进行删边操作,每次在图中删除最小生成树内的一条边,判断剩下的点能否构成继续构成一棵最小生成树,同时这个新的最下生成树的权值是否和第一次判断的权值相等,如果相等,最下生成树不唯一,如果把所有的边都删过一次,所得的最小生成树的权值都和第一次不想等,最下生成树唯一。


emmmmm,Kurskal 实现的话比 prime 简单,但是Kurskal 不是太熟悉,死磕prime嗑了半天,终于知道咋记录了...

下边代码:

#include<stdio.h>
#include<string.h>
int s[1100][1100];//存图 
int dis[1100][3];//存储更新的值及更新的下标 
int dis1[1100];
int next[1100][4];//记录初始数据构建最小生成树时的坐标 
int book[1100];
int tree(int n)//判断图是否能够构建最小生成树 
{
	int i,j;
	int min,sum;
	int v;
	sum=0;
	memset(book,0,sizeof(book));
	for(i=1;i<=n;i++)
	{
		dis[i][1]=s[1][i];//存储图第一行内数据 
		dis[i][2]=1; 	//记录当前第i个数据来自第一行 
	}
	
	book[1]=1;
	int p=1;
	int q=1;
	for(i=1;i<n;i++)
	{
		min=1e9;
		for(j=1;j<=n;j++)
		{		
			if(dis[j][1]<=min&&book[j]==0)
			{
				min=dis[j][1];//记录dis中的最小值,用于下一次查找 
				v=j;
			}
		}
		next[p][1]=dis[v][2];//next记录第一次找到的最小值得横纵坐标 
		next[p++][2]=v;		 //即i行j列	
		q=v;
		sum+=dis[v][1];
		book[v]=1;
		for(j=1;j<=n;j++)
		{
			if(dis[j][1]>s[v][j]&&book[j]==0)
			{
				dis[j][1]=s[v][j];
				dis[j][2]=v;//当找到的第v行能更新dis数组时,同时记录更新的数字来自第几行 
			}
		}
	}
	return sum;

}

int tree1(int n)//删边判断是否存在次小生成树等于最小生成树 
{
	int i,j;
	int min,sum;
	int v;
	sum=0;
	memset(book,0,sizeof(book));
	for(i=1;i<=n;i++)
		dis1[i]=s[1][i];//记录图第一行 数据 
	
	book[1]=1;
	for(i=1;i<n;i++)//下边进行最小生成时模板判断即可 
	{
		min=1e9;
		for(j=1;j<=n;j++)
		{		
			if(dis1[j]<=min&&book[j]==0)
			{
				min=dis1[j];
				v=j;
			}
		}
		sum+=dis1[v];
		book[v]=1;
		for(j=1;j<=n;j++)
		{
			if(dis1[j]>s[v][j]&&book[j]==0)
			{
				dis1[j]=s[v][j];
			}
		}
	}
	return sum;

}

int main()
{
	int i,j,k,m,n;
	int T,t,u,v,w;
	scanf("%d",&T);
	while(T--)
	{
		memset(next,0,sizeof(next));
		scanf("%d%d",&n,&m);
		for(i=1;i<=n;i++)//初始化图 
		{
			for(j=1;j<=n;j++)
			{
				if(i==j)
					s[i][j]=0;
				else
					s[i][j]=1e9;
			}
		}
		for(i=1;i<=m;i++)//读取数据构建图 
		{
			scanf("%d%d%d",&u,&v,&w);
			s[u][v]=w;
			s[v][u]=w;
		}
		int flag=0;
		int sum=tree(n);//先进行最小生成树操作 
		int sum2;//记录删边之后的图每次最小生成树操作的结果 
		for(i=1;i<n;i++)//重点来了,想了半天终于找到如何记录 
		{
			//第一次构建图时每次找到的最小值对应的横纵坐标存在next中
			int x=s[next[i][1]][next[i][2]]; //记录删除的点的权值,用于还原 
			s[next[i][1]][next[i][2]]=1e9;//删边 
			s[next[i][2]][next[i][1]]=1e9;//删边 
			sum2=tree1(n);		//构建最小生成树 
			if(sum2==sum)//判断删边之后的图构建的树是否和之前的一样 
			{
				flag=1;
				break;
			}	
			s[next[i][1]][next[i][2]]=x;//还原边 
			s[next[i][2]][next[i][1]]=x;
		}
		if(flag==0)
		{
			printf("%d\n",sum);
		}
		else
		{
			printf("Not Unique!\n");
		}
	}
	return 0;
}



猜你喜欢

转载自blog.csdn.net/seven_deadly_sins/article/details/80395437