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

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

    题意:求有向图的最小生成树,叫做最小树形图,并规定起点恒为1

    算法:朱刘算法

      1 #include<cstdio>
      2 #include<cstring>
      3 #include<algorithm>
      4 #include<cmath>
      5 using namespace std;
      6 const double inf=0x7fffffff;
      7 struct node
      8 {
      9     int u,v;
     10     double w;
     11 }edge[11000];
     12 int n,m;
     13 double in[110];
     14 int pre[110];
     15 double dis(double x1,double x2,double y1,double y2)
     16 {
     17     return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
     18 }
     19 double solve(int root)
     20 {
     21     double ans=0;
     22     int mark[110],vis[110];
     23     int i;
     24     while(true)
     25     {
     26        for(i=0;i<n;i++)
     27            in[i]=inf;
     28        for(i=0;i<m;i++)//找每个点的最小入边,并记录前驱
     29        {
     30            int uu=edge[i].u;
     31            int vv=edge[i].v;
     32            if(edge[i].w<in[vv]&&uu!=vv)
     33            {
     34                in[vv]=edge[i].w;
     35                pre[vv]=uu;
     36            }
     37        }
     38        for(i=0;i<n;i++)//除了根节点,其他点不能为孤立点,否则无解
     39        {
     40            if(i==root) continue;
     41            if(in[i]==inf) return -1;
     42        }
     43        int cnt=0;
     44        memset(vis,-1,sizeof(vis));
     45        memset(mark,-1,sizeof(mark));
     46        in[root]=0;//删除根节点的最小入边
     47        for(i=0;i<n;i++)
     48        {
     49            ans+=in[i];
     50            int v=i;
     51            while(v!=root&&vis[v]!=i&&mark[v]==-1)//如果有环,这些点将在同一环中
     52            {
     53                vis[v]=i;
     54                v=pre[v];
     55            }
     56            if(v!=root&&mark[v]==-1)//找到环了
     57            {
     58                int u;
     59                for(u=pre[v];u!=v;u=pre[u])//给环中每个节点编上同号,代表缩环之后的点标号
     60                {
     61                    mark[u]=cnt;
     62                }
     63                mark[v]=cnt++;
     64            }
     65        }
     66        if(cnt==0) break;//没找到环,则最小生成树已求出
     67        for(i=0;i<n;i++)//没有在环中的点,给它们编号,下次循环还是单点
     68        {
     69            if(mark[i]==-1)
     70            mark[i]=cnt++;
     71        }
     72        for(i=0;i<m;i++)//缩环操作,环成单点
     73        {
     74            int u=edge[i].u;
     75            int v=edge[i].v;
     76            edge[i].u=mark[u];
     77            edge[i].v=mark[v];
     78            if(mark[u]!=mark[v])//不在环中的边,权值要减去in[v]
     79             edge[i].w-=in[v];
     80        }
     81        n=cnt;//更新下次循环的顶点数和根节点编号
     82        root=mark[root];
     83     }
     84     return ans;
     85 }
     86 int main()
     87 {
     88     double x[110],y[110];
     89     int i;
     90     while(scanf("%d%d",&n,&m)!=EOF)
     91     {
     92         for(i=0;i<n;i++)
     93             scanf("%lf%lf",&x[i],&y[i]);
     94         for(i=0;i<m;i++)
     95         {
     96             int a,b;
     97             scanf("%d%d",&a,&b);
     98             edge[i].u=--a;
     99             edge[i].v=--b;
    100             if(edge[i].u!=edge[i].v)//去掉自环
    101             edge[i].w=dis(x[a],x[b],y[a],y[b]);
    102             else edge[i].w=inf;
    103         }
    104         double ans=solve(0);
    105         if(ans<0) printf("poor snoopy\n");
    106         else
    107         printf("%.2f\n",ans);
    108     }
    109     return 0;
    110 }
  • 相关阅读:
    分布式系统关注点(3)——过去这几十年,分布式系统的「数据一致性」精华都在这了!
    分布式系统关注点(1)——不知道是不是最通俗易懂的《数据一致性》剖析了
    《西虹市首富》给我们技术人带来的思考
    Kaazing Gateway简单使用
    pylot测试工具环境搭建
    NodeJS学习笔记
    JavaScript逗号操作符
    翻译:SockJS-node文档(一)
    【原】Learning Spark (Python版) 学习笔记(二)----键值对、数据读取与保存、共享特性
    2015年总结与2016年目标和计划
  • 原文地址:https://www.cnblogs.com/mt522/p/5303834.html
Copyright © 2011-2022 走看看