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

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

解题思路:
找出次小生成树。

先求出最小生成树,把树中的每条边都标记用过(used数组)。要想求得次小生成树肯定是拿一条不在最小生成树中的边去替换最小生成树的一条边。
那么应该替换哪一条边?

当在最小生成树中加入一条边时,必会构成回路,加入的这条边一定是大于等于路径上权值最大的边。(不然这条边一开始就是最小生成树里的边了)

当加入一条没有用过的边(设这条边连接的是i和j)时,在最小生成树中i到j就形成了回路。这时i到j有两条路径:一条是本来最小生成树里的路径,一条是新加入的边。那么要在这个回路里删去一条边使这个回路删一条边后的权值最小,则应该删去最小生成树中i到j路径上权值最大的边。(要生成一个新的树当然不是删这条新加的边)

要想使替换一条边后的树的权值尽可能小,就在所有没有在树中的边与树中权值最大的边比较,如果树外的边可以替换树中的边并且这两条边的权值一样,则说明我们替换出来的这个次小生成树和最小生成树的权值相等,也就是最小生成树不唯一。

在prim的同时用maxx数组保存任意两点之间(任意两点可能不是直接连通)路径上的最大权值的边。
在建树完成后,拿树外的每一条边尝试是否可以替换树中的边,如果能够替换,并且这两条边(替换的和被替换的)的权值相等则说明最小生成树不唯一。

具体步骤见代码注解:

AC代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;

int mp[110][110],dis[110],book[110],maxx[110][110],n;
int pre[110];
int used[110][110];

int prim()
{
    int sum=0;
    for(int i=1;i<=n;i++)
    {
        dis[i]=mp[1][i];
        pre[i]=1;//我们先设树中只有1这个顶点,所以每个顶点的前驱都先设为顶点1 
    }
    dis[1]=0;
    book[1]=1;

    int minn,u;
    for(int i=1;i<=n-1;i++)
    {
        u=-1,minn=INF;
        for(int j=1;j<=n;j++)
        {
            if(dis[j]<minn&&book[j]==0)
            {
                minn=dis[j];
                u=j;
            }
        }

        if(u==-1) return sum;//加上这句话可以不用把所有的点都遍历完 
        book[u]=1;
        sum+=minn;//记录此时树中的权值的和
        used[pre[u]][u]=used[u][pre[u]]=1;//used表示这条边加入了树中 
        //在加入新的点后重新更新每个点到u路径上的最大的权值 
        for(int j=1;j<=n;j++)
            maxx[j][u]=max(maxx[j][pre[u]],minn);//j到pre[u]与pre[u]到u比较,也就是新加入的边和这条路径中上次找到的最大边比较
        for(int k=1;k<=n;k++)
        {
            if(mp[u][k]<dis[k]&&book[k]==0)
            {
                dis[k]=mp[u][k];//把u加入树后更新此时每个非树顶点到树的最短距离 
                pre[k]=u;//在这里把树中每个顶点的前驱都顺便记录下来 
            }
        }
    }
    return sum;
}

int main()
{
    int t,m;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d",&n,&m);
        memset(book,0,sizeof(book));
        memset(mp,INF,sizeof(mp));
        memset(used,0,sizeof(used));
        memset(maxx,0,sizeof(maxx));

        int u,v,w;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            if(w<mp[u][v]) mp[u][v]=mp[v][u]=w;
        }

        int sum=prim();
        int flag=0;

        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(used[i][j]==0&&mp[i][j]<INF)//如果连接ij的这条边没有在树中并且ij是连通的 
                {
                    if(mp[i][j]==maxx[i][j])//如果这条要加入的边能够替换最小生成树中的一条边(权值不变且仍是最小生成树) 
                    {
                        flag=1;
                        break;
                    }
                }
            }
            if(flag) break;
        }
        if(flag) printf("Not Unique!\n");
        else printf("%d\n",sum);
    }
    return 0;
}

17.12.19更:
要在更新maxx数组之前加一句判断语句,保证u到u的距离为0而不是更新为边minn的值
至于不加为什么过我也不知道。。。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#define INF 0x3f3f3f3f
using namespace std;

int mp[110][110],book[110],dis[110],n;
int pre[110],used[110][110],maxx[110][110];

int prim()
{
    int sum=0;
    for(int i=1;i<=n;i++)
    {
        dis[i]=mp[1][i];
        pre[i]=1;
    }
    dis[1]=0;
    book[1]=1;

    int u,minn;
    for(int i=1;i<=n-1;i++)
    {
        u=-1,minn=INF;
        for(int j=1;j<=n;j++)
        {
            if(dis[j]<minn&&book[j]==0)
            {
                minn=dis[j];
                u=j;
            }
        }

        if(u==-1) return sum;
        book[u]=1;
        sum+=minn;
        used[pre[u]][u]=used[u][pre[u]]=1;

        for(int j=1;j<=n;j++)
        {
            if(book[j]&&j!=u)//要加上j!=u,虽然数据水AC了..如果不加则当u==u时距离应该为0却更新为了minn 
                maxx[j][u]=maxx[u][j]=max(maxx[j][pre[u]],minn);
        }

        for(int k=1;k<=n;k++)
        {
            if(mp[u][k]<dis[k]&&book[k]==0)
            {
                dis[k]=mp[u][k];
                pre[k]=u;
            }
        }
    }
    return sum;
}

int main()
{
    int tt,m;
    scanf("%d",&tt);
    while(tt--)
    {
        scanf("%d%d",&n,&m);

        memset(book,0,sizeof(book));
        memset(mp,INF,sizeof(mp));
        memset(used,0,sizeof(used));
        memset(maxx,0,sizeof(maxx));

        int u,v,w;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&u,&v,&w);
            if(w<mp[u][v]) mp[u][v]=mp[v][u]=w;
        }

        int sum=prim();

        int flag=0;
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                if(used[i][j]==0&&mp[i][j]<INF)
                {
                    if(mp[i][j]==maxx[i][j])
                    {
                        flag=1;
                        break;
                    }
                }
            }
            if(flag) break;
        }
        if(flag) printf("Not Unique!\n");
        else printf("%d\n",sum);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值