POJ - 3164 - 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
题意
求最小树形图
思路
朱刘算法
代码
#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#include<iostream>
#include<cmath>
#include<stack>
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=1010;
typedef long long ll;
struct Node
{
    int x;
    int y;
}point[maxn];
struct node//边的权和顶点
{
    int u;
    int v;
    double w;
}edge[maxn*maxn];
int pre[maxn],id[maxn],vis[maxn],n,m,pos;
double in[maxn];//存最小入边权,pre[v]为该边的起点
double Directed_MST(int root,int V,int E)//朱刘算法求最小树形图(有向图) 
{
    double res=0;//存最小树形图总权值
    while(1)
    {
        //1.找每个节点的最小入边
        for(int i=0;i<V;i++)
            in[i]=inf;//初始化为无穷大
        for(int i=0;i<E;i++)//遍历每条边
        {
            int u=edge[i].u;
            int v=edge[i].v;
            if(edge[i].w<in[v]&&u!=v)//说明顶点v有条权值较小的入边 ,记录之
            {
                pre[v]=u;//节点u指向v
                in[v]=edge[i].w;//最小入边
                if(u==root)//这个点就是实际的起点
                    pos=i;
            }
        }
        for(int i=0;i<V;i++)//判断是否存在最小树形图
        {
            if(i==root)
                continue;
            if(in[i]==inf)
                return -1;//除了根以外有点没有入边,则根无法到达它  说明它是独立的点 一定不能构成树形图
        }
        //2.找环
        int cnt=0;//记录环数
        memset(id,-1,sizeof(id));
        memset(vis,-1,sizeof(vis));
        in[root]=0;
        for(int i=0;i<V;i++) //标记每个环
        {
            res+=in[i];//记录权值
            int v=i;
            while(vis[v]!=i&&id[v]==-1&&v!=root)
            {
                vis[v]=i;
                v=pre[v];
            }
            if(v!=root&&id[v]==-1)
            {
                for(int u=pre[v];u!=v;u=pre[u])
                    id[u]=cnt;//标记节点u为第几个环
                id[v]=cnt++;
            }
        }
        if(cnt==0)
            break; //无环   则break
        for(int i=0;i<V;i++)
            if(id[i]==-1)
                id[i]=cnt++;
            //3.建立新图   缩点,重新标记
        for(int i=0;i<E;i++)
        {
            int u=edge[i].u;
            int v=edge[i].v;
            edge[i].u=id[u];
            edge[i].v=id[v];
            if(id[u]!=id[v])
                edge[i].w-=in[v];
        }
        V=cnt;
        root=id[root];
    }
    return res;
}
double dis(Node a,Node b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        for(int i=0;i<n;i++)
            scanf("%d%d",&point[i].x,&point[i].y);
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&edge[i].u,&edge[i].v);
            edge[i].u--;
            edge[i].v--;
            if(edge[i].u!=edge[i].v)
                edge[i].w=dis(point[edge[i].u],point[edge[i].v]);
            else
                edge[i].w=inf;
        }
        double ans=Directed_MST(0,n,m);
        if(ans==-1)
            printf("poor snoopy\n");
        else
            printf("%.2f\n",ans);
    }
    return 0;
}

 

 
posted @ 2021-03-05 19:39  我找木鱼  阅读(47)  评论(0)    收藏  举报