Command Network

Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 20766   Accepted: 5920

题目链接:http://poj.org/problem?id=3164

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

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

题意:

给出一个有向图,求出这个图的最小生成树。

题解:

直接用朱刘算法即可解决这个问题,可以参见:https://www.cnblogs.com/thefirstfeeling/p/4410705.html

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cmath>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = ,M = 1e4+;
int n,m;
struct Point{
int x,y;
}p[N];
double dis(int x,int y){
return sqrt((double)(p[x].x-p[y].x)*(p[x].x-p[y].x)+(double)(p[x].y-p[y].y)*(p[x].y-p[y].y));
}
struct Edge{
int u,v;
double w;
}e[M];
int pre[N]; //记录前驱.
int id[N],vis[N];
double in[N];
double dirMst(int root){
double ans=;
while(){
for(int i=;i<=n;i++) in[i]=INF;
memset(id,-,sizeof(id));
memset(vis,-,sizeof(vis));
for(int i=;i<=m;i++){
int u=e[i].u,v=e[i].v;
double w=e[i].w;
if(w<in[v] && v!=u){
pre[v]=u;
in[v]=w;
}
} //求最小入边集
in[root]=;
pre[root]=root;
for(int i=;i<n;i++){
if(in[i]==INF) return -;
ans+=in[i];
}
int idx = ; //新标号
for(int i=;i<n;i++){
if(vis[i] == - ){
int u = i;
while(vis[u] == -){
vis[u] = i;
u = pre[u];
}
if(vis[u]!=i || u==root) continue; //判断是否形成环
for(int v=pre[u];v!=u;v=pre[v] )
id[v]=idx;
id[u] = idx++;
}
}
if(idx==) break;
for(int i=;i<n;i++){
if(id[i]==-) id[i]=idx++;
}
for(int i=;i<=m;i++){
e[i].w-=in[e[i].v];
e[i].u=id[e[i].u];
e[i].v=id[e[i].v];
}
n = idx;
root = id[root];//给根新的标号
}
return ans;
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
for(int i=;i<=n;i++) scanf("%d%d",&p[i].x,&p[i].y);
for(int i=;i<=m;i++){
int u,v;
scanf("%d%d",&u,&v);
e[i].u=u-;e[i].v=v-;e[i].w=dis(u,v);
}
double ans = dirMst();
if(ans==-) puts("poor snoopy");
else printf("%.2f\n",ans);
}
return ;
}

POJ3164:Command Network(有向图的最小生成树)的更多相关文章

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

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

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

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

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

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

  4. Command Network

    Command Network Time Limit: 1000MSMemory Limit: 131072K Total Submissions: 11970Accepted: 3482 Descr ...

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

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

  6. POJ 3164 Command Network 最小树形图

    题目链接: 题目 Command Network Time Limit: 1000MS Memory Limit: 131072K 问题描述 After a long lasting war on w ...

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

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

  8. Command Network OpenJ_Bailian - 3436(最小有向生成树模板题)

    链接: http://poj.org/problem?id=3164 题目: Command Network Time Limit: 1000MS   Memory Limit: 131072K To ...

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

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

随机推荐

  1. CodeForces - 913C(二进制)

    链接:CodeForces - 913C 题意:给出 n 瓶饮料的花费 C 数组,每瓶的体积是 2^(i-1) 升,求至少买 L 升的最少花费. 题解:二进制数的组合可以表示任何一个数.第 i 的饮料 ...

  2. python selenium 使用htmlunit 执行测试。非图形界面浏览器。

    其实就是换个浏览器,只是这个浏览器没有图形界面而已. browser = webdriver.Chrome() 换成 browser = webdriver.Remote(desired_capabi ...

  3. [HNOI2017]影魔

    题意: 给定 \(n\) 个数的排列,\(m\) 次询问,每次询问询问一个区间内所有子区间的贡献. 每个区间如果两个端点分别是最大值和次大值,我们就算 \(P1\) 的贡献. 如果两个端点一个是最大值 ...

  4. CryptoZombies学习笔记——Lesson5

    chapter1:token代币 简而言之,通证就是支持交易的包含一系列规范的函数接口的一个智能合约,发币可以用ERC20标准,但是像僵尸这种非同质化代币,需要用ERC721标准 chapter2:e ...

  5. geth账户密码

    xiaocong geth账户密码 123 {d6abe909013d8da914ae2a08c9b58e7b76601b39} 账户密码 123456 0x4A7F15104F54dB3214D2F ...

  6. 十一:Centralized Cache Management in HDFS 集中缓存管理

    集中的HDFS缓存管理,该机制可以让用户缓存特定的hdfs路径,这些块缓存在堆外内存中.namenode指导datanode完成这个工作. Centralized cache management i ...

  7. 最小生成树——prim

    prim:逐“点”生成最小生成树 与Dijkstra不同的是:加入点到生成树中,不要考虑与源点的距离,而是考虑与生成树的距离 #include <iostream> #include &l ...

  8. “今日校园” App 用户体验分析

    一.背景 为进一步提升信息化应用水平,更好的服务师生,南通大学智慧校园移动端APP“今日校园”定于11月5日正式上线运行.登陆APP可浏览学校新闻.校园生活.各部门微信公众号等内容,查看校内通知.校内 ...

  9. [zt]手把手教你写对拍程序(PASCAL)

    谁适合看这篇文章? ACMERS,OIERS或其它参加算法竞赛或需要算法的人 对操作系统并不太熟悉的人 不会写对拍的人 在网上找不到一个特别详细的对拍样例的人 不嫌弃我写的太低幼的人 前言 在NOIP ...

  10. 一个例子说明mouseover事件与mouseenter事件的区别

    <html> <head> <meta charset="UTF-8"> <title>haha</title> < ...