题目链接:

题目

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. spark写入Oracle 报错 java.lang.ArrayIndexOutOfBoundsException: -32423

    原因: oracle 10g的驱动执行的批量提交只支持32768个参数,如果表的字段多于32个,就会出现该异常 解决办法: 升级oracle的驱动版本,换成ojdbc6.jar

  2. 时间类型(DataTime)赋空值

    暂时只发现这一个方法 如果直接Datetime time=DBNull.Value;会报null与DataTime没有隐式转换 SqlCommand cmd = SqlCommand(conn); / ...

  3. Base Pattern基本模式_Gateway入口

    •Gateway入口 ◦一个封装了对外部系统或资源访问的对象. ◾OO系统中,也需要访问一些不是对象的事物,DB表,XML,事务. ◾这些外部资源的API很复杂. ◾入口类对象将简单的方法调用转换成相 ...

  4. 如何给xml应用样式

    引子:可扩展标记语言xml(Extensible Markup Language)自己平常也用到的不多,除了在ajax处理服务器返回的数据可能会用到外(不过一般用json处理数据的比较常见)还真没怎么 ...

  5. 8个web前端的精美HTML5 & CSS3效果及源码下载

    作为一个前沿的 Web 开发者,对于 HTML5 和 CSS3 技术或多或少都有掌握.前几年这些新技术刚萌芽的时候,开发者们已经使用它们来小试牛刀了,如今这些先进技术已经遍地开发,特别是在移动端大显身 ...

  6. transport

    #include<iostream> using namespace std; int transport(int a) { ; ) ; else a=a/; d=; ) { a=a*+; ...

  7. The New Debugger

    在debug下有一个中文叫做杂项的选项卡下有配置的内容 里面可以配置debug的模式 有的时候一些莫名其妙的问题需要 调整里面的设置 <<SAP debug的几种方式.pdf>> ...

  8. Linux下的vi编辑器与gcc工具的使用

    最近在网上找了些视频,自学了一点Linux环境下,C编程的方法. 注 vi与vim是有区别的,vim打开的源码文件其中的关键字是有颜色的. vi编辑器有3种模式,命令行模式,插入模式,底行模式. 如果 ...

  9. PHP性能优化-编译级别的缓存

    最近安装了 php5.6,发现有了 opcache.so扩展文件,于是,搜索了一下,发现 zend opcache已经融入到 ph5.5以上的版本了,即兴奋,不用再去找xcache,apc,eAcce ...

  10. Ubontu使用技巧

    1. ctrl + alt + T  =>  打开命令行窗口 2. sudo su => 开启root权限 3. cd  => 打开文件夹 4. cd "Program F ...