POJ1679 The Unique MST —— 次小生成树

xiaoxiao2021-02-28  122

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

The Unique MST Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 31378 Accepted: 11306

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!

Source

POJ Monthly--2004.06.27 srbga@POJ

题解:

问:最小生成树是否唯一。

次小生成树模板题。

代码如下:

#include <iostream> #include <cstdio> #include <algorithm> #include <cstring> using namespace std; typedef long long LL; const double EPS = 1e-6; const int INF = 2e9; const LL LNF = 9e18; const int MOD = 1e9+7; const int MAXN = 1e2+10; int n, m; int cost[MAXN][MAXN]; int lowc[MAXN], vis[MAXN], used[MAXN][MAXN], pre[MAXN], Max[MAXN][MAXN]; int Prim() { int ret = 0; memset(vis,0,sizeof(vis)); memset(used,0,sizeof(used)); memset(Max,0,sizeof(Max)); lowc[1] = 0; pre[1] = -1; vis[1] = 1; for(int i = 2; i<=n; i++) { lowc[i] = cost[1][i]; pre[i] = 1; } for(int i = 2; i<=n; i++) { int k, minn = INF; for(int j = 1; j<=n; j++) if(!vis[j] && minn>lowc[j]) minn = lowc[k=j]; if(minn==INF) return -1; vis[k] = 1; ret += minn; used[k][pre[k]] = used[pre[k]][k] = 1; for(int j = 1; j<=n; j++) { if(vis[j]) Max[j][k] = Max[k][j] = max(Max[j][pre[k]], lowc[k]); if(!vis[j] && lowc[j]>cost[k][j]) { lowc[j] = cost[k][j]; pre[j] = k; } } } return (ret==INF)?-1:ret; } int t1, t2; int SMST() { int ret = INF; for(int i = 1; i<=n; i++) for(int j = i+1; j<=n; j++) { if(cost[i][j]!=INF && !used[i][j]) ret = min(ret, t1+cost[i][j]-Max[i][j]); } return ret; } int main() { int T; cin>>T; while(T--) { scanf("%d%d",&n,&m); for(int i = 1; i<=n; i++) for(int j = 1; j<=n; j++) cost[i][j] = (i==j)?0:INF; for(int i = 1; i<=m; i++) { int u, v, w; cin>>u>>v>>w; cost[u][v] = cost[v][u] = w; } t1 = Prim(); t2 = SMST(); if(t1!=-1 && t2!=-1 && t1!=t2) printf("%d\n", t1); else printf("Not Unique!\n"); } }

转载请注明原文地址: https://www.6miu.com/read-64591.html

最新回复(0)