poj_1679 The Unique MST

The Unique MST

Time Limit: 1000MS

Memory Limit: 10000K

Total Submissions: 14582

Accepted: 5035

题目连接:http://poj.org/problem?id=1679

Description

Given a connected undirectedgraph, 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 thefollowing 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 thespanning tree that has the smallest total cost. The total cost of T means thesum of the weights on all the edges in E'.

Input

The first line contains asingle integer t (1 <= t <= 20), the number of test cases. Each caserepresents 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 linescontains a triple (xi, yi, wi), indicating that xi and yi are connected by anedge with weight = wi. For any two nodes, there is at most one edge connectingthem.

Output

For each input, if the MST isunique, print the total cost of it, or otherwise print the string 'NotUnique!'.

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!

Source

POJMonthly--2004.06.27 srbga@POJ

题意:

         判断最小生成树是否唯一,即是否有两个一模一样的最小生成树的值

解题思路:

         这是一个求次小生成树的题目,可以依照求最小生成树的方法来求,但是求最小生成树的时候要保存该生成树的每一条边,在删除某一条边后在求最小生成树,比较是否相等

代码:

//求次小生成树

/*

       首先求出原图最小生成树,记录权值之和为MinST。枚举添加每条不在最小生成树上的边(u,v)

       加上以后一定会形成一个环。找到环上权值第二大的边(即除了(u,v)以外的权值最大的边),把

       它删掉,计算当前生成树的权值之和。取所有枚举修改的生成树权值之和的最小值,就是次小生

       成树。

*/

#include <iostream>
#include<cstdio>
#include<cstring>
#define MAX 200
#define VALUE 0xfffffff
using namespace std;

//存储最小生成树的边
struct des
{
	int w;
	int s;
	int e;
};

int g[MAX][MAX];
int visited[MAX];
des minCost[MAX];
int v,e;

//求最小生成树
int prim()
{
    int i;
    for(i=0;i<=v;i++)
	{
        minCost[i].w=VALUE;
		minCost[i].s=0;
		minCost[i].e=0;
		visited[i]=0;
	}
    minCost[1].w=0;
    int res=0;
    while(true)
    {
        int t=-1;
		int a=0;
        for(i=1;i<=v;i++)
        {
            if(visited[i]==0 && (t==-1 || minCost[i].w<minCost[t].w))
			{
                t=i;
				minCost[t].e=minCost[i].e;
				minCost[t].s=minCost[i].s;
				minCost[t].w=minCost[i].w;
			}
        }
        //每条边都遍历完
        if(t==-1)
            break;
        //访问t点
        visited[t]=1;
		if(minCost[t].w==VALUE)
			return -1;//不连通
        res+=minCost[t].w;
        //printf("%d\t",minCost[t].w);
        for(i=1;i<=v;i++)
        {
            if(minCost[i].w>g[t][i] && g[t][i]!=0)
            {
                minCost[i].w=g[t][i];
				minCost[i].s=t;
				minCost[i].e=i;
            }

        }
    }
    return res;
}


void secondShortPath()
{
    int path1=prim();//最短路径
	if(path1==-1 || path1==0)
	{
		printf("0\n");
		return ;
	}
    int i;

	//枚举最小生成树的每一条边,
	des tempCost[MAX];
	//保存原来的值
	for(i=0;i<=v;i++)
	{
		tempCost[i]=minCost[i];
	}
	for(i=1;i<=v;i++)
	{
		if(tempCost[i].s!=0 && tempCost[i].e!=0)
		{
			//假设最小生成树中某一条边没了,再求最小生成树,看是否还相等
			g[tempCost[i].s][tempCost[i].e]=0;//minCost值发生了改变
			g[tempCost[i].e][tempCost[i].s]=0;
			int path2=prim();
			if(path2==-1)//不连通
				continue;
			if(path1==path2)//最小生成树不唯一
			{
				printf("Not Unique!\n");
				return ;
			}
			//还原之前删除的边
			g[tempCost[i].s][tempCost[i].e]=tempCost[i].w;
			g[tempCost[i].e][tempCost[i].s]=tempCost[i].w;
		}
	}

    printf("%d\n",path1);
}

int main()
{
	int ts;
	scanf("%d",&ts);
	while(ts--)
	{
		memset(g,0,sizeof(g));

		scanf("%d%d",&v,&e);
		int i;
		int start,end,weight;
		for(i=0;i<e;i++)
		{
			scanf("%d%d%d",&start,&end,&weight);
			g[start][end]=weight;
			g[end][start]=weight;
		}
		secondShortPath();
	}

    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

疯的世界

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值