1. 程式人生 > >次小生成樹-------K - The Unique MST

次小生成樹-------K - 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!

次小生成樹的演算法解釋:
這兩種演算法的思路都是相同的,首先求出最小生成樹,我們列舉每條不在最小生成樹上的邊,並把這條邊放到最小生成樹上面,然後就一定會形成環,那麼我們在這條環路中取出一條最長的邊權(除了新加入的那一條邊)。用這個這條邊取代那個最長邊權,最終我們得到的權值就是次小生成樹的權值。

演算法實現
prim演算法實現:
我們在求解最小生成樹的時候我們要使用一個二位陣列maxd[i][j]表示最小生成樹中i點到j點的最大邊權,我們使用動態規劃的思想來計算這個陣列,比如當前節點為x,他的父親節點為per[x],以及根節點root,那麼
maxd[root][x] = max(maxd[root][per[x]] , maxd[per[x]][x]);

我們就會得到最終的結果陣列
我們還需要陣列:connect[i][j]表示最小生成樹中這條邊有沒有被用到,剩下的就是我們要去模擬演算法解釋裡所說的刪邊以及添邊的操作了

這道題,算出最小生成樹與次小生成樹,如果相等,就存在多條。。。。

就當總結個次小生成樹的模板

#include<iostream>
#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<cmath>
using namespace std;
typedef long long LL;
const int inf = 0x3f3f3f3f;
const double eps = 1e-8;
const double PI = acos(-1);
#define pb push_back
#define mp make_pair
#define fi first
#define se second
const int N = 105;

int lowcost[N];
int mk[N][N];
bool vis[N];
int pre[N];
bool use[N][N];
int MAX[N][N];
int n,m;

int prim()
{
    memset(vis,false,sizeof(vis));
    memset(MAX,0,sizeof(MAX));
    memset(use,false,sizeof(use));
    for(int i = 1;i <= n;++i){
        lowcost[i] = mk[1][i];
        pre[i] = 1;
    }
    lowcost[1] = 0;
    vis[1] = true;
    pre[1] = -1;
    int ans = 0;
    for(int i = 0;i < n - 1;++i)
    {
        int MIN = inf;
        int k = 0;
        for(int j = 1;j <= n;++j){
            if(!vis[j] && MIN > lowcost[j]){
                MIN = lowcost[j];
                k = j;
            }
        }
        if(MIN == inf){
            return -1;
        }
        vis[k] = true;
        ans += MIN;
        use[k][pre[k]] = use[pre[k]][k] = true;
        for(int j = 1;j <= n;++j){
            if(vis[j]) MAX[j][k] = MAX[k][j] = max(MAX[j][pre[k]],MIN);
            if(!vis[j] && lowcost[j] > mk[k][j]){
                lowcost[j] = mk[k][j];
                pre[j] = k;
            }
        }
    }
    return ans;
}

int ans;
int smst()
{
    int MIN = inf;
    for(int i = 1;i <= n;++i){
        for(int j = i + 1;j <= n;++j){
            if(!use[i][j] && mk[i][j] != inf){
                MIN = min(MIN,ans - MAX[i][j] + mk[i][j]);
            }
        }
    }
    //不存在第二棵生成樹
    if(MIN == inf) return -1;
    return MIN;
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d %d",&n,&m);
        memset(mk,inf,sizeof(mk));
//        for(int i = 1;i <= n;++i){
//            printf("%s\n",s[i]);
//        }
        for(int i = 0;i < m;++i){
            int u,v,w;
            scanf("%d %d %d",&u,&v,&w);
            if(mk[u][v] > w){
                mk[u][v] = mk[v][u] = w;
            }
        }
        ans = prim();
        if(ans == -1){
            printf("Not Unique!\n");
            continue;
        }
        if(ans == smst()){
            printf("Not Unique!\n");
        }else{
            printf("%d\n",ans);
        }
    }
    return 0;
}