题目链接:

题目

Command Network

Time Limit: 1000MS

Memory Limit: 131072K

问题描述

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.

输入

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.

输出

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

样例

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

output

31.19

poor snoopy

题意

求固定根的最小树形图

题解

朱刘算法

代码

#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std; const int maxn = 111;
const int maxm = 11111;
const double INF = ~0u >> 1; struct Edge {
int u, v;
double w;
}egs[maxm]; struct Point {
int x, y;
}pt[maxn]; int n, m; double dis(const Point& p1, const Point& p2) {
return sqrt(1.0*(p1.x - p2.x)*(p1.x - p2.x) + 1.0*(p1.y - p2.y)*(p1.y - p2.y));
} double in[maxn];
int id[maxn], vis[maxn], pre[maxn];
double Directed_MST(int rt) {
double ret = 0;
while (1) {
//求最小入度边
for (int i = 0; i < n; i++) in[i] = INF;
for (int i = 0; i < m; i++) {
Edge& e = egs[i];
if (e.w < in[e.v] && e.u != e.v) {
in[e.v] = e.w;
pre[e.v] = e.u;
}
}
for (int i = 0; i < n; i++) {
if (i!=rt&&in[i] == INF) return -1;
}
int tot = 0;
memset(id, -1, sizeof(id));
memset(vis, -1, sizeof(vis));
in[rt] = 0;
//找环,缩点
for (int i = 0; i < n; i++) {
ret += in[i];
int v = i;
while (vis[v] != i&&id[v] == -1 && v != rt) {
vis[v] = i;
v = pre[v];
}
if (id[v] == -1 && v != rt) {
for (int u = pre[v]; u != v; u = pre[u]) {
id[u] = tot;
}
id[v] = tot++;
}
}
//没有环
if (tot == 0) break;
for (int i = 0; i < n; i++) {
if (id[i] == -1) id[i] = tot++;
}
//更新到环的距离
for (int i = 0; i < m; i++) {
Edge& e = egs[i];
int v = e.v;//这个v要留下来!
e.u = id[e.u],e.v = id[e.v];
if (e.u != e.v) {
e.w -= in[v];
}
}
n = tot;
rt = id[rt];
}
return ret;
} int main() {
while (scanf("%d%d", &n, &m) == 2 && n) {
for (int i = 0; i < n; i++) {
scanf("%d%d", &pt[i].x, &pt[i].y);
}
for (int i = 0; i < m; i++) {
Edge& e = egs[i];
scanf("%d%d", &e.u, &e.v),e.u--,e.v--;
if (e.u != e.v) e.w = dis(pt[e.u], pt[e.v]);
else e.w = INF;
}
double ans = Directed_MST(0);
if (ans ==-1) {
puts("poor snoopy");
}
else {
printf("%.2f\n", ans);
}
}
return 0;
}

POJ 3164 Command Network 最小树形图的更多相关文章

  1. POJ 3164 Command Network 最小树形图模板

    最小树形图求的是有向图的最小生成树,跟无向图求最小生成树有很大的区别. 步骤大致如下: 1.求除了根节点以外每个节点的最小入边,记录前驱 2.判断除了根节点,是否每个节点都有入边,如果存在没有入边的点 ...

  2. POJ 3164 Command Network 最小树形图 朱刘算法

    =============== 分割线之下摘自Sasuke_SCUT的blog============= 最 小树形图,就是给有向带权图中指定一个特殊的点root,求一棵以root为根的有向生成树T, ...

  3. POJ3436 Command Network [最小树形图]

    POJ3436 Command Network 最小树形图裸题 傻逼poj回我青春 wa wa wa 的原因竟然是需要%.2f而不是.2lf 我还有英语作业音乐作业写不完了啊啊啊啊啊啊啊啊啊 #inc ...

  4. poj 3164 Command Network

    http://poj.org/problem?id=3164 第一次做最小树形图,看着别人的博客写,还没弄懂具体的什么意思. #include <cstdio> #include < ...

  5. POJ 3164——Command Network——————【最小树形图、固定根】

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

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

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

  7. POJ 3164 Command Network(最小树形图模板题+详解)

    http://poj.org/problem?id=3164 题意: 求最小树形图. 思路: 套模板. 引用一下来自大神博客的讲解:http://www.cnblogs.com/acjiumeng/p ...

  8. POJ 3164 Command Network ( 最小树形图 朱刘算法)

    题目链接 Description After a long lasting war on words, a war on arms finally breaks out between littlek ...

  9. poj 3164 Command Network(最小树形图模板)

    Command Network http://poj.org/problem?id=3164 Time Limit: 1000MS   Memory Limit: 131072K Total Subm ...

随机推荐

  1. (转)Android之自定义适配器

    ListView作为一个实际开发中使用率非常高的视图,一般的系统自带的适配器都无法满足开发中的需求,这时候就需要开发人员来自定义适配器使得ListView能够有一个不错的显示效果. 有这样一个Demo ...

  2. IOS 模仿TableView封装

    一.先贴一下未封装的代号,好跟后面的对比 @interface MTHomeDropdown : UIView + (instancetype)dropdown; @property (nonatom ...

  3. SQL Server 安装程序失败 不能在控件上调用 Invoke 或 BeginInvoke

    出现这种问题的原因是权限问题,怎么处理呢,使用管理员运行 如果这种方法不行,比如我的就不可以,点击右键 对各个权限对象重新添加完全控制权限. 我的电脑的情况是安装sql2010,然后安装sql管理工具 ...

  4. 20141128—JavaScript对象

    JavaScript 中的所有事物都是对象:字符串.数值.数组.函数... String 对象的 length 属性来获得字符串的长度: var message="Hello World!& ...

  5. ThinkPHP控制器

    ThinkPHP控制器Controller 1.什么是控制器 在MVC框架中,其核心就是C(Controller)控制器.主要用于接收用户请求,处理业务逻辑. 2.控制器的定义 在一个ThinkPHP ...

  6. daxuez.com

    大学z,一个还没想好的名字和项目 初期定位:服务于武汉各大高校的大学学生群体 服务项目:兼职.旅游.培训.租车.班服订做.票务

  7. 【风马一族_Android】Android Studio 给APP设置签名

    在Android Studio中,给App签名,如果没有给App设置签名的话,Android Studio会主动给app设置一个默认的签名 接下来,介绍主动给App设置一个签名的整个步骤过程: 1) ...

  8. ADO.NET笔记——将DataReader作为函数返回值

    相关知识: 在很多情况下,可能把数据库的访问封装到一个函数中,通过该函数返回一个DataReader对象给调用者.例如定义函数:SqlDataReader returnDR(),然后再Main函数中调 ...

  9. Eclispe使用Maven添加官方库的jar包

    先到百度或google搜索maven仓库,在仓库中搜索需要的jar包,如poi.jar. 搜索到之后找到需要的jar包,找到这里

  10. 封装底层Ajax

    创建Ajax简易步骤:创建Ajax对象:var xhr=new XMLHttpRequest();链接服务器:xhr.open('get','a.php',true);发送请求或数据:xhr.send ...