zoukankan      html  css  js  c++  java
  • URAL 1416 Confidential (最小生成树+次小生成树)

    Description
    Zaphod Beeblebrox — President of the Imperial Galactic Government. And by chance he is an owner of enterprises that trade in secondhand pens. This is a complicated highly protable and highly competitive business. If you want to stay a leader you are to minimize your expenses all the time. And the presedent's high post helps in those aairs. But he is to keep this business in secret. As a president Zaphod has access to the top secret and important information an exact value of power loss in the hyperspace transition between the planets. Of course, this information is very useful to his company. Zaphod is to choose the minimal possible set of trans-planet passages so that he could pass from any planet to any other one via those passages and their total cost would be minimal. The task won't be complicated if Zaphod was not to keep in secret that he helps his company with the secret information. Thus, Zaphod decided to find not the cheapest passages set but the next one. As a real businessman he wants to estimate the value of his conspiracy expenses.

    Input
    The first input line contains two integers: N (2 ≤ N ≤ 500) is a number of planets in the Galaxy and M is an amount of possible transitions. The next M lines contain three integers ai, bi the numbers of the planets that are connected with some passage (1 ≤ ai, bi ≤ N), and wi (0 ≤ wi ≤ 1000) is the transition cost. If an A to B transition is possible then a B to A transition is possible, too. The cost of those transitions are equal. There is not more than one passage between any two planets. One can reach any planet from any other planet via some chain of these passages.

    Output
    You should find two different sets of transitions with the minimal possible cost and output theirs costs. Print the minimal possible cost first. If any of those sets of transitions does not exist denote it's cost by ?1.

    Sample Input
    4 6
    1 2 2
    2 3 2
    3 4 2
    4 1 2
    1 3 1
    2 4 1
    3 2
    1 2 2
    2 3 2

    input output

    Cost: 4
    Cost: 4
    Cost: 4
    Cost: -1

    分析:
    首先了解一下关于次小生成树的解法:

    给出一个带边权的无向图G,设其最小生成树为T,求出图G的与T不完全相同的边权和最小的生成树(即G的次小生成树)。一个无向图的两棵生成树不完全相同,当且仅当这两棵树中至少有一条边不同。注意,图G可能不连通,可能有平行边,但一定没有自环(其实对于自环也很好处理:直接舍弃。因为生成树中不可能出现自环)。

    关于这道题可以这样来分析:

    定义生成树T的一个可行变换(-E1, +E2):将T中的边E1删除后,再加入边E2(满足边E2原来不在T中但在G中),若得到的仍然是图G的一棵生成树,则该变换为可行变换,该可行变换的代价为(E2权值 - E1权值)。这样,很容易证明,G的次小生成树就是由G的最小生成树经过一个代价最小的可行变换得到。进一步可以发现,这个代价最小的可行变换中加入的边E2的两端点如果为V1和V2,那么E1一定是原来最小生成树中从V1到V2的路径上的权值最大的边。

    这样,对于本题就有两种算法了:(以下的T全部指G的最小生成树)
    (1)Prim:
    设立数组len,len[x][y]表示T中从x到y路径上的最大边的权值。

    len数组可以在用prim算法求最小生成树的过程中得出。每次将边(i, j)加入后(j是新加入树的边,i=pre[j]),枚举树中原有的每个点k(包括i,但不包括j),则len[k][j]=max{len[i][j], (i, j)边权值},又由于len数组是对称的,可以得到len[j][k]=len[k][j]。
    然后千万记住将图G中的边(i, j)删除(就是将邻接矩阵中(i, j)边权值改为∞)

    因为T中的边是不能被加入的。等T被求出后,所有的len值也求出了,然后,枚举点i、j,若邻接矩阵中边(i, j)权值不是无穷大(这说明i、j间存在不在T中的边),则求出{(i, j)边权值 -lenF[i][j]}的值,即为加入边(i, j)的代价,求最小的总代价即可。

    另外注意三种特殊情况:
    【1】图G不连通,此时最小生成树和次小生成树均不存在。判定方法:在扩展T的过程中找不到新的可以加入的边;
    【2】图G本身就是一棵树,此时最小生成树存在(就是G本身)但次小生成树不存在。判定方法:在成功求出T后,发现邻接矩阵中的值全部是无穷大;
    【3】图G存在平行边。这种情况最麻烦,因为这时代价最小的可行变换(-E1, +E2)中,E1和E2可能是平行边
    因此,只有建立两个邻接矩阵,分别存储每两点间权值最小的边和权值次小的边的权值,然后,每当一条新边(i, j)加入时,不是将邻接矩阵中边(i, j)权值改为无穷大,而是改为连接点i、j的权值次小的边的权值。

    #include<stdio.h>
    #include<iostream>
    #include<string.h>
    const int INF=0x3f3f3f3f;
    using namespace std;
    int n,m;
    int tu[505][505];//保存在原始的图信息
    int len[505][505];//保存每两点咋最小生成树上路径中最长边长
    int dis[505];//距离
    int pre[505];//最小生成树里面这个点的前驱节点
    void init()//数据初始化
    {
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                tu[i][j]=INF;
                len[i][j]=0;
            }
        }
    }
    
    void prim()
    {
        dis[1]=-1;
        for(int i=2; i<=n; i++) //初始化最小生成树里的路径长度和前驱节点
        {
            dis[i]=tu[1][i];
            pre[i]=1;
        }
        int sum=0,Min,k;
        for(int i=2; i<=n; i++)//进行n-1次循环
        {
            Min=INF;
            k=0;
            for(int j=1; j<=n; j++)//找出当前的最小边
            {
                if(dis[j]!=-1&&Min>dis[j])
                {
                    Min=dis[j];
                    k=j;
                }
            }
            if(k==0)  break;
            tu[k][pre[k]]=tu[pre[k]][k]=INF;//把最小生成树上的这条边删去
            for(int j=1; j<=n; j++)
            {
                if(dis[j]==-1)//这个点已经加入到最小生成树里面
                {
                    //如果此时多加一条k到j的边,这样的话肯定就形成了一个环,我们要找出这个环里面的最大值
                    len[k][j]=len[j][k]=max(Min,len[pre[k]][j]);
                }
            }
    
            dis[k]=-1;
            sum+=Min;
            for(int j=1; j<=n; j++)
            {
                if(dis[j]!=-1&&dis[j]>tu[k][j])
                {
                    dis[j]=tu[k][j];
                    pre[j]=k;
                }
            }
        }
        printf("Cost: %d
    ",sum);
        Min=INF;
        bool flag=0;
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                if(tu[i][j]!=INF&&Min>tu[i][j]-len[i][j])//这条边被其他的边替代过后的距离差
                {
                    Min=tu[i][j]-len[i][j];
                    flag=1;
                }
        if(!flag) printf("Cost: -1
    ");
        else printf("Cost: %d
    ",sum+Min);
    }
    int main()
    {
        while(~scanf("%d%d",&n,&m))
        {
            init();
            int a,b,w;
            for(int i=0; i<m; i++)
            {
                scanf("%d%d%d",&a,&b,&w);
                tu[a][b]=tu[b][a]=w;
            }
            prim();
        }
        return 0;
    }
    

    2)Kruskal:
    Kruskal算法也可以用来求次小生成树。在准备加入一条新边(a, b)(该边加入后不会出现环)时,选择原来a所在连通块(设为S1)与b所在连通块(设为S2)中,点的个数少的那个(如果随便选一个,最坏情况下可能每次都碰到点数多的那个,时间复杂度可能增至O(NM)),找到该连通块中的每个点i,并遍历所有与i相关联的边,若发现某条边的另一端点j在未选择的那个连通块中(也就是该边(i, j)跨越了S1和S2)时,就说明最终在T中"删除边(a, b)并加入该边"一定是一个可行变换,且由于加边是按照权值递增顺序的,(a, b)也一定是T中从i到j路径上权值最大的边,故这个可行变换可能成为代价最小的可行变换,计算其代价为[(i, j)边权值 - (a, b)边权值],取最小代价即可。

    注意,在遍历时需要排除一条边,就是(a, b)本身(具体实现时由于用DL边表,可以将边(a, b)的编号代入)。另外还有一个难搞的地方:如何快速找出某连通块内的所有点?方法:由于使用并查集,连通块是用树的方式存储的,可以直接建一棵树(准确来说是一个森林),用“最左子结点+相邻结点”表示,则找出树根后遍历这棵树就行了,另外注意在合并连通块时也要同时合并树。

    对于三种特殊情况:
    【1】图G不连通。判定方法:遍历完所有的边后,实际加入T的边数小于(N-1);
    【2】图G本身就是一棵树。判定方法:找不到这样的边(i, j);
    【3】图G存在平行边。这个对于Kruskal来说完全可以无视,因为Kruskal中两条边只要编号不同就视为不同的边。

    其实Kruskal算法求次小生成树还有一个优化:每次找到边(i, j)后,一处理完这条边就把它从图中删掉,因为当S1和S2合并后,(i, j)就永远不可能再是可行变换中的E2了。

    #include<stdio.h>
    #include<iostream>
    #include<string.h>
    #include<algorithm>
    const int INF=0x3f3f3f3f;
    using namespace std;
    int n,m;
    int fa[505];//并查集数组
    int findRoot(int x)           //递归查找顶点x所在树的根
    {
        if (fa[x] == x) return x;//若为x,则x的根就是自身
        else                    //否则
        {
            int tmp =findRoot(fa[x]);//递归查找x的父亲Tree[x]的根,tmp为最终的树根
            fa[x]= tmp;           //查找过程中进行路径压缩:把x到根之间遇到的所有顶点的父亲设为tmp
            return tmp;             //返回树根
        }
    }
    struct Node//存储边信息
    {
        int a,b,w;
        bool select;//标记是否在最小生成树中
    } edge[250009];
    
    bool cmp(Node A,Node B)
    {
        if(A.w!=B.w)
            return A.w<B.w;
        if(A.a!=B.a)
            return A.a<B.a;
        return A.b<B.b;
    }
    //链式前向星的数据结构
    struct Node1
    {
        int to;
        int next;
    };
    
    Node1 link[505];//边数组
    int Count;//边数组中数据的个数
    int head[505];//邻接表的头结点位置
    int End[505];//邻接表的伪结点位置
    int len[505][505];//每两点在最小生成树上路径中最长边长
    void init()//初始化
    {
        for(int i=1; i<=n; i++)
        {
            head[i]=-1;
            fa[i]=i;
        }
    }
    void kruskal()
    {
        int k=0;//加入到最小生成树里的边的数目
        //初始化邻接表,对于每个节点添加一条指向其自身的边,表示以i为代表元的集合只有点i
        for(Count=1; Count<=n; Count++)
        {
            link[Count].to=Count;
            link[Count].next=head[Count];
            End[Count]=Count;
            head[Count]=Count;
        }
        for(int i=1; i<=m; i++)
        {
            if(k==n-1) break;
            int x=findRoot(edge[i].a);
            int y=findRoot(edge[i].b);
    
            if(x!=y)//这条边没有加入到生成树里面
            {
                //修改部分,遍历两个节点所在的集合
                for(int w=head[x]; w!=-1; w=link[w].next)
                {
                    printf("w===%d
    ",w);
                    for(int v=head[y]; v!=-1; v=link[v].next)
                    {
                        //每次合并两个等价类的时候,分别属于两个等价类的两个点间的最长边一定是当前加入的边
                        len[link[w].to][link[v].to]=len[link[v].to][link[w].to]=edge[i].w;
                    }
                }
                //合并两个邻接表
                link[End[y]].next=head[x];
                End[y]=End[x];
                fa[x]=y;
                k++;
                edge[i].select=true;
            }
        }
    }
    int main()
    {
        while(~scanf("%d%d",&n,&m))
        {
            init();
            for(int i=1; i<=m; i++)
            {
                scanf("%d%d%d",&edge[i].a,&edge[i].b,&edge[i].w);
                edge[i].select=false;
            }
            sort(edge+1,edge+1+m,cmp);
            kruskal();
            int mst=0;
            for(int i=1; i<=m; i++)
            {
                if(edge[i].select)
                {
                    mst+=edge[i].w;
                }
            }
            printf("Cost: %d
    ",mst);
            int ans=INF;
            for(int i=1; i<=m; i++)
            {
                if(!edge[i].select)
                    ans=min(ans,mst+edge[i].w-len[edge[i].a][edge[i].b]);
            }
            if(ans==INF)
                ans=-1;
            printf("Cost: %d
    ",ans);
        }
        return 0;
    }
    
  • 相关阅读:
    Python的异常处理
    flex stage.width 与stage.stageWidth的区别
    Flex timer使用 keydown事件注册到stage
    flex 事件注册和鼠标拖动
    window.open a.href打开窗口referer的问题
    java UUID的创建
    flex chrome浏览器调试flex程序
    EL表达式 requestScope initParam用法
    Java8 Lumbda表达式 初步
    jQuery 动画的执行
  • 原文地址:https://www.cnblogs.com/cmmdc/p/7722062.html
Copyright © 2011-2022 走看看