POJ 3164 Command Network 最小树形图 (朱刘算法详解及模板)

Command Network

Time Limit: 1000MS Memory Limit: 131072K
Description

After a long lasting war on words, a war on arms finally breaks out between littleken’s and KnuthOcean’s kingdoms. A sudden and violent assault by KnuthOcean’s force has rendered a total failure of littleken’s command network. A provisional network must be built immediately. littleken orders snoopy to take charge of the project.

With the situation studied to every detail, snoopy believes that the most urgent point is to enable littenken’s commands to reach every disconnected node in the destroyed network and decides on a plan to build a unidirectional communication network. The nodes are distributed on a plane. If littleken’s commands are to be able to be delivered directly from a node A to another node B, a wire will have to be built along the straight line segment connecting the two nodes. Since it’s in wartime, not between all pairs of nodes can wires be built. snoopy wants the plan to require the shortest total length of wires so that the construction can be done very soon.

Input

The input contains several test cases. Each test case starts with a line containing two integer N (N ≤ 100), the number of nodes in the destroyed network, and M (M ≤ 104), the number of pairs of nodes between which a wire can be built. The next N lines each contain an ordered pair xi and yi, giving the Cartesian coordinates of the nodes. Then follow M lines each containing two integers i and j between 1 and N (inclusive) meaning a wire can be built between node i and node j for unidirectional command delivery from the former to the latter. littleken’s headquarter is always located at node 1. Process to end of file.

Output

For each test case, output exactly one line containing the shortest total length of wires to two digits past the decimal point. In the cases that such a network does not exist, just output ‘poor snoopy’.

Sample Input

4 6
0 6
4 6
0 0
7 20
1 2
1 3
2 3
3 4
3 1
3 2
4 3
0 0
1 0
0 1
1 2
1 3
4 1
2 3
Sample Output

31.19
poor snoopy

题意:给你N个点的坐标和M条有向边,问你以点1为根的最小树形图的边权之和。

思路:
最小树形图 朱刘算法
(引用一下)阐述下算法的流程:算法一开始先判断从固定根开始是否可达所有原图中的点,若不可,则一定不存在最小树形图。
第二步,遍历所有的边,从中找出除根结点外各点的最小入边,累加权值,构成新图。
接着判断该图是否存在环。若不存在,则该图便是所求最小树型图,当前权为最小权。
否则对环缩点,然后回到第二步继续判断。
再来谈谈细节,首先是查环,对于新图来说只有n-1条入边,对于各条入边,其指向的顶点是唯一的,于是我们可以在边表中添加from/pre,表示该边的出发点,并考虑到如果存在环,则对环上所有边反向,环是显然同构的,于是最多作V次dfs就能在新图中找到所有的环,并能在递归返回时对顶点重标号进行缩点。
然后就是重要的改边法,对于所有不在环上的边,修改其权为w-min(v),w为当前边权,min(v)为当前连接v点的最小边权。其意义在于选择当前边的同时便放弃了原来的最小入边。(每个点只有一条入边是最优的)
我们可以知道,每次迭代选边操作O(E),缩点操作O(V),更新权操作O(E),至少使一个顶点归入生成树,于是能在V-1步内完成计算,其复杂度为O(VE)。

针对这道题:
最开始的图,把所有的最小入边都累加到ret里。至于为什么,因为这样才能保证所得的ret有可能是最小树形图的解,当然,是在这些最小入边集合不行成环得情况下。
如果有了环,ret肯定不是最终答案,因为环中间有的边需要删掉,而且环之间也要连接起来。现在我们无法得知删除环中的哪些边才行。这就需要建立新图了。
eg:某个图的部分图中, 1->2权值为3, 2->1权值为4, 3->1权值为9, 4->2权值为7。 那么可以看到,结点1和结点2是形成了一个环的。我们仅从其大小不知道删除哪条边比较好,这时看到3->1权值为9, 如果走这条边,那么接下来只能删除掉2->1这条边,同理走4->2的话就要删除掉1->2这条边。 那么就不妨建立新图, 将1和2缩成一点,3->1的权值就变成了9-4=5, 4->2的权值变成了7-3=4。
这样的话,就相当于删除了不需要走的边了。
形成新图后,又变成了最小树形图的求解,就这样循环下去,直到图中的最小边集没有环为止。

以上为定根最小树型图,对于无固定根最小树型图,只要虚拟一个根连所有的点的权为边权总和+1,最后的结果减去(边权+1)即可。另外对于求所定的根标号,只要搜索被选中的虚边就可以判断了。

#include <iostream>  
#include <algorithm>  
#include <cstdio>
#include <cmath>
#include <cstring>
#define INF 2000000000  
#define MAXN 105  
#define MAXM 1005
using namespace std;

struct Point{  
    double x, y;  
}pit[MAXN];

struct node{  
    int u, v;  
    double w;  
}ed[MAXN * MAXN];

int pre[MAXN], scc[MAXN], vis[MAXN], n, m;
double in[MAXN];

double dis(Point a, Point b){
    return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));  
}

double solve(int root, int n, int m){  
    double ret = 0;
    while(true){
        for(int i=1; i<=n; i++) in[i] = INF; //维护最小入边 
        in[root] = 0;
        for(int i=1; i<=m; i++){
            int u = ed[i].u, v = ed[i].v;  
            if(ed[i].w < in[v] && u != v/*缩点之后会有u==v的情况*/){
                pre[v] = u;//维护来源,保证了入边只有一条 
                in[v] = ed[i].w;
            }
        }
        for(int i=1; i<=n; i++){
            if(in[i] == INF) return -1;//除了以外有点没有入边,则根从无法到达? 
        }
        int cnt = 1;//统计环的个数 
        memset(scc, -1, sizeof(scc));
        memset(vis, -1, sizeof(vis));
        for(int i=1; i<=n; i++){//标记每个环
            ret += in[i];
            int v = i;
            while(vis[v] != i && scc[v] == -1 && v != root){
            //每个点寻找其前序点,要么最终寻找至根部,要么找到一个环
                vis[v] = i;
                v = pre[v];
            }
            if(v != root && scc[v] == -1){//找到环之后缩点
                for(int u=pre[v]; u!=v; u=pre[u])
                    scc[u] = cnt;
                scc[v] = cnt++;
            }
        }
        if(cnt == 1) break; //无环
        for(int i=1; i<=n; i++)  
            if(scc[i] == -1) scc[i] = cnt++;
        for(int i=1; i<=m; i++){//重新建图  
            int u = ed[i].u, v = ed[i].v;  
            ed[i].u = scc[u];  
            ed[i].v = scc[v];  
            if(scc[u] != scc[v])//如果是在一个环里面的,我们已经计算过了,不再考虑 
                ed[i].w -= in[v];//如果不在一个环里面 ,那么我们选这条边时,就可以删掉之前连向它的边
        }
        n = cnt - 1;  
        root = scc[root];  
    }  
    return ret;  
}

int main(){  
    while(scanf("%d%d", &n, &m) != EOF){
        for(int i=1; i<=n; i++) 
            scanf("%lf%lf", &pit[i].x, &pit[i].y);  
        for(int i=1; i<=m; i++){
            scanf("%d%d", &ed[i].u, &ed[i].v);  
            if(ed[i].u != ed[i].v) 
                ed[i].w = dis(pit[ed[i].u], pit[ed[i].v]);  
            else ed[i].w = INF;//去除自环  
        }
        double ans = solve(1, n, m);  
        if(ans == -1) printf("poor snoopy\n");  
        else printf("%.2f\n", ans);  
    }  
    return 0;  
}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值