Command Network
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 12833   Accepted: 3717

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

POJ Monthly--2006.12.31, galaxy
题意:多组数据,给点数n,边数m,下面n行点坐标,m行边的出点入点,求1为根的最小树形图边权值。
算法:朱永津-刘振宏算法。
算法思想:
0. 若从根开始走有哪个点无法走到则没有最小树形图,甚至树形图都没有。
1. 对每个点求一个边权最小的前驱(求最小弧),并且对这些最小弧建新图(思想上建图)
2. 在新图上对每个环缩点,然后循环过程【1】;
3. 缩到不能再缩时(某次遍历没有环可以缩),则ans=∑除根节点以外每个点的最小弧权值
4. 返回ans算法完成实现。
ps .  1:算法的实现当然是需要一定的技巧的~~!
ps .  2:下附构造流程图和代码,实现过程在代码中有详细解释与举例。
#include<cstdio>
#include<cstring>
#include<cmath>
#define inf 2000000000
/*inf可以更大一点*/
#define N 200
#define M 2000
#define NN 50000
using namespace std;
struct Fiona
{double x,y;
}s[N];/*点*/
struct Syndra
{int u,v;double w;
}e[NN];/*边*/
int pre[N],f[N],visit[N];/*前驱,缩点时属于的点,缩点时的一个环判断*/
double l[N];/*l[i],i的最小弧长度*/
double dist(Fiona A,Fiona B)
{return sqrt((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y));/*求坐标之间直线距离*/
}
double Directed_MST(int root,int n,int m)
{int i,j,k;int u,v,cnt;double ans=0;while(1){cnt=0;memset(f,0,sizeof(f));memset(visit,0,sizeof(visit));
/*1.求每个点的最小入边(求最小弧集)*/for(i=1;i<=n;i++)l[i]=inf;/*清最大值(这里清的2*10^9其实不是最大值,但是也够了)*/for(i=1;i<=m;i++){u=e[i].u;v=e[i].v;if(l[v]>e[i].w&&u!=v)pre[v]=u,l[v]=e[i].w;/*更新最小弧*/}/*若某个非起点的点没有前驱(最小弧),即这个点无法通过别的某个点到达,则图非联通,则返回一个否定值↓↓↓*/for(i=1;i<=n;i++)if(l[i]==inf&&i!=root)return -1;l[root]=0;/*因为下面的循环要加上每个点的最小弧长度,而源点不要要入边,所以清一下*/for(i=1;i<=n;i++){ans+=l[i];v=i;while(visit[v]!=i&&!f[v]&&v!=root){/*从当前点往前驱找,直到找到一个环(visit[v]!=i)或者找到了源点,即无法接着找前驱了或者找到了某个已经被标记完的环(!f[v])*/visit[v]=i;/*不能在此处写visit[v]=1,然后判断!visit[v],因为在找的时候visit的作用是一个环回来,判断找到了一个环,而非是遍历过了*//*如      ① ↓ ⑦ ←⑥ ←② ←⑤ ↑     ↓  ↑ ⑧ →⑨  ③ →④  除了①到②长度100000,其他都小于10可以吧?然后手调一下就“理解深刻”了。ps:如果整个图是一个个三角环组成点连成的链,那么是不是可以比较有效地卡一下这个版本的朱刘算法实现呢? */v=pre[v];/*向前遍历寻找*/}if(v!=root&&!f[v]){/*注意此处的“!f[v]”没有虽然不会错,但是这会导致对环内每个点重复标记,一方面略有效卡评测,一方面调试时看缩点会很恶心*/f[v]=++cnt;for(u=pre[v];u!=v;u=pre[u])f[u]=cnt;/*这里随意写了,只要保证该在一个环的在一个环就行*/}}if(!cnt)break;/*此处判断有多少环,即进行了几次缩点,若无法再缩(cnt==0)则跳出*/for(i=1;i<=n;i++)if(!f[i])f[i]=++cnt;/*把剩余的点标记*/for(i=1;i<=m;i++){u=e[i].u;e[i].u=f[u];v=e[i].v;e[i].v=f[v];if(f[u]!=f[v])e[i].w-=l[v];/*O(m)对每条边进行权值缩减处理*//*为什么要缩减呢? 如:    ① ↓ ⑦ ←⑥ ←② ←⑤ ↑     ↓  ↑ ⑧ →⑨  ③ →④  除了①到②长度100000,其他都小于10。我们来手模拟一遍 : 当第一次缩点完成,即2345被缩好后, 我们的ans实际代表该图中除了① → ②以外所有边的权值和而真正的答案应该是除了⑤ → ②以外所有边的权值和。这样我们把②的所有入边权值减去⑤ → ②的长度,使得下次ans+边权的时候加的是(w【① → ②】-w【⑤ → ②】)即使得初始加的“⑤ → ②”边权值被删掉。(略有点网络流反向弧的感觉) */}n=cnt;/*因为缩点了,所以我们需要修改一个点的总数*/root=f[root];}return ans;
}
int main()
{freopen("test.in","r",stdin);int i,n,m;double ans;while(scanf("%d%d",&n,&m)!=EOF){/*此处的加点加边我不赘述了*/for(i=1;i<=n;i++)scanf("%lf%lf",&s[i].x,&s[i].y);for(i=1;i<=m;i++){scanf("%d%d",&e[i].u,&e[i].v);if(e[i].u!=e[i].v)e[i].w=dist(s[e[i].u],s[e[i].v]);else i--,m--;//除去自边  }ans=Directed_MST(1,n,m);if(ans==-1)printf("poor snoopy\n");else printf("%.2f\n",ans);/*poj double 要用%f */}return 0;
}

/*******************************************************************
再附个测试数据:

Input:
*******************************************************************
4 7
0 30
0 5
0 13
0 5
1 3
3 1
2 1
2 4
4 2
3 4
2 3
5 9
0 0
0 15
0 19
0 19
0 22
1 2
1 3
1 4
2 3
2 4
3 2
3 5
4 1
5 2
5 8
0 1
0 27
0 0
0 40
0 30
1 3
2 4
3 1
3 4
3 5
4 1
4 5
5 2
5 12
0 2
0 24
0 2
0 34
0 17
2 4
4 4
1 3
2 4
4 4
3 5
1 4
3 4
4 2
5 5
2 2
1 2
4 12
0 0
0 3
0 4
0 6
1 2
1 3
1 4
2 1
2 3
2 4
3 1
3 2
3 4
4 1
4 2
4 3
*******************************************************************
Output:
*******************************************************************
25.00
26.00
47.00
47.00
6.00
*******************************************************************/
复制去Google翻译翻译结果
</SPAN><预NAME =“针”类=“CPP”>的<span style=“字体大小:14px;”></ SPAN>的<span style=“字体大小:24PX;”><SPAN></SPAN>

POJ3164 最小树形图 有向图的最小生成树 模板题 朱刘算法 朱永津-刘振宏算法相关推荐

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

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

  2. 最小生成树模板题 P1692

    Description 给出N个顶点.E条边的连通无向简单图,请你完成下列任务: 任务1.求边权和最小的生成树(最小生成树) 任务2.求边权和最大的生成树(最大生成树) 任务3.求最大边最小的生成树( ...

  3. 图论-有向图的连通性模板题(hdu1296)(hdu1827)

    1.强连通分量: 强连通分量可以理解为边数最少的情况下是一个环. 这里写了一个模板题用的是tarjan算法,当然还有其他算法. tarjan算法的关键其实还是对于num数组和low数组的使用 然后可以 ...

  4. 最小生成树(模板题:最优布线问题,繁忙的都市,联络员)(C++)

    文章目录 序言 正文 First Promble 最优布线问题 时间限制: 1000 m s 1000 ms 1000ms 空间限制: 262144 K B 262144 KB 262144KB 题目 ...

  5. 天空之城(最小生成树模板题)

    题目描述 **链接:https://ac.nowcoder.com/acm/contest/9986/J 来源:牛客网 天空之城有5个小镇,名字分别为Ada, Aed, Akk, Orz, Apq,他 ...

  6. 最小树形图-朱刘算法详解 +例题解析

    文章目录 最小树形图 定义 和最小生成树的区别 朱刘算法 思想 步骤 流程展示 算法实现 例题 POJ3164_Command_Network HDU2121_Ice_cream's_world_II ...

  7. 一起开心2020暑假训练第二周 图论(模板题)

    比赛链接: 文章目录 A HDU 1285 一 B HDU 1863 起 C POJ 2387 开 D POJ 1502 心 E HDU 5922 图 F HDU 2112 论 A HDU 1285 ...

  8. 【POJ3164】Command Network 最小树形图模板题 重修版

    链接: #include <stdio.h> int main() {puts("转载请注明出处[vmurder]谢谢");puts("网址:blog.csd ...

  9. POj 3164 Command Network最小树形图 模板题 朱刘算法

    Command Network After a long lasting war on words, a war on arms finally breaks out between littleke ...

最新文章

  1. Java FAQ(6)
  2. python3 import 和__import__() 的区别
  3. any() missing 1 required positional arguments: dim
  4. binlog二进制文件解析
  5. 用生动的例子花式解释:python类中一定需要有 __init__方法么?没有会怎样?
  6. 如何在Mac计算机上轻松查找和删除类似照片
  7. 为啥HashMap的默认容量是16?
  8. ES6学习(箭头函数详解)
  9. 95.91p30.space\/index.php,关于 ThinkPHP6 分页样式的定制及点击下一页搜索条件丢失的解决方法...
  10. Python 3.6模拟输入并爬取百度前10页密切相关链接
  11. 点到线段的距离 计算几何
  12. 组队学习-数据采集-八爪鱼实操&使用感想
  13. NIOS II 内核使用 之 代码保存FLASH(EPCSX芯片)
  14. nodejs+express(ejs)做摇一摇小游戏(公司年会摇一摇游戏环节,大屏幕统计前几名摇动次数),大家一起摇一摇,看谁摇的次数多,并用excel-export导出excel
  15. 力扣(226.112)补9.8
  16. Java8 官方jvm 标准参考 -XX 配置参数详细信息
  17. 2014上海全国邀请赛题解 HDOJ 5090-5099
  18. 循环数142857问题 java_循环小数问题
  19. Onvif/RTSP流媒体安防RTSP无插件直播方案及RTSP配置规则
  20. 时间序列预测的8种常用方法简介

热门文章

  1. android 键盘 自动消失,Android 系统键盘怎么也不消失
  2. 2016年4月编程语言排行榜 Visual Basic正渐行渐远
  3. 排查maven中可以从远程下载下来jar包,但是却报错Failure to find was cached in the local repository, resolution will not
  4. 神器-可视化分析之Basemap实战详解(二)
  5. c语言大作业酒店管理系统,C语言酒店管理系统(最新整理)
  6. 二次吐血整理的 MAYA教程 快捷键大全,别收藏,直接粘贴拿走!
  7. [2008-05-26]我的梦
  8. html5 indexeddb 排序,HTML5 进阶系列:indexedDB 数据库
  9. Axure的强大逻辑交互
  10. 普宁市中学高考成绩查询2021,2021年中山高考状元多少分是谁,中山高考状元名单资料...