Command Network

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

给 n 个点的坐标,m 对相连的点(给出 m 对下标,可认为是点的编号,点之间的距离就是边权),求最小树形图
模板题
从这里 https://www.cnblogs.com/hdu-zsk/p/8167687.html 学的,图解很详细
最小树形图(Arborescence,亦称Directed Rooted Tree)是有向图的最小生成树(Minimum Spanning Tree)
常用的有朱刘算法和 tarjan
朱刘算法分为四个过程:
1)求最短弧集合 E
2)判断集合 E 中有没有有向环,如果有转步骤 3,否则转 4
3)收缩点,把有向环收缩成一个点,并且对图重新构建,包括边权值的改变和点的处理,之后再转步骤 1
4)展开收缩点,求得最小树形图

收缩点时,为什么出边的权不变,入边的权要减去in [u]: 对于新图中的最小树形图T,设指向人工节点的边为e。将人工节点展开以后,e指向了一个环。假设原先e是指向u的,这个时候我们将环上指向u的边 in[u]删除,这样就得到了原图中的一个树形图。我们会发现,如果新图中e的权w’(e)是原图中e的权w(e)减去in[u]权的话,那么在我们删除 掉in[u],并且将e恢复为原图状态的时候,这个树形图的权仍然是新图树形图的权加环的权,而这个权值正是最小树形图的权值。所以在展开节点之后,我们 得到的仍然是最小树形图。逐步展开所有的人工节点,就会得到初始图的最小树形图了
这是个没看懂的解释,看了代码之后发现,有一段是这样的:

for(i = 1; i <= n; ++i){res += in[i];

假设不恢复原图,结果里加上了这些 in[v], 所以在新图里,到这一块的每个入边权要减掉这一块

#include <cstdio>
#include <iostream>
#include <cstring>
#include <queue>
#include <cmath>
#include <set>
#include <algorithm>
#include <map>
#include <vector>
#define mod 1000000007
using namespace std;
typedef long long ll;const int N = 106;   //点的数量
const double inf = 0x3f3f3f3f;
const double eps = 1e-6;int pre[N], vis[N], id[N], x[N], y[N];  pre[v] = u, // 记录最小入边起点,id 给环编号,x, y 坐标
int cnt, n, m;
double in[N];  //记录每个点的最小入边边权,本题是 double 类型struct node
{int u, v;double w;
}e[N*N];void add(int u, int v, double w)
{e[cnt].u = u;e[cnt].v = v;e[cnt].w = w;++cnt;
}double dis(int x1, int y1, int x2, int y2)  //计算距离
{return sqrt((double)(x1-x2)*(x1-x2)+(double)(y1-y2)*(y1-y2));
}double zhuliu(int rt, int n, int m)
{int i, j, u, v, ct = 0;double w, res = 0;   //res 总权值while(1){for(i = 1; i <= n; ++i)in[i] = inf;  初始化for(i = 0; i < m; ++i){   //遍历所有边,找每个点的最小入边u = e[i].u;v = e[i].v;if(e[i].w < in[v] && u != v)in[v] = e[i].w, pre[v] = u;}for(i = 1; i <= n; ++i)   //遍历所有点,如果除根点外有孤立点,最小树形图不存在if(i != rt && fabs(in[i]-inf) < eps)return -1;for(i = 1; i <= n; ++i)  //初始化vis[i] = id[i] = 0;vis[rt] = 1;in[rt] = ct = 0;for(i = 1; i <= n; ++i){  //遍历所有点,寻找所有环res += in[i];  //先加上最小边权v = i;while(vis[v] != i && id[i] == 0 && v != rt)vis[v] = i, v = pre[v];if(id[v] == 0 && v != rt){  //说明上面循环是因为vis[i] == i, 出现已经标记的点,存在环id[v] = ++ct; for(u = pre[v]; u != v; u = pre[u])  //给这个环里的点标上同一个编号id[u] = ct;}}if(ct == 0) //无环break;for(i = 1; i <= n; ++i) //遍历所有点,给独立点也更新编号if(!id[i])id[i] = ++ct;for(i = 0; i < m; ++i){  //建立新图,缩点标记,更新最小边权u = e[i].u;v = e[i].v;e[i].u = id[u];e[i].v = id[v];if(id[u] != id[v])e[i].w -= in[v];}n = ct;rt = id[rt]; // 循环,看新图是否有环}return res;
}int main()
{int n, m, i, a, b;while(~scanf("%d%d", &n, &m)){cnt = 0;memset(e, 0, sizeof e);for(i = 0; i <= n; ++i)pre[i] = i;for(i = 1; i <= n; ++i)scanf("%d%d", &x[i], &y[i]);for(i = 0; i < m; ++i){scanf("%d%d", &a, &b);add(a, b, dis(x[a], y[a], x[b], y[b]));}double res = zhuliu(1, n, m);if(res == -1)printf("poor snoopy\n");elseprintf("%.2lf\n", res);}return 0;
}

POj 3164 Command Network最小树形图 模板题 朱刘算法相关推荐

  1. POJ 3164 Command Network (最小树形图)

    Command Network Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 10136   Accepted: 2946 ...

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

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

  3. POJ3164 最小树形图 有向图的最小生成树 模板题 朱刘算法 朱永津-刘振宏算法

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

  4. POJ-3164 Command Network 最小树形图

    题目链接:http://poj.org/problem?id=3164 裸的最小树形图,用朱-刘算法解决,具体实现过程如下:算法一开始先判断从固定根开始是否可达所有原图中的点,若不可,则一定不存在最小 ...

  5. POJ 3164 Command Network (最小树形图)

    [题目链接]http://poj.org/problem?id=3164 [解题思路]百度百科:最小树形图 ]里面有详细的解释,而Notonlysucess有精简的模板,下文有对其模板的一点解释,前提 ...

  6. [POJ 3164]Command Network(最小树形图,朱刘算法)

    文章目录 title solution code title solution 读完翻译后,很明显就是个朱刘算法的板子题 最小树形图,就是给出一个带权有向图 从中指定一个特殊的结点 root 求一棵以 ...

  7. POJ 3164 Command Network(朱刘算法)

    Command Network Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 21481   Accepted: 6117 ...

  8. POJ 3164 Command Network

    Description After a long lasting war on words, a war on arms finally breaks out between littleken's ...

  9. poj 3164 Command Network

    最小树形图,,不熟练啊,写错几个地方无限TLE #include<stdio.h> #include<string.h> #include<math.h> #def ...

最新文章

  1. k8s部署jar包_学习K8S之路.6--- 在K8S中部署Jenkins,并使用Jenkins打包jar包
  2. 教AI逐帧搓招玩《铁拳》通关最高难度,现在的街机游戏爱好者有点东西啊
  3. windows安装ffmpeg
  4. Promise 的基础用法
  5. 关于servlet中出现GET方法不能应用于此url的解决办法
  6. Mac 被曝存在恶意漏洞:黑客可随意调动摄像头,波及四百万用户!
  7. 第D题 把手放在键盘上时,稍不注意就会往右错一位。
  8. SQL SERVER 查找某个字符在字符串中出现的次数
  9. hdu1215七夕节
  10. 2020年MathorCup数学建模B题养老服务床位需求预测与运营模式研究全过程解题程序及多篇论文
  11. 游戏程序开发的工作主要包括哪些方面
  12. S7-200SMART案例分析——伺服接线
  13. STGNN(www 2020)论文总结
  14. linux中获取几天前或者几天后的日期
  15. 影响人生的五大定律,值得一读
  16. 初学者都能看懂的 Spring 源码之依赖注入(DI)源码分析
  17. maccamy fuchs matlab,近海固定式风机单桩大直径基础波浪载荷研究
  18. mysql通过ip地址进行访问
  19. 值得推荐的免费网上课程
  20. Matplotlib实践

热门文章

  1. ubuntu编辑只读文件
  2. 秀动脚本增加微信通知和多账号抢购
  3. 【Java开发】Java实现调用微信机器人,发送企业微信通知
  4. iPhoneX炫彩壁纸背景demo(含动效)
  5. 网络上的计算机无权限访问权限,电脑连不上网,提示无网络访问权限怎么办?...
  6. python中description_python中cursor.description什么意思
  7. Xilinx IDELAYE2应用笔记及仿真实操
  8. 怎么隐藏label标签
  9. COVID-19 Cases Prediction (Regression)
  10. Java经典300例-基础篇-001:Hello Kitty