zoukankan      html  css  js  c++  java
  • 朱刘算法

    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

    没有完全理解 等学完塔杨缩点 回来补坑

    先传上抄的代码作为模板

    //朱刘算法
    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<algorithm>
    typedef long long LL;
    const int N=110;
    const int M=10010;
    const int INF=0x3f3f3f3f;
    const double inf=1e10;
    using namespace std;
    struct node{
        int x,y;
    }point[N];
    struct edg {
        int u, v;
        double cost;
    } edge[M];
    int n, m;
    int id[N],vis[N],pre[N];
    double in[N];
    double Directed_MST(int root) {
        double ret = 0;
        while(true) {
            for(int i=0;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].cost < in[v] && u != v) {
                    in[v] = edge[i].cost;
                    pre[v] = u;
                }
            }
            for(int i=0;i<n;i++) { //如果存在除root以外的孤立点,则不存在最小树形图
                if(i == root)  continue;
                if(in[i] == inf)  return -1;
            }
            int cnt = 0;
            memset(vis,-1,sizeof(vis));
            memset(id,-1,sizeof(id));
            in[root] = 0;
            for(int i=0;i<n;i++) {    //找环
                ret += 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;
                    id[v] = cnt++;
                }
            }
            if(cnt == 0) break;
            for(int i=0;i<n;i++)
                if(id[i] == -1)
                    id[i] = cnt++;    //重新标号
            for(int i=0;i<m;i++){    //更新其他点到环的距离
                int v =edge[i].v;
                edge[i].u = id[edge[i].u];
                edge[i].v = id[edge[i].v];
                if(edge[i].u != edge[i].v)
                    edge[i].cost -= in[v];
            }
            n = cnt;
            root = id[root];
        }
        return ret;
    }
    double distant(node a,node b)
    {
        return sqrt((double)((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)));
    }
    int main()
    {
        while(scanf("%d%d",&n,&m)!=EOF){
            for(int i=0;i<n;i++)
                scanf("%d%d",&point[i].x,&point[i].y);
            for(int i=0;i<m;i++){
                int a,b;
                scanf("%d%d",&a,&b);
                edge[i].u=a-1;
                edge[i].v=b-1;
                if(edge[i].u!=edge[i].v)
                    edge[i].cost=distant(point[edge[i].u],point[edge[i].v]);
                else edge[i].cost=inf;
            }
            double ans=Directed_MST(0);
            if(ans==-1)
                printf("poor snoopy
    ");
            else
                printf("%.2lf
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    Linux内核源码分析方法
    OVS处理upcall流程分析
    Linux内核源码目录结构分析
    理解OpenStack中的OpenvSwitch的几个要点
    OVS源码connmgr_run分析
    ovs-appctl 命令合集
    云计算底层技术-使用openvswitch
    OVS架构
    Open vSwitch Datapath浅析
    Openvswitch原理与代码分析(4):网络包的处理过程
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852296.html
Copyright © 2011-2022 走看看