链接:

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. 使用salt-ssh初始化系统安装salt-minion

    salt-ssh介绍及使用方法 在ssh上执行salt命令和状态而不安装salt-minion,类似于ansible. 1. salt-ssh的安装: [root@linux-node1 ~]# yu ...

  2. Mysql:存储过程游标不进循环的原因详解

    ------------吾亦无他,唯手熟尔,谦卑若愚,好学若饥------------- 本篇博客给刚接触存储过程的朋友做个引导作用,目的是解决游标不走循环 很多人发现他的游标,无论是嵌套循环还是单层 ...

  3. Html+CSS 学习第二天

    趁着这两天,将html和CSS基本上学了一遍,大家如果想学习的话,可以百度w3cSchool,进行学习. 基础我就不说了,直接将我做的一个登陆页面放上去.刚学完CSS,写个漂亮的登录界面恶心死我了,感 ...

  4. Git生成SSH密钥

    git config --global user.name "yangjianliang"配置用户名 git config --global user.email "52 ...

  5. JUC——原子类操作(三)

    原子类操作 既然强调了并发访问,那么就必须考虑操作系统位数:32位操作系统还是64位操作系统,对于long型数据类型而言,是64位的.但是如果现在项目运行在32位系统上,则long型数据会占用32位空 ...

  6. Vue学习计划基础笔记(一) - vue实例

    最近又重新看vue的文档了,计划是别人写的,之前看过一次,没有考虑太多,只考虑看懂能用就好.看完之后写过写demo,现在是零实际项目经验的,所以这一次打算细看,算是官方文档的二次产物吧,但是不是全部直 ...

  7. C/C++语言基础

    1. 一个子类中含有其他类对象,则构造函数的顺序是? 先执行基类的(如果基类当中有虚基类,要先执行虚基类的,其他基类则按照声明派生类是的顺序依次执行),在执行成员对象的,最后执行自己的. 2.spri ...

  8. windows python MySQL-python安装过程

    问题表述: pip install MySQL-python==1.2.5 出现如下报错: C:\Users\Administrator\AppData\Local\Programs\Common\M ...

  9. 笔试题:C++打印队列

    题目:打印队列 题目介绍:现在用打印机打印队列,已知打印任务有9个优先级(1-9),现在给出一系列任务,求输出打印顺序(任务下标,从0开始). 例: 输入:9,3,5,4,7,1 输出:0,4,2,3 ...

  10. DX孟虎点评新兴市场:巴西俄罗斯火爆背后

    [亿邦动力网讯]4月3日消息,在第九届中国中小企业电子商务大会暨2014中国(河南)跨境贸易电子商务峰会上,DX公司CEO孟虎对新兴市场做了详细的分析,指出在当今的跨境电商环境下,北美.西欧作为电商成 ...