POj 3164 Command Network最小树形图 模板题 朱刘算法

Command Network

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 对相连的点(给出 m 对下标,可认为是点的编号,点之间的距离就是边权),求最小树形图
模板题
从这里 https://www.cnblogs.com/hdu-zsk/p/8167687.html 学的,图解很详细
最小树形图(Arborescence,亦称Directed Rooted Tree)是有向图的最小生成树(Minimum Spanning Tree)
常用的有朱刘算法和 tarjan
朱刘算法分为四个过程:
1)求最短弧集合 E
2)判断集合 E 中有没有有向环,如果有转步骤 3,否则转 4
3)收缩点,把有向环收缩成一个点,并且对图重新构建,包括边权值的改变和点的处理,之后再转步骤 1
4)展开收缩点,求得最小树形图

收缩点时,为什么出边的权不变,入边的权要减去in [u]: 对于新图中的最小树形图T,设指向人工节点的边为e。将人工节点展开以后,e指向了一个环。假设原先e是指向u的,这个时候我们将环上指向u的边 in[u]删除,这样就得到了原图中的一个树形图。我们会发现,如果新图中e的权w’(e)是原图中e的权w(e)减去in[u]权的话,那么在我们删除 掉in[u],并且将e恢复为原图状态的时候,这个树形图的权仍然是新图树形图的权加环的权,而这个权值正是最小树形图的权值。所以在展开节点之后,我们 得到的仍然是最小树形图。逐步展开所有的人工节点,就会得到初始图的最小树形图了
这是个没看懂的解释,看了代码之后发现,有一段是这样的:

for(i = 1; i <= n; ++i){
            res += in[i];

假设不恢复原图,结果里加上了这些 in[v], 所以在新图里,到这一块的每个入边权要减掉这一块

#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#include <cmath>
#include <set>
#include <algorithm>
#include <map>
#include <vector>
#define mod 1000000007
using namespace std;
typedef long long ll;

const int N = 106;   //点的数量
const double inf = 0x3f3f3f3f; 
const double eps = 1e-6;

int pre[N], vis[N], id[N], x[N], y[N];  pre[v] = u, // 记录最小入边起点,id 给环编号,x, y 坐标
int cnt, n, m;
double in[N];  //记录每个点的最小入边边权,本题是 double 类型

struct node
{
    int u, v;
    double w;
}e[N*N];

void add(int u, int v, double w)
{
    e[cnt].u = u;
    e[cnt].v = v;
    e[cnt].w = w;
    ++cnt;
}

double dis(int x1, int y1, int x2, int y2)  //计算距离
{
    return sqrt((double)(x1-x2)*(x1-x2)+(double)(y1-y2)*(y1-y2));
}

double zhuliu(int rt, int n, int m)
{
    int i, j, u, v, ct = 0;
    double w, res = 0;   //res 总权值
    while(1){
        for(i = 1; i <= n; ++i)
            in[i] = inf;  初始化
        for(i = 0; i < m; ++i){   //遍历所有边,找每个点的最小入边
            u = e[i].u;
            v = e[i].v;
            if(e[i].w < in[v] && u != v)
                in[v] = e[i].w, pre[v] = u;
        }
        for(i = 1; i <= n; ++i)   //遍历所有点,如果除根点外有孤立点,最小树形图不存在
            if(i != rt && fabs(in[i]-inf) < eps)
                return -1;
        for(i = 1; i <= n; ++i)  //初始化
            vis[i] = id[i] = 0;
        vis[rt] = 1;
        in[rt] = ct = 0;
        for(i = 1; i <= n; ++i){  //遍历所有点,寻找所有环
            res += in[i];  //先加上最小边权
            v = i;
            while(vis[v] != i && id[i] == 0 && v != rt)
                vis[v] = i, v = pre[v];
            if(id[v] == 0 && v != rt){  //说明上面循环是因为vis[i] == i, 出现已经标记的点,存在环
                id[v] = ++ct; 
                for(u = pre[v]; u != v; u = pre[u])  //给这个环里的点标上同一个编号
                    id[u] = ct;
            }
        }
        if(ct == 0) //无环
            break;
        for(i = 1; i <= n; ++i) //遍历所有点,给独立点也更新编号
            if(!id[i])
                id[i] = ++ct;
        for(i = 0; i < m; ++i){  //建立新图,缩点标记,更新最小边权
            u = e[i].u;
            v = e[i].v;
            e[i].u = id[u];
            e[i].v = id[v];
            if(id[u] != id[v])
                e[i].w -= in[v];
        }
        n = ct;
        rt = id[rt]; // 循环,看新图是否有环
    }
    return res;
}


int main()
{
    int n, m, i, a, b;
    while(~scanf("%d%d", &n, &m)){
        cnt = 0;
        memset(e, 0, sizeof e);
        for(i = 0; i <= n; ++i)
            pre[i] = i;
        for(i = 1; i <= n; ++i)
            scanf("%d%d", &x[i], &y[i]);
        for(i = 0; i < m; ++i){
            scanf("%d%d", &a, &b);
            add(a, b, dis(x[a], y[a], x[b], y[b]));
        }
        double res = zhuliu(1, n, m);
        if(res == -1)
            printf("poor snoopy\n");
        else
            printf("%.2lf\n", res);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值