zoukankan      html  css  js  c++  java
  • POJ3164:Command Network(有向图的最小生成树)

    Command Network

    Time Limit: 1000MS   Memory Limit: 131072K
    Total Submissions: 20766   Accepted: 5920

    题目链接:http://poj.org/problem?id=3164

    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:

    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

    题意:

    给出一个有向图,求出这个图的最小生成树。

    题解:

    直接用朱刘算法即可解决这个问题,可以参见:https://www.cnblogs.com/thefirstfeeling/p/4410705.html

    代码如下:

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <iostream>
    #include <queue>
    #include <cmath>
    #define INF 0x3f3f3f3f
    using namespace std;
    typedef long long ll;
    const int N = 105,M = 1e4+5;
    int n,m;
    struct Point{
        int x,y;
    }p[N];
    double dis(int x,int y){
        return sqrt((double)(p[x].x-p[y].x)*(p[x].x-p[y].x)+(double)(p[x].y-p[y].y)*(p[x].y-p[y].y));
    }
    struct Edge{
        int u,v;
        double w;
    }e[M];
    int pre[N]; //记录前驱.
    int id[N],vis[N];
    double in[N];
    double dirMst(int root){
        double ans=0;
        while(1){
            for(int i=0;i<=n;i++) in[i]=INF;
            memset(id,-1,sizeof(id));
            memset(vis,-1,sizeof(vis));
            for(int i=1;i<=m;i++){
                int u=e[i].u,v=e[i].v;
                double w=e[i].w;
                if(w<in[v] && v!=u){
                    pre[v]=u;
                    in[v]=w;
                }
            }           //求最小入边集
            in[root]=0;
            pre[root]=root;
            for(int i=0;i<n;i++){
                if(in[i]==INF) return -1;
                ans+=in[i];
            }
            int idx = 0; //新标号
            for(int i=0;i<n;i++){
                if(vis[i] == -1 ){
                    int u = i;
                    while(vis[u] == -1){
                        vis[u] = i;
                        u = pre[u];
                    }
                    if(vis[u]!=i || u==root) continue;     //判断是否形成环
                    for(int v=pre[u];v!=u;v=pre[v] )
                        id[v]=idx;
                    id[u] = idx++;
                }
            }
            if(idx==0) break;
            for(int i=0;i<n;i++){
                if(id[i]==-1) id[i]=idx++;
            }
            for(int i=1;i<=m;i++){
                e[i].w-=in[e[i].v];
                e[i].u=id[e[i].u];
                e[i].v=id[e[i].v];
            }
            n = idx;
            root = id[root];//给根新的标号
        }
        return ans;
    }
    int main(){
        while(scanf("%d%d",&n,&m)!=EOF){
            for(int i=1;i<=n;i++) scanf("%d%d",&p[i].x,&p[i].y);
            for(int i=1;i<=m;i++){
                int u,v;
                scanf("%d%d",&u,&v);
                e[i].u=u-1;e[i].v=v-1;e[i].w=dis(u,v);
            }
            double ans = dirMst(0);
            if(ans==-1) puts("poor snoopy");
            else printf("%.2f
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    用Java求两个字符串所有的公共子序列
    Windows系统上安装logstash和logstash-input-jdbc
    Linux下zip文件的压缩和解压命令
    Linux-root用户下新建用户及为新用户配置密码
    Linux下查看是否安装jdk的命令
    使用git clone命令报错:error: RPC failed; curl 18 transfer closed with outstanding read data remaining
    SCP not a regular file
    常用正则表达式
    Android Wear 兼容
    git diff 配置 meld diff
  • 原文地址:https://www.cnblogs.com/heyuhhh/p/10372337.html
Copyright © 2011-2022 走看看