• poj 1679 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算法的基础上做一些变化。跑完后额外存储最小生成树的n-1条边used[i][j],每两个点间最长的边mdis[i][j]。具体见代码。

    然后遍历所有不在最小生成树中的边。将这种边加进最小生成树中肯定会形成一个环。若该环中还有一条边与该边一样大。则最小生成树不唯一。

    #include<cstdio>
    #include<algorithm>
    #include<cstring>
    
    using namespace std;
    
    const int maxn=100;
    const int maxm=10000;
    const int inf=1000000000;
    
    int to[maxm+10];
    int w[maxm+10];
    int nex[maxm+10];
    int head[maxn+10];
    
    int lowc[maxn+10];
    int from[maxn+10];
    int mdis[maxn+10][maxn+10];
    int vis[maxn+10];
    int used[maxn+10][maxn+10];
    
    int main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            int n,m;
            scanf("%d%d",&n,&m);
            memset(head,-1,sizeof(head));
            for(int i=0,cnt=0,x,y,z;i<m;i++)
            {
                scanf("%d%d%d",&x,&y,&z);
                to[cnt]=y;w[cnt]=z;nex[cnt]=head[x];head[x]=cnt++;
                to[cnt]=x;w[cnt]=z;nex[cnt]=head[y];head[y]=cnt++;
            }
    
            int ans=0;
            for(int i=1;i<=n;i++)
                lowc[i]=inf;
            memset(vis,0,sizeof(vis));
            memset(used,0,sizeof(used));
            for(int i=head[1],x,y,z;i!=-1;i=nex[i])
            {
                x=1;y=to[i];z=w[i];
                lowc[y]=z;
                from[y]=x;
            }
            vis[1]=1;
            for(int i=2;i<=n;i++)
            {
                //更新思路:mdis->vis,used->lowc,from
                int x,y=0;
                for(int i=1;i<=n;i++)
                    if(!vis[i])
                    {
                        if(y==0||lowc[i]<lowc[y])
                            y=i;
                    }
                x=from[y];
                ans+=lowc[y];
                mdis[x][y]=mdis[y][x]=lowc[y];
                for(int i=1;i<=n;i++)
                {
                    if(vis[i]&&i!=x)
                        mdis[i][y]=mdis[y][i]=max(mdis[i][x],lowc[y]);
                }
                vis[y]=1;used[x][y]=used[y][x]=1;
                x=y;
                for(int i=head[x],z;i!=-1;i=nex[i])
                {
                    y=to[i];z=w[i];
                    if(lowc[y]>z)
                    {
                        lowc[y]=z;
                        from[y]=x;
                    }
                }
            }
    
            bool flag=true;
            for(int i=1;i<=n;i++)
            {
                for(int j=head[i];j!=-1;j=nex[j])
                {
                    int l=to[j];
                    if(!used[i][l]&&w[j]==mdis[i][l])
                    {
                        flag=false;
                        break;
                    }
                }
                if(!flag)
                    break;
            }
    
            if(flag)
            {
                printf("%d
    ",ans);
            }
            else
            {
                printf("Not Unique!
    ");
            }
        }
        return 0;
    }
    View Code
  • 相关阅读:
    scrapy Request方法
    from lxml import etree报错
    python文件管道 下载图集
    scrapy基本爬虫,采集多页
    python操作excel xlwt (转)
    matplotlib 设置标题 xy标题等
    matplotlib 饼状图
    acwing 600. 仰视奶牛
    LeetCode 684. 冗余连接
    LeetCode 200. 岛屿数量
  • 原文地址:https://www.cnblogs.com/acboyty/p/9745846.html
Copyright © 2020-2023  润新知