POJ-1679(The Unique MST) 次小生成树

POJ-1679 次小生成树算法解析
本文详细介绍了求解次小生成树问题的一个具体算法实例,通过POJ-1679题目进行阐述。利用Prim算法寻找最小生成树,并进一步判断最小生成树与次小生成树的关系,实现对特定情况的处理。

POJ-1679

hint:
次小生成树,判断最小生成树和次小生成树是否相等, 若相等则not unique

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int MAXN = 105;
const int INF = 0x3f3f3f3f;
int cost[MAXN][MAXN];
int lowcost[MAXN];
int MAX[MAXN][MAXN];    //MST 中i->j权值最大的边
int used[MAXN][MAXN];
int pre[MAXN];
int visit[MAXN];


int prim(int n){
    memset(used, 0, sizeof(used));
    memset(MAX, 0, sizeof(MAX));
    memset(visit, 0, sizeof(visit));
    for(int i = 1; i <= n; ++i) lowcost[i] = cost[1][i], pre[i] = 1;
    visit[1] = 1;
    pre[1] = -1;
    lowcost[1] = 0;
    int res1 = 0;   //最小生成树权值
    int res2 = INF; //次小生成树权值
    for(int i = 2; i <= n; ++i)
    {
        int minc = INF;
        int flag = -1;
        for(int j = 1; j <= n; ++j){
            if(!visit[j] && minc > lowcost[j]){
                minc = lowcost[j];
                flag = j;
            }
        }
        if(flag == -1) return -1;
        res1 += minc;
        visit[flag] = 1;
        used[flag][pre[flag]] = used[pre[flag]][flag] = 1;
        for(int j = 1; j <= n; ++j)
        {
            if(visit[j]) MAX[j][flag] = MAX[flag][j] = max(MAX[j][flag], lowcost[flag]);
            if(!visit[j] && lowcost[j] > cost[flag][j])
            { 
                lowcost[j] = cost[flag][j];
                pre[j] = flag;
            }
        }
    }

    for(int i = 1; i <= n; ++i)
        for(int j = i + 1; j <= n; ++j){
            //cout << i << " " << j << " " << used[i][j] << endl;
            if(cost[i][j] != INF && !used[i][j]){
                res2 = min(res2, res1 - MAX[i][j] + cost[i][j]);
            }
        }
    //cout << "r1 " << res1 << " r2 " << res2 << endl;
    if(res1 == res2) return -2;
    else return res1;
}

int main(){
    int t, n, m;
    scanf("%d", &t);
    while(t--){
        int a, b, c;
        scanf("%d%d", &n, &m);
        for(int i = 0; i < MAXN; ++i)
            for(int j = 0; j < MAXN; ++j) cost[i][j] = (i == j) ? 0 : INF;
        for(int i = 0; i < m; ++i)
        {
            scanf("%d%d%d", &a, &b, &c);
            if(cost[a][b] > c){
                cost[a][b] = cost[b][a] = c;
            }
        }
        int t = prim(n);
        if(t == -2) printf("Not Unique!\n");
        else printf("%d\n", t);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值