poj 3164 Command Network(最小树形图—朱刘算法模版)

17 篇文章 0 订阅
Command Network
Time Limit: 1000MS Memory Limit: 131072K
Total Submissions: 10259 Accepted: 2977

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
 
题意:求一个有固定根的有向图的最小生成树,即求有固定根的最小树形图:以根为起点,沿给定有向边,可以访问到所有的点,并使所构成的边权值之和最小。
思路:用朱刘算法,详细讲解最小树形图——朱刘算法

简单来说,分三步,如下:

  1. 每个点找其最小的入边In[v] ? 如果有除跟节点以外的点找不到入边,则无解 : 否则答案累加In[v]
  2. 看看有没有环 ? 无环则已经找到解,返回答案 : 将环缩点
  3. 重新构图,每条边[u->v]的权值减去In[v],然后重复第一步 
     
AC代码:
#include <cstring>
#include <string>
#include <cstdio>
#include <algorithm>
#include <queue>
#include <cmath>
#include <vector>
#include <cstdlib>
#include <iostream>
#define max2(a,b) ((a) > (b) ? (a) : (b))
#define min2(a,b) ((a) < (b) ? (a) : (b))

using namespace std;
const double INF=10000000;
const int N=105;
struct point
{
    double x,y;
} p[N];
struct node
{
    int u,v;
    double w;
} edge[10005];
double in[N];
int pre[N],hash[N],vis[N];
int n;
double get_dis(point a,point b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
void add(int u,int v,int i)
{
    edge[i].u=u;
    edge[i].v=v;
    edge[i].w=get_dis(p[u],p[v]);
}
double Directed_MST(int root,int m)
{
    double sum=0;
    while(1)
    {
        for(int i=1; i<=n; i++)
            in[i]=INF;
        for(int i=0; i<m; i++) //找最小入边
        {
            int u=edge[i].u;
            int v=edge[i].v;
            if(edge[i].w<in[v]&&u!=v)  //重构的新图可能存在自环
            {
                pre[v]=u;
                in[v]=edge[i].w;
            }
        }
        for(int i=1; i<=n; i++) //判断是否存在最小树形图
        {
            if(i==root) continue;
            if(in[i]==INF) return -1;
        }
        int cntnode=1;
        memset(hash,-1,sizeof(hash));
        memset(vis,-1,sizeof(vis));
        in[root]=0;
        for(int i=1; i<=n; i++)  //检查是否存在环
        {
            sum+=in[i];
            int v=i;
            while(vis[v]!=i&&hash[v]==-1&&v!=root)
            {
                vis[v]=i;
                v=pre[v];
            }
            if(v!=root&&hash[v]==-1)      //若存在环,将环缩点
            {
                for(int u=pre[v]; u!=v; u=pre[u])
                    hash[u]=cntnode;
                hash[v]=cntnode++;
            }
        }
        if(cntnode==1) break;   //若不存在环,算法终止
        for(int i=1; i<=n; i++) //构新图
            if(hash[i]==-1)
                hash[i]=cntnode++;
        for(int i=0; i<m; i++)
        {
            int v=edge[i].v;
            edge[i].u=hash[edge[i].u];
            edge[i].v=hash[edge[i].v];
            if(edge[i].u!=edge[i].v)
                edge[i].w-=in[v];
        }
        n=cntnode-1;
        root=hash[root];
    }
    return sum;
}
int main()
{
    int a,b,cnt_edge,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        cnt_edge=0;
        for(int i=1; i<=n; i++)
            scanf("%lf%lf",&p[i].x,&p[i].y);
        for(int i=0; i<m; i++)
        {
            scanf("%d%d",&a,&b);
            if(a==b) continue;
            add(a,b,cnt_edge);
            cnt_edge++;
        }
        double ans=Directed_MST(1,cnt_edge);
        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、付费专栏及课程。

余额充值