(最小树形图 朱刘算法) poj 3164

Command Network

Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 14340   Accepted: 4118

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
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<limits.h>
using namespace std;
const int MAXN = 110;
const int MAXM = 10010;

struct node
{
    int from;
    int to;
    double w;
};
node e[MAXM];

struct node1
{
    double x;
    double y;
};
node1 Point[MAXN];

double Dist(node1 a, node1 b)
{
    double x=a.x-b.x;
    double y=a.y-b.y;
    return sqrt(x*x+y*y);
}

int pre[MAXN],vis[MAXN],flag[MAXN];
double In[MAXN],sum;

double ZhuLiu(int root,int N,int M)
{
    sum=0;
    while(true)
    {
        for(int i=0;i<N;++i)
            In[i]=INT_MAX;
        for(int i=0;i<M;++i)
        {
            int u=e[i].from;
            int v=e[i].to;
            if(e[i].w<In[v]&&u!= v)
            {
                pre[v]=u;//v为终点,pre[v]存放起点
                In[v]=e[i].w;//权值最小的边
            }
        }

        for(int i=0;i<N;++i)//如果存在除root以外的孤立点,则不存在最小树形图
        {
            if(i==root)
                continue;
            if(In[i]==INT_MAX)
                return -1;
        }
        int CntNode=0;
        memset(flag,-1,sizeof(flag));
        memset(vis,-1,sizeof(vis));
        In[root]=0;
        for(int i=0;i<N;++i)   //找环,标记每个环
        {
            sum+=In[i];
            int v=i;
            while(vis[v]!=i&&flag[v]==-1&&v!=root)//每个点寻找其前序点,要么最终寻找至根部,要么找到一个环
            {
                vis[v]=i;
                v=pre[v];
            }
            if(v!=root&&flag[v]==-1)  //新图重新编号
            {
                for(int u=pre[v];u!=v;u=pre[u])
                    flag[u]=CntNode;
                flag[v]=CntNode++;
            }
        }
        if(CntNode==0)    //无环,跳出
            break;
        for(int i=0;i<N;++i)
        {
            if(flag[i]==-1)
                flag[i]=CntNode++;
        }
        for(int i=0;i<M;++i)  //建立新图,更新其他点到环的距离
        {
            int v=e[i].to;
            e[i].from=flag[e[i].from];
            e[i].to=flag[e[i].to];
            if(e[i].from!=e[i].to)
                e[i].w-=In[v];
        }
        N=CntNode;
        root=flag[root];
    }
    return sum;
}

int main()
{
    int x,y,N,M;
    while(~scanf("%d%d",&N,&M))
    {
        int id=0;
        for(int i=0;i< N;++i)
            scanf("%lf%lf",&Point[i].x,&Point[i].y);
        for(int i=0;i<M;++i)
        {
            scanf("%d%d",&x,&y);
            if(x==y)
                continue;
            x--;
            y--;
            e[id].from=x;
            e[id].to=y;
            e[id++].w=Dist(Point[x],Point[y]);
        }
        double ans=ZhuLiu(0,N,id);
        if(ans==-1)
            printf("poor snoopy\n");
        else
            printf("%.2f\n",ans);
    }
    return 0;
}

  

时间: 2024-10-14 12:29:18

(最小树形图 朱刘算法) poj 3164的相关文章

POJ 3164 Command Network 最小树形图-朱刘算法裸题

题目来源:POJ 3164 Command Network 题意:求以1为根的最小树形图 没有输出字符串 思路:直接高朱刘算法 不懂的可以百度 学会了就是直接套模板的事情 其实就是不断消圈而已 不构成圈就有解 无法从根到达其他点就无解 #include <cstdio> #include <cstring> #include <cmath> const int maxn = 110; const int maxm = 50010; const double INF =

POJ 3164 Command Network (最小树形图-朱刘算法)

题目地址:POJ 3164 最小树形图第一发. 把一个v写成u了.....TLE了一晚上...(虽说今晚出去玩了..) 刚开始看这个算法的时看模板以为又是一个isap....吓得一个哆嗦.但是仔细看了看之后发现还是挺好理解的.写下自己的理解. 朱刘算法其实只有3步,然后不断循环. 1:找到每个点的最小入边.既然是生成树,那么对于每个点来说,只要选一个权值最小的入边就可以了.贪心思想.因为如果不是最小入边,那么它肯定不是最小树形图的一条边,考虑它是没有意义的. 2:找环.找环找的是最小入边构成的新

图论--最小树形图朱刘算法模板

最小树形图就是一个有向图,从根节点可以到达其他所有节点,而除根节点外的每个节点又有且仅有一个父节点,这样一张边权和最小的图就是最小树形图. 最小树形图有它特有的算法,朱刘算法.原理是先对除根节点以外的所有节点先找寻一条最小入边,接着判图中是否有有向环,如果有,将每个环缩成一点,再对这些点重新找最小入边,重复合并操作直至没有环. 我用的主要是从kuangbin巨巨的模板小改得到的昂. 各种注释便于理解: 1 #include<stdio.h> 2 #include<string.h>

&ldquo;Hdu4966&rdquo; GGS-DDU - 最小树形图/朱刘算法

原文引用https://www.dazhuanlan.com/2019/08/26/5d6302f8c23db/ 题目链接:传送门 Description 有一个人,想学习N个科目,每个科目都有相应的层次 有M个课程,M个课程的要求是,你的第c个科目的层次要达到l1,才可以参加,参加完这个课程后,你需要缴费money,但你的第d个科目的层次会达到l2 问如何花最少的钱,使得每个科目的层次都达到最高 Solution 每个科目的每个层次都看成一个点,每个科目的层次0和一个根连边,费用为0:每个科目

POJ - 3164-Command Network 最小树形图——朱刘算法

POJ - 3164 题意: 一个有向图,存在从某个点为根的,可以到达所有点的一个最小生成树,则它就是最小树形图. 题目就是求这个最小的树形图. 参考资料:https://blog.csdn.net/txl199106/article/details/62045479 #include <iostream> #include <cstdio> #include <algorithm> #include <cstring> #include <strin

HDU 4966 GGS-DDU (最小树形图-朱刘算法)

题目地址:HDU 4966 刚开始没看清总级别只有500这一条件,看成了每一个都是500..然后建图思路就想歪了.....后来才发现是总共只有500..那么建图就很简单了..把每个科目的每个等级都设为一个点,把所有的0等级设为同一个树根.然后把所有科目的高等级向低等级连一条权值为0的有向边,第一个作用是保证最后的最小树形图是所有点都可达,第二个作用是保证每节课的的所需等级,只要达到高等级,那么使低等级也符合.然后再对每节课连边即可. 代码如下: #include <iostream> #inc

TJU 2248 Channel Design (最小树形图-朱刘算法)

题目地址:TJU 2248 最小树形图模板题.熟练一下模板. 代码如下: #include <iostream> #include <string.h> #include <math.h> #include <queue> #include <algorithm> #include <stdlib.h> #include <map> #include <set> #include <stdio.h>

定根最小树形图 朱刘算法 luogu P4716

https://www.luogu.org/problem/P4716 #include<bits/stdc++.h> #define ll long long #define rep(ii,a,b) for(int ii=a;ii<=b;++ii) #define per(ii,a,b) for(int ii=b;ii>=a;--ii) #define forn(i,x,g,e) for(int i=g[x];i;i=e[i].next) #define IO ios::sync

【POJ 3164】【朱刘算法模板】Command Network

Command Network Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 13977   Accepted: 4012 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 a