链接:

http://poj.org/problem?id=3164

题目:

Command Network
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 8922   Accepted: 2609

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

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

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = , INF = 0x7fffffff;
int n, m;
int vis[maxn], inc[maxn], pre[maxn];
double w[][]; struct edge
{
int x, y;
}Edge[maxn]; void dfs(int u)
{
vis[u] = ;
for(int i=; i<=n; i++)
if(!vis[i] && w[u][i] < INF)
dfs(i);
} double dirmst(int u)
{
double ans = ;
//== 步骤1: 判断能否形成最小树形图,直接dfs遍历 (就是检验一下图是否能够联通)
dfs(u);
for(int i=; i<=n; i++)
if(!vis[i])
return -;
//== 如果可以形成最小树形图,继续
mem(vis, );
while(true)
{
//== 1. 找最小前驱边
for(int i=; i<=n; i++) if(i != u && !inc[i]){
w[i][i] = INF; pre[i] = i;
for(int j=; j<=n; j++) if(!inc[j] && w[j][i] < w[pre[i]][i])
pre[i] = j;
}
//== 2.判断是否有环
int i;
for(i=; i<=n; i++) if(i != u && !inc[i]){
int j = i, cnt = ;
while(j != u && pre[j] != i && cnt <= n) j = pre[j], ++cnt;
if(j == u || cnt > n) continue;
break;
}
//== 没有找到环,得到答案
if(i > n)
{
for(int i=; i<=n; i++) if(i != u && !inc[i]) ans += w[pre[i]][i];
return ans;
}
//== 有环,则对这个环进行收缩
int j = i;
mem(vis, );
do{
ans += w[pre[j]][j], j = pre[j], vis[j] = inc[j] = true;
}while(j != i);
inc[i] = false; // 环缩成了点i,点i仍然存在 for(int k=; k<=n; k++) if(vis[k]){ //在环中的点
for(int j=; j<=n; j++) if(!vis[j]){ //不在环中的点
if(w[i][j] > w[k][j]) w[i][j] = w[k][j]; //更新环的出边
if(w[j][k] < INF && w[j][k] - w[pre[k]][k] < w[j][i]) //更新换的入边
w[j][i] = w[j][k] - w[pre[k]][k];
}
}
}
return ans;
} void init()
{
mem(vis, );
mem(inc, );
rap(i, , n)
rap(j, i, n)
w[i][j] = w[j][i] = INF;
} int main()
{
while(~scanf("%d%d", &n, &m))
{
init();
rap(i, , n)
{
scanf("%d%d", &Edge[i].x, &Edge[i].y);
}
rap(i, , m)
{
int a, b;
scanf("%d%d", &a, &b);
double c = sqrt((double)(Edge[a].x - Edge[b].x)*(Edge[a].x - Edge[b].x) + (double)(Edge[a].y - Edge[b].y)*(Edge[a].y - Edge[b].y));
if(w[a][b] > c)
w[a][b] = c;
}
double ans = dirmst();
if(ans < ) puts("poor snoopy");
else printf("%.2f\n", ans); } return ;
}

Command Network OpenJ_Bailian - 3436(最小有向生成树模板题)的更多相关文章

  1. 【网络流#2】hdu 1533 - 最小费用最大流模板题

    最小费用最大流,即MCMF(Minimum Cost Maximum Flow)问题 嗯~第一次写费用流题... 这道就是费用流的模板题,找不到更裸的题了 建图:每个m(Man)作为源点,每个H(Ho ...

  2. POJ2135 最小费用最大流模板题

    练练最小费用最大流 此外此题也是一经典图论题 题意:找出两条从s到t的不同的路径,距离最短. 要注意:这里是无向边,要变成两条有向边 #include <cstdio> #include ...

  3. 2018牛客网暑期ACM多校训练营(第五场) E - room - [最小费用最大流模板题]

    题目链接:https://www.nowcoder.com/acm/contest/143/E 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...

  4. Treasure Exploration POJ - 2594 【有向图路径可相交的最小路径覆盖】模板题

    Have you ever read any book about treasure exploration? Have you ever see any film about treasure ex ...

  5. POJ 1459 Power Network(网络最大流,dinic算法模板题)

    题意:给出n,np,nc,m,n为节点数,np为发电站数,nc为用电厂数,m为边的个数.      接下来给出m个数据(u,v)z,表示w(u,v)允许传输的最大电力为z:np个数据(u)z,表示发电 ...

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

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

  7. POJ 3164 Command Network 最小树形图

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

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

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

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

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

随机推荐

  1. day 2 异常传递 ,抛出

    1.异常的传递 def test1(): print("---test1--") print(num) print('---test1 over---') def test2(): ...

  2. Zabbix学习之路(十)之分布式监控zabbix_proxy及交换机监控

    1.Zabbix分布式监控 zabbix proxy 可以代替 zabbix server 检索客户端的数据,然后把数据汇报给 zabbix server,并且在一定程度上分担了zabbix serv ...

  3. MySQL入门篇(五)之高可用架构MHA

    一.MHA原理 1.简介: MHA(Master High Availability)目前在MySQL高可用方面是一个相对成熟的解决方案,它由日本DeNA公司youshimaton(现就职于Faceb ...

  4. 【mysql经典题目】行转列

    参考:http://www.cnblogs.com/h07061108/p/mysql_questions.html#3806338 实现如下效果 CREATE TABLE IF NOT EXISTS ...

  5. 【索引】MySQL索引

    一.索引的定义及作用 1. 二.索引的创建及删除 1.1查看表的索引 show index from tblname; 1.2.创建索引 1.22创建普通索引 ALTER TABLE `table_n ...

  6. Spring学习(九)-----Spring bean配置继承

    在 Spring,继承是用为支持bean设置一个 bean 来分享共同的值,属性或配置. 一个子 bean 或继承的bean可以继承其父 bean 的配置,属性和一些属性.另外,子 Bean 允许覆盖 ...

  7. Maven学习(七)-----Maven添加远程仓库

    Maven添加远程仓库 默认情况下,Maven从Maven中央仓库下载所有依赖关系.但是,有些库丢失在中央存储库,只有在Java.net或JBoss的储存库远程仓库中能找到. 1. Java.net资 ...

  8. 【洛谷】题解 P1056 【排座椅】

    题目链接 因为题目说输入保证会交头接耳的同学前后相邻或者左右相邻,所以一对同学要分开有且只有一条唯一的通道才能把他们分开. 于是可以吧这条通道累加到一个数组里面.应为题目要求纵列的通道和横列的通道条数 ...

  9. Kubernetes学习-相关概念

    Kubernetes架构图 上图可以看到如下组件,使用特别的图标表示Service和Label: Pod Container(容器) Label()(标签) Replication Controlle ...

  10. US Customs bond DDP 船运

    客户提供目的港门点地址,提供美国进口产品的关税税率基本上就可以了关于ISF信息到时候你发给老外让老外填填好就可以了BAND 货值*0.575%POA  货值*0.335%这二个费用如果国内付就付了,国 ...