poj 1679 The Unique MST,次小產生樹,pojmst

來源:互聯網
上載者:User

poj 1679 The Unique MST,次小產生樹,pojmst

次小產生樹
求最小產生樹時,用數組Max[i][j]來表示MST中i到j的最大邊權。
求完後,直接枚舉所有不在MST中的邊,替換掉最大邊權的邊,更新答案。


#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 110;const int INF = 1e9;bool vis[maxn];int d[maxn];int pre[maxn];int Max[maxn][maxn];bool used[maxn][maxn];int g[maxn][maxn];int n, m;int Prim(){    int ans = 0;    memset(vis, false, sizeof vis );    memset(Max, 0, sizeof Max );    memset(used, false, sizeof used );    vis[0] = true;    pre[0] = -1;    for(int i=1; i<n; ++i) {        d[i] = g[0][i];        pre[i] = 0;    }    for(int i=1; i<n; ++i) {        int p = -1;        for(int j=0; j<n; ++j) if(!vis[j] && (p==-1||d[p]>d[j]))                p = j;        if(-1==p) return -1;        ans += d[p];        vis[p] = true;        used[p][pre[p]] = used[pre[p]][p] = true;        for(int j=0; j<n; ++j) {            if(vis[j]) Max[j][p] = Max[p][j] = max(Max[j][pre[p]], d[p]);            if(!vis[j]&&d[j]>g[p][j]) {                d[j] = g[p][j];                pre[j] = p;            }        }    }    return ans;}void solve(){    int res = Prim();    if(-1 == res) { //圖不連通        puts("Not Unique!");        return ;    }    int Mn = INF;    for(int i=0; i<n; ++i)        for(int j=i+1; j<n; ++j)            if(g[i][j]!=INF && !used[i][j]) {                Mn = min(Mn, g[i][j]-Max[i][j]);            }    if(Mn==INF || Mn!=0){ //不存在次小產生樹 或 不相等        printf("%d\n", res);    }else {        puts("Not Unique!");    }}int main(){    int T;    scanf("%d", &T);    while(T--) {        scanf("%d%d", &n, &m);        for(int i=0; i<n; ++i) for(int j=0; j<n; ++j) g[i][j] = INF;        int x, y, z;        while(m--) {            scanf("%d%d%d", &x, &y, &z);            x--;            y--;            g[x][y] = g[y][x] = z;        }        solve();    }    return 0;}



最小產生樹是否唯一解答

摘要:最小產生樹是圖論的經典問題,求最小產生樹以及求最小產生樹的權值和得到了足夠關注,而很少人去研究。對於給定的圖而言,因為最小產生樹的權值和是確定的,所以最小產生樹不唯一若且唯若最小產生樹的形狀不唯一。本文提出判斷的三種方法並且對它們給予分析和評價。關鍵詞:最小產生樹;唯一;prim演算法;kruskal演算法;次小產生樹中圖分類號:TP301.6 文獻標識碼:A文章編號:1007-9599 (2011)06-0000-02Minimum Spanning Tree If the Unique Wu Yuliang,Kong Fanlong(Central China Normal University,Wuhan430079,China)Abstract:Minimum spanning tree is a classic problem of graph theory,find the minimum spanning tree minimum spanning tree,and find the weight and get enough attention,and few people to study the minimum spanning tree is unique.For a given graph is concerned,because the weights and the minimum spanning tree is determined,so the minimum spanning tree is not unique and only if the shape of the minimum spanning tree is not unique.Determine whether the proposed minimum spanning tree,and the only three ways to give their analysis and evaluation.Keywords:Minimum spanning tree;Unique;Prim algorithm;Kruskal algorithm;Small spanning tree一、三種方法判斷最小產生樹(MST)是否唯一(一)藉助prim演算法提出的方法prim演算法的基本思想是:首先選取圖中的任意一個頂點v作為樹的根加入產生樹的集合Q中,之後不斷往產生樹中(集合Q中)添加頂點w,頂點w滿足與集合Q中的某個頂點之間有邊,且該邊上的權值是此時所有串連集合Q中的結點與不在集合Q中的結點的邊中權值最小的,如此加入n-1個結點後,就形成了MST。(剩餘3202字)
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.