zoukankan      html  css  js  c++  java
  • 最小树形图(poj3164)

    Command Network
    Time Limit: 1000MS   Memory Limit: 131072K
    Total Submissions: 12834   Accepted: 3718

    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 jbetween 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,问要是所有的基站都受到信号,最小的花费是多少
    分析:最小树形图
    程序:
    #include"string.h"
    #include"stdio.h"
    #include"math.h"
    #include"queue"
    #define eps 1e-10
    #define M 109
    #define inf 100000000
    using namespace std;
    struct node
    {
        double x,y;
    }p[M];
    struct edge
    {
        int u,v;
        double w;
    }edge[M*M];
    int pre[M],id[M],use[M];
    double in[M];
    double pow(double x)
    {
        return x*x;
    }
    double Len(node a,node b)
    {
        return sqrt(pow(a.x-b.x)+pow(a.y-b.y));
    }
    double mini_tree(int root,int n,int m)
    {
        double ans=0;
        int i,u;
        while(1)
        {
            for(i=1;i<=n;i++)
                in[i]=inf;
            for(i=1;i<=m;i++)
            {
                int u=edge[i].u;
                int v=edge[i].v;
                if(edge[i].w<in[v]&&u!=v)
                {
                    in[v]=edge[i].w;
                    pre[v]=u;
                }
            }
            for(i=1;i<=n;i++)
            {
                if(i==root)continue;
                ans+=in[i];
                if(fabs(in[i]-inf)<eps)
                    return -1;
            }
            memset(id,-1,sizeof(id));
            memset(use,-1,sizeof(use));
            int cnt=0;
            for(i=1;i<=n;i++)
            {
                int v=i;
                while(v!=root&&use[v]!=i&&id[v]==-1)
                {
                    use[v]=i;
                    v=pre[v];
                }
                if(v!=root&&id[v]==-1)
                {
                    ++cnt;
                    id[v]=cnt;
                    for(u=pre[v];u!=v;u=pre[u])
                        id[u]=cnt;
                }
            }
            if(cnt==0)
                break;
            for(i=1;i<=n;i++)
                if(id[i]==-1)
                    id[i]=++cnt;
            for(i=1;i<=m;i++)
            {
                int u=edge[i].u;
                int v=edge[i].v;
                edge[i].u=id[u];
                edge[i].v=id[v];
                if(edge[i].u!=edge[i].v)
                    edge[i].w-=in[v];
            }
            n=cnt;
            root=id[root];
        }
        return ans;
    }
    int main()
    {
        int n,m,i;
        while(scanf("%d%d",&n,&m)!=-1)
        {
            for(i=1;i<=n;i++)
                scanf("%lf%lf",&p[i].x,&p[i].y);
            for(i=1;i<=m;i++)
            {
                int a,b;
                scanf("%d%d",&a,&b);
                double L=Len(p[a],p[b]);
                edge[i].u=a;
                edge[i].v=b;
                edge[i].w=L;
            }
            double ans=mini_tree(1,n,m);
            if(ans<0)
                printf("poor snoopy
    ");
            else
            printf("%.2lf
    ",ans);
        }
        return 0;
    }


  • 相关阅读:
    [读书笔记]SQLSERVER企业级平台管理实践读书笔记--从等待事件判断性能瓶颈
    Docker machine学习
    不同数据库连接字符串的网站
    Windows 可以操纵linux内文件,与本地一致的工具
    OpenSSH 安全漏洞(CVE-2021-28041)修复(升级OpenSSH至最新版本(8.6p1))
    PostgreSQL
    firewalld添加/删除服务service,端口port
    PostgreSQL 序列操作
    PostgreSQL/pgsql 为表添加列/ 判断列存不存在再添加列
    Windows10中Power Shell(x64)出现“无法加载 PSReadline 模块。控制台在未使用 PSReadline 的情况下运行。”的解决办法
  • 原文地址:https://www.cnblogs.com/mypsq/p/4348170.html
Copyright © 2011-2022 走看看