zoukankan      html  css  js  c++  java
  • POJ 3164——Command Network——————【最小树形图、固定根】

    Command Network
    Time Limit: 1000MS   Memory Limit: 131072K
    Total Submissions: 15080   Accepted: 4331

    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 Mlines 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

    Source

     
     
    题目大意:问你起点在1,存不存在最小有向生成树,如果存在输出结果,如果不存在,输出poor Snoopy。
     
    解题思路:固定根的最小树形图。直接用朱刘算法套上就能过了。    注意点:1.如果是不定根,可以自己加一个起点,然后让起点跟其他点连边。2.不能记录路径。
     
    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<algorithm>
    #include<iostream>
    #include<vector>
    using namespace std;
    const int maxn = 1100;
    const int INF = 0x3f3f3f3f;
    struct Coor{
        double x,y;
    }coors[maxn];
    struct Edge{
        int from,to;
        double dist;
    }edges[maxn*maxn];
    int pre[maxn],vis[maxn],ID[maxn];
    double In[maxn];
    double distan(Coor a,Coor b){
        double dx = a.x - b.x;
        double dy = a.y - b.y;
        return sqrt( dx * dx + dy * dy);
    }
    double Zhuliu(int root,int n,int m){
        double ret = 0;
        int u,v;
        while(true){
            for(int i = 0; i < n; i++){
                In[i] = 1.0*INF;
            }
            for(int i = 0; i < m; i++){
                Edge &e = edges[i];
                u = e.from; v = e.to;
                if(In[v] > e.dist && u != v){
                    pre[v] = u;
                    In[v] = e.dist;
                }
            }
            for(int i = 0; i < n; i++){
                if(i == root) continue;
                if(In[i] == INF)
                    return -1;
            }
            In[root] = 0;
            int cntcir = 0;
            memset(vis,-1,sizeof(vis));
            memset(ID,-1,sizeof(ID));
            for(int i = 0; i < n; i++){
                ret += In[i];
                v = i;
                while(vis[v]!= i && ID[v] ==-1 &&v != root){
                    vis[v] = i;
                    v = pre[v];
                }
                if(v != root && ID[v] == -1){
                    for(u = pre[v]; u != v; u = pre[u]){
                        ID[u] = cntcir;
                    }
                    ID[v] = cntcir++;
                }
            }
            if(cntcir == 0){
                break;
            }
            for(int i = 0; i < n; i++){
                if(ID[i]==-1){
                    ID[i] = cntcir++;
                }
            }
            for(int i = 0; i < m; i++){
                v = edges[i].to;
                Edge & e = edges[i];
                e.from = ID[e.from];
                e.to = ID[e.to];
                if(e.from != e.to){
                    e.dist -= In[v];
                }
            }
            n = cntcir;
            root = ID[root];
        }
        return ret;
    }
    int main(){
        int n,m;
        while(scanf("%d%d",&n,&m)!=EOF){
            for(int i = 0; i < n; i++){
                scanf("%lf%lf",&coors[i].x,&coors[i].y);
            }
            int a,b;
            for(int i = 0; i < m; i++){
                scanf("%d%d",&a,&b);
                a--,b--;
                edges[i].from = a;
                edges[i].to = b;
                if(a == b){
                    edges[i].dist = 1.0*INF;
                    continue;
                }
                edges[i].dist = distan(coors[a],coors[b]);
            }
            double res = Zhuliu(0,n,m);
            if(res == -1){
                puts("poor snoopy");
            }else{
                printf("%.2f
    ",res);
            }
        }
        return 0;
    }
    

      

  • 相关阅读:
    objectivec 中 category 和 extension 的区别
    提高audioqueue启动时间的一个技巧
    iOS中url中文编码问题
    XP重装后grub引导修复( Ubuntu10.10 与XP双系统 )
    Ubuntu10.10 与XP双系统安装
    BSF脚本引擎
    随机访问类RandomAccessFile多线程下载
    Spring AOP解决系统日志备忘
    OA和KM知识管理的区别
    JavaMail接收指定账号邮件
  • 原文地址:https://www.cnblogs.com/chengsheng/p/4933191.html
Copyright © 2011-2022 走看看