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\n");
else
printf("%.2lf\n",ans);
}
return 0;
}

最小树形图(poj3164)的更多相关文章

  1. poj3164 (朱刘算法 最小树形图)

    题目大意:给定n个点坐标,m条有向边,要求最小树形图. 题解:直接上模板,前面打的 vis[v]=i一直把i打成1,一直TLE. #include<iostream> #include&l ...

  2. POJ3164 Command Network —— 最小树形图

    题目链接:https://vjudge.net/problem/POJ-3164 Command Network Time Limit: 1000MS   Memory Limit: 131072K ...

  3. POJ3164 Command Network(最小树形图)

    图论填个小坑.以前就一直在想,无向图有最小生成树,那么有向图是不是也有最小生成树呢,想不到还真的有,叫做最小树形图,网上的介绍有很多,感觉下面这个博客介绍的靠谱点: http://www.cnblog ...

  4. poj3164 最小树形图板子题

    /* 思路很简单,也不知道哪里错了TAT */ /* N个点通过笛卡尔坐标表示 根节点是1,求最小树形图 */ #include<iostream> #include<cstdio& ...

  5. poj3164最小树形图模板题

    题目大意:给定一个有向图,根节点已知,求该有向图的最小树形图.最小树形图即有向图的最小生成树,定义为:选择一些边,使得根节点能够到达图中所有的节点,并使得选出的边的边权和最小. 题目算法:朱-刘算法( ...

  6. poj3164(最小树形图&朱刘算法模板)

    题目链接:http://poj.org/problem?id=3164 题意:第一行为n, m,接下来n行为n个点的二维坐标, 再接下来m行每行输入两个数u, v,表点u到点v是单向可达的,求这个有向 ...

  7. POJ - 3164-Command Network 最小树形图——朱刘算法

    POJ - 3164 题意: 一个有向图,存在从某个点为根的,可以到达所有点的一个最小生成树,则它就是最小树形图. 题目就是求这个最小的树形图. 参考资料:https://blog.csdn.net/ ...

  8. bzoj4349: 最小树形图

    最小树形图模板题…… 这种\(O(nm)\)的东西真的能考到么…… #include <bits/stdc++.h> #define N 60 #define INF 1000000000 ...

  9. hdu 4966 GGS-DDU (最小树形图)

    比较好的讲解:http://blog.csdn.net/wsniyufang/article/details/6747392 view code//首先为除根之外的每个点选定一条入边,这条入边一定要是 ...

随机推荐

  1. (二)使用预定义模型 QStringListModel例子

    使用预定义模型 QStringListModel例子 源代码如下 Main.cpp #include <QApplication> #include "teamleadersdi ...

  2. BCM_GPIO驱动测试

    在写内核驱动的时候,最好先在uboot上,进行裸板测试,验证寄存器,再移植到内核中,这样可以熟悉寄存器,也排除内核中的一些干扰. /********************************** ...

  3. Spring 4 官方文档学习(十)数据访问之OXM

    http://docs.spring.io/spring/docs/current/spring-framework-reference/html/oxm.html Java Object 与 XML ...

  4. Spring bean的初始化及销毁

    Spring bean的几个属性:scope.init-method.destroy-method.depends-on等. Scope 在Spring容器中是指其创建的Bean对象相对于其他Bean ...

  5. (转)Invalidate、RedrawWindow与UpdateWindow的区别

     一:什么时候才会发生重绘窗口的消息? 当需要更新或重新绘制窗口的外观时,应用程序就会发送WM_PAINT消息.对窗口进行重新绘制. 二:Invalidate() -- RedrawWindow() ...

  6. HDF5 文件格式简介

    三代测序下机的原始数据不再是fastq格式了,而是换成了hdf5 格式,在做三代数据的分析之前,有必要先搞清楚hdf5 这种文件格式; 官网的链接如下:https://support.hdfgroup ...

  7. vc 获取 硬盘序列号 和 cpu

    vc 获取 硬盘序列号 和 cpu 唯一iD的方法?如题---------网上找来很多资料 也没找到, 要支持xp win7 32/64 系统下都能获取 硬盘序列号 和cpu ID 哪位朋友帮帮忙: ...

  8. Java基础--常用IO流使用实例

    读取文本文件: private static void ioDemo1() { try { FileReader fileReader = new FileReader("C:\\Users ...

  9. logging.xml file setfile(null,true) call failed

    定义目录三个方法:一:${catalina.base}或${catalina.home}相对路径配置方法.catalina.home是你配置服务器时自动在环境变量中加的路径,默认是指向tomcat服务 ...

  10. 【Raspberry Pi】openwrt 路由

    http://blog.sina.com.cn/s/blog_40983e5e0102v6qt.html