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

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

 
 
题目大意:问你起点在1,存不存在最小有向生成树,如果存在输出结果,如果不存在,输出poor Snoopy。
 
解题思路:固定根的最小树形图。直接用朱刘算法套上就能过了。    注意点:1.如果是不定根,可以自己加一个起点,然后让起点跟其他点连边。2.不能记录路径。
 
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
const int maxn = 1100;
const int INF = 0x3f3f3f3f;
struct Coor{
double x,y;
}coors[maxn];
struct Edge{
int from,to;
double dist;
}edges[maxn*maxn];
int pre[maxn],vis[maxn],ID[maxn];
double In[maxn];
double distan(Coor a,Coor b){
double dx = a.x - b.x;
double dy = a.y - b.y;
return sqrt( dx * dx + dy * dy);
}
double Zhuliu(int root,int n,int m){
double ret = 0;
int u,v;
while(true){
for(int i = 0; i < n; i++){
In[i] = 1.0*INF;
}
for(int i = 0; i < m; i++){
Edge &e = edges[i];
u = e.from; v = e.to;
if(In[v] > e.dist && u != v){
pre[v] = u;
In[v] = e.dist;
}
}
for(int i = 0; i < n; i++){
if(i == root) continue;
if(In[i] == INF)
return -1;
}
In[root] = 0;
int cntcir = 0;
memset(vis,-1,sizeof(vis));
memset(ID,-1,sizeof(ID));
for(int i = 0; i < n; i++){
ret += In[i];
v = i;
while(vis[v]!= i && ID[v] ==-1 &&v != root){
vis[v] = i;
v = pre[v];
}
if(v != root && ID[v] == -1){
for(u = pre[v]; u != v; u = pre[u]){
ID[u] = cntcir;
}
ID[v] = cntcir++;
}
}
if(cntcir == 0){
break;
}
for(int i = 0; i < n; i++){
if(ID[i]==-1){
ID[i] = cntcir++;
}
}
for(int i = 0; i < m; i++){
v = edges[i].to;
Edge & e = edges[i];
e.from = ID[e.from];
e.to = ID[e.to];
if(e.from != e.to){
e.dist -= In[v];
}
}
n = cntcir;
root = ID[root];
}
return ret;
}
int main(){
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
for(int i = 0; i < n; i++){
scanf("%lf%lf",&coors[i].x,&coors[i].y);
}
int a,b;
for(int i = 0; i < m; i++){
scanf("%d%d",&a,&b);
a--,b--;
edges[i].from = a;
edges[i].to = b;
if(a == b){
edges[i].dist = 1.0*INF;
continue;
}
edges[i].dist = distan(coors[a],coors[b]);
}
double res = Zhuliu(0,n,m);
if(res == -1){
puts("poor snoopy");
}else{
printf("%.2f\n",res);
}
}
return 0;
}

  

POJ 3164——Command Network——————【最小树形图、固定根】的更多相关文章

  1. POJ 3164 Command Network 最小树形图

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

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

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

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

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

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

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

  5. poj 3164 Command Network

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

  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. Failed to export using the options you specified. Please check your options and try again

    参考这篇<从ASP.NET传递参数给水晶报表> http://www.cnblogs.com/insus/p/3281114.html  是可以传递参了.但是点击报表的菜单条上的打印图标没 ...

  2. git rm 与 git reset

    https://www.cnblogs.com/sunshine-xin/articles/3521481.html 1. git rm --cached file will remove the f ...

  3. 学习高性能mysql

    Mysql 的InnoDB存储引擎实现的不是简单的行级锁,实现的是MVCC,多版本并发控制,可以理解成行级锁的一个变种. InnoDB的MVCC是通过在每行纪录后面保存两个隐藏的列来实现的.这两个列, ...

  4. nagios安装使用指南

    话不多说,下面开始,nagios具体的介绍,可以搜一下,这篇文章为作者在实际操作中整理出来,写出来的都是负责人的内容~ 环境准备 此文档共用2台服务器的配置,操作系统均为centOS6.7,安装用户都 ...

  5. 老男孩Day3作业:工资管理系统

    作业需求: 1.从info.txt文件中读取员工及其工资信息,最后将修改或增加的员工工资信息也写入原info.txt文件. 2.能增查改员工工资 3.增.改员工工资用空格分隔 4.实现退出功能 1)编 ...

  6. 10.20 olinr

    感谢olinr提供md文件 免得我整理格式了 1.求助 (help.cpp/c/pas) [问题背景] 马上就要noip了,lrt同志\(\displaystyle\begin{vmatrix}\te ...

  7. CF959E Mahmoud and Ehab and the xor-MST 思维

    Ehab is interested in the bitwise-xor operation and the special graphs. Mahmoud gave him a problem t ...

  8. PHP删除目录下包含某个字符串的全部文件

    //获取全部的路径 function tree(&$arr_file, $directory, $dir_name='') { $mydir = dir($directory);    whi ...

  9. spring boot 下websocket实现的两种方法

    websocket前台实现代码,保存为html执行就好 html代码来自:https://blog.csdn.net/M348915654/article/details/53616837 <h ...

  10. JEECG入门

    姓名:陈中娇    班级:软件151 1.准备: 下载Jdk1.6+.myeclipse.tomcat6.0.MySQL数据库.jeecg-framework压缩包 2.安装:①.安装jdk,配置好环 ...