POJ1679 The Unique MST次小生成树

2/3转载:http://www.cnblogs.com/Howe-Young/p/4911992.html

次小生成树,就是求除了最小生成树之外最小的那个生成树。

下面介绍一下利用prim求次小生成树的主要步骤。

1.先求出来最小生成树。并将最小生成树任意两点之间路径当中的权值最大的那一条找出来,为什么要找最大的呢,因为生成树加入一条边之后一定构成了回路,那么肯定要去掉这个回路当中一条边才是生成树,那么,怎么去边才是次小的,那就去掉除了刚刚添加的一条边之外回路当中权值最大的一个,所以留下的就是最小的。

2.枚举最小生成树外的每一条边。找出最小的就是次小生成树。

POJ1679 The Unique MST

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!

题意:给定图,让求它的最小生成树是否唯一。如果唯一的话输出最小生成树的权值和,否则输出Not Unique!

思路:直接求次小生成树就行。

这里写图片描述
预处理出每两个点的最小瓶颈prim(同时可以算出来MST),去掉最小瓶颈路,加上map[i][j],计算所有的这些情况,取最小。例如去8添9.
计算最小瓶颈路的时候之前用的LCA,logn,但是计算出所有点的最小瓶颈路,然后还要算出最小生成树,复杂度和写法不如prim.

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
const int maxn = 111;
const int inf = 0x3f3f3f3f;
int Map[maxn][maxn];//邻接矩阵存图
int Max[maxn][maxn];//表示最小生成树中i到j的最大边权
bool used[maxn][maxn];//判断该边是否加入最小生成树
int pre[maxn];
int dis[maxn];
void init(int n)
{
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            if (i == j) Map[i][j] = 0;
            else Map[i][j] = inf;
}
void read(int m)
{
    int u, v, w;
    for (int i = 0; i < m; i++)
    {
        scanf("%d %d %d", &u, &v, &w);
        Map[u][v] = Map[v][u] = w;
    }
}
int prim(int n)
{
    int ans = 0;
    bool vis[maxn];
    memset(vis, false, sizeof(vis));
    memset(used, false, sizeof(used));
    memset(Max, 0, sizeof(Max));
    for (int i = 2; i <= n; i++) 
    {
        dis[i] = Map[1][i];
        pre[i] = 1;
    }
    pre[1] = 0;
    dis[1] = 0;
    vis[1] = true;
    for (int i = 2; i <= n; i++)
    {
        int min_dis = inf, k;
        for (int j = 1; j <= n; j++)
        {
            if (!vis[j] && min_dis > dis[j])
            {
                min_dis = dis[j];
                k = j;
            }
        }
        if (min_dis == inf) return -1;//如果不存在最小生成树
        ans += min_dis;
        vis[k] = true;
        used[k][pre[k]] = used[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]], dis[k]);
            if (!vis[j] && dis[j] > Map[k][j])
            {
                dis[j] = Map[k][j];
                pre[j] = k;
            }
        }
    }
    return ans;//最小生成树的权值之和
}
int smst(int n, int min_ans)//min_ans 是最小生成树的权值和
{
    int ans = inf;
    for (int i = 1; i <= n; i++)
        for (int j = i + 1; j <= n; j++)
            if (Map[i][j] != inf && !used[i][j])//枚举最小生成树之外的边
                ans = min(ans, min_ans + Map[i][j] - Max[i][j]);
    if (ans == inf) return -1;
    return ans;
}
void solve(int n)
{
    int ans = prim(n);
    if (ans == -1)
    {
        puts("Not Unique!");
        return;
    }
    if (smst(n, ans) == ans)
    printf("Not Unique!\n");
    else
    printf("%d\n", ans);
}

int main()
{
    int T, n, m;
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d %d", &n, &m);
        init(n);
        read(m);
        solve(n);
    }
    return 0;
}

Self,已A

//有没有重边? 
#include<cstring>
#include<iostream>
#include<cstdio>
#define maxn 105
#define INF 0x3f3f3f3f
using namespace std; 
int head[maxn],maxcost[maxn][maxn],map[maxn][maxn];
bool used[maxn][maxn];//m 
//struct Edge{
//  int to,v,next;
//}edge[?*2];
//void add(int x,int y,int z)
//{
//  cnt++;
//  edge[cnt].to=y;
//  edge[cnt].v=z;
//  edge[cnt].next=head[x];
//  head[x]=cnt;
//}
int n,m,t,cnt,x,y,z,vis[maxn],d[maxn],pre[maxn];
int prim(int s)
{
    int res=0;
    memset(maxcost,0,sizeof(maxcost));
    for(int i=1;i<=n;i++)
        vis[i] = 0, d[i] = INF, pre[i]=i;

    d[s]=0;
    for(int i=0;i<n;i++)//循环计数 
    {
        int mn=INF, index=-1;
        for(int j=1;j<=n;j++)
        {
            if(!vis[j] && d[j]<mn)
            {
                mn=d[j];
                index=j;
            }
        }
        if(index==-1)
            break;

        res+=mn;
        vis[index]=1;
        used[index][pre[index]]=used[pre[index]][index]=1;

        for(int j=1;j<=n;j++)
            if(vis[j])//已经访问过的肯定是生成树的一部分 
                maxcost[index][j] = maxcost[j][index] = 
                    max(maxcost[pre[index]][j],d[index]);//pre[index]一定在生成树中    

        for(int j=1;j<=n;j++)//更新全部链接index的点的权值 
        {
            if(!vis[j] && map[index][j]<d[j])
            {
                d[j] = map[index][j];
                pre[j] = index;
            }
        }
    }
    return res;
}
int exprim(int n,int mins)
{
    int ans=INF;
    for(int i=1;i<=n;i++)//枚举最小生成树之外的边
        for(int j=i+1;j<=n;j++)
            if(map[i][j]!=INF && !used[i][j])
            {
                ans=min(ans,mins+map[i][j]-maxcost[i][j]);
            }
    if(ans==INF) return -1;
    return ans;
}
void judge(int n)
{
    int ans=prim(n);
    if (ans == -1)
    {
        puts("Not Unique!");
        return;
    }
    if (exprim(n, ans) == ans)
    printf("Not Unique!\n");
    else
    printf("%d\n", ans);
}
int main()
{
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        cnt=0;
        memset(head,0,sizeof head);
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                map[i][j]=map[j][i]=INF; 
        memset(used,0,sizeof used);
        for(int i=1;i<=n;i++)
            map[i][i]=0; 
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&x,&y,&z);
            map[x][y]=z;
            map[y][x]=z; 
        }
        judge(n);
    }
} 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值