poj1679 The Unique MST(次小生成树)

The Unique MST
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 20475 Accepted: 7201

Description

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!
/*
更新,之前的都忘了。。。。加油!!
Tme:2015-1-15 12:27
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int MAX=300;
const int INF=0x3f3f3f3f;
int n,m;
int pre[MAX];
int g[MAX][MAX];
int dis[MAX],path[MAX][MAX];
bool vis[MAX],used[MAX][MAX];
void Init(int n){
    for(int i=0;i<=n;i++){
        for(int j=i;j<=n;j++){
            if(i==j)g[i][j]=0;
            else g[i][j]=g[j][i]=INF;

            used[i][j]=used[j][i]=0;
            path[i][j]=path[j][i]=0;
        }
        vis[i]=0;
    }
}

int Prim(){
   for(int i=1;i<=n;i++){
        dis[i]=g[1][i];
        pre[i]=1;
   }
   int Mst=0;
   vis[1]=true;
   for(int i=1;i<=n;i++){
        int u=1,minV=INF;
        for(int j=1;j<=n;j++){
            if(!vis[j]&&minV>dis[j]){
                minV=dis[j];
                u=j;
            }
        }
        if(minV==INF) return Mst;
           Mst+=dis[u];
        vis[u]=true;
        used[u][pre[u]]=used[pre[u]][u]=true;
        for(int j=1;j<=n;j++){
            if(vis[j]){
                //path[u][j]表示u到j的最大值
                //dis[u]是相当于这个集合到u的值,如果小于原来的话,之前就已经更新了
                //即u--j的最大值为(u的上一个值到j的最大值)和(到u的距离的值)取最大值,
                //因为都是已经在集合中的,相当于从往前倒着访问呢
                //比如1--2 2--3 3--4 u为4的时候,从1到3会挨着求到4的最大值,求1的时候path[3][1]的已经求出来了
                path[u][j]=path[j][u]=max(path[pre[u]][j],dis[u]);
            }
            if(!vis[j]&&g[u][j]<dis[j]){
                dis[j]=g[u][j];
                pre[j]=u;
            }
        }
   }
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d%d",&n,&m);
        Init(n);
        int u,v,w;
        while(m--){
            scanf("%d%d%d",&u,&v,&w);
            if(g[u][v]>w)
                g[u][v]=g[v][u]=w;
        }
        int Mst=Prim();
        int ans=INF;//初始化为NF
        for(int i=1;i<=n;i++){
            for(int j=1;j<=n;j++){
                if(i!=j&&!used[i][j]){
                   ans=min(ans,Mst-path[i][j]+g[i][j]);
                }
            }
        }
        //printf("%d\n",Mst);
        if(Mst!=ans){
            printf("%d\n",Mst);
        }else{
            puts("Not Unique!");
        }
    }
return 0;
}

/*
题目大意:问是否存在相同的两个最小生成树有的话输出权值,没有输出
心得:敲了好几次,能自己写下来了,查错一直没查出来,没找到求次小生成树应该从没访问过的边里边找,对着找出来,path没有初始化wa了一次,2015-1-15 11:50 加。。path[i][j]表示的是i到j的最大值。。每次都会从u开始更新,所以,path[pre[u]][j]相当于这个集合到j的最大值,不是1--j的最大值,因为dis会一直更新,也不是求得最短路
*/
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int INF=1<<25; 
const int MAX=102;
int N,M;
int map[MAX][MAX],path[MAX][MAX];
bool used[MAX][MAX],vis[MAX];
int pre[MAX],d[MAX];
int Prim(){
	int Mst=0;	
	memset(vis,0,sizeof(vis));
	memset(pre,0,sizeof(pre));
	memset(d,0,sizeof(d));
	for(int i=1;i<=N;i++){
		d[i]=map[1][i];
		pre[i]=1;
	}
	vis[1]=true;
	int u;
	for(int i=1;i<N;i++){
		int minV=INF;
		for(int j=1;j<=N;j++){
			if(!vis[j]&&minV>d[j]){
				minV=d[j]; 
				u=j;
			}
		}
		if(minV==INF)return Mst;
		vis[u]=true;
		Mst+=d[u];
		//printf("%d\n",d[u]);
		used[u][pre[u]]=used[pre[u]][u]=true;
		for(int j=1;j<=N;j++){
			if(vis[j]){//可以不加 j!=u
				path[u][j]=path[j][u]=max(path[pre[u]][j],d[u]);
			}
			if(!vis[j]){
				if(d[j]>map[u][j]){
					pre[j]=u;
					d[j]=map[u][j];
				}
			}
		} 
	}
	return Mst;
}
int main(){
	int T;
	scanf("%d",&T);
	while(T--){
		scanf("%d%d",&N,&M);
		for(int i=1;i<=N;i++){//Initial
			for(int j=i;j<=N;j++){
				if(i!=j)
				map[i][j]=map[j][i]=INF;
				else
				map[i][j]=0;
				
				path[j][i]=path[i][j]=0;
				used[i][j]=used[j][i]=0;
			}
		}
		int a,b,c;
		for(int i=1;i<=M;i++){
			scanf("%d%d%d",&a,&b,&c);
			map[b][a]=map[a][b]=c;
		}
		int Mst=Prim();
	//	printf("%d\n",Mst);
		int ans=INF;
		for(int i=1;i<=N;i++){
			for(int j=1;j<=N;j++){
				if(i!=j&&!used[i][j])
				//这儿是从没有访问过的边里边找一条不是从访问过的找 无语 
				ans=min(ans,Mst+map[i][j]-path[i][j]);
			}
		}
		//printf("%d\n",ans);
		if(ans!=Mst)
			printf("%d\n",Mst);
		else
			printf("Not Unique!\n");
	}
return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值