最小树形图(poj3164)
Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 12834 | Accepted: 3718 |
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 jbetween
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
程序:
#include"string.h"
#include"stdio.h"
#include"math.h"
#include"queue"
#define eps 1e-10
#define M 109
#define inf 100000000
using namespace std;
struct node
{
double x,y;
}p[M];
struct edge
{
int u,v;
double w;
}edge[M*M];
int pre[M],id[M],use[M];
double in[M];
double pow(double x)
{
return x*x;
}
double Len(node a,node b)
{
return sqrt(pow(a.x-b.x)+pow(a.y-b.y));
}
double mini_tree(int root,int n,int m)
{
double ans=0;
int i,u;
while(1)
{
for(i=1;i<=n;i++)
in[i]=inf;
for(i=1;i<=m;i++)
{
int u=edge[i].u;
int v=edge[i].v;
if(edge[i].w<in[v]&&u!=v)
{
in[v]=edge[i].w;
pre[v]=u;
}
}
for(i=1;i<=n;i++)
{
if(i==root)continue;
ans+=in[i];
if(fabs(in[i]-inf)<eps)
return -1;
}
memset(id,-1,sizeof(id));
memset(use,-1,sizeof(use));
int cnt=0;
for(i=1;i<=n;i++)
{
int v=i;
while(v!=root&&use[v]!=i&&id[v]==-1)
{
use[v]=i;
v=pre[v];
}
if(v!=root&&id[v]==-1)
{
++cnt;
id[v]=cnt;
for(u=pre[v];u!=v;u=pre[u])
id[u]=cnt;
}
}
if(cnt==0)
break;
for(i=1;i<=n;i++)
if(id[i]==-1)
id[i]=++cnt;
for(i=1;i<=m;i++)
{
int u=edge[i].u;
int v=edge[i].v;
edge[i].u=id[u];
edge[i].v=id[v];
if(edge[i].u!=edge[i].v)
edge[i].w-=in[v];
}
n=cnt;
root=id[root];
}
return ans;
}
int main()
{
int n,m,i;
while(scanf("%d%d",&n,&m)!=-1)
{
for(i=1;i<=n;i++)
scanf("%lf%lf",&p[i].x,&p[i].y);
for(i=1;i<=m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
double L=Len(p[a],p[b]);
edge[i].u=a;
edge[i].v=b;
edge[i].w=L;
}
double ans=mini_tree(1,n,m);
if(ans<0)
printf("poor snoopy\n");
else
printf("%.2lf\n",ans);
}
return 0;
}
最小树形图(poj3164)的更多相关文章
- poj3164 (朱刘算法 最小树形图)
题目大意:给定n个点坐标,m条有向边,要求最小树形图. 题解:直接上模板,前面打的 vis[v]=i一直把i打成1,一直TLE. #include<iostream> #include&l ...
- POJ3164 Command Network —— 最小树形图
题目链接:https://vjudge.net/problem/POJ-3164 Command Network Time Limit: 1000MS Memory Limit: 131072K ...
- POJ3164 Command Network(最小树形图)
图论填个小坑.以前就一直在想,无向图有最小生成树,那么有向图是不是也有最小生成树呢,想不到还真的有,叫做最小树形图,网上的介绍有很多,感觉下面这个博客介绍的靠谱点: http://www.cnblog ...
- poj3164 最小树形图板子题
/* 思路很简单,也不知道哪里错了TAT */ /* N个点通过笛卡尔坐标表示 根节点是1,求最小树形图 */ #include<iostream> #include<cstdio& ...
- poj3164最小树形图模板题
题目大意:给定一个有向图,根节点已知,求该有向图的最小树形图.最小树形图即有向图的最小生成树,定义为:选择一些边,使得根节点能够到达图中所有的节点,并使得选出的边的边权和最小. 题目算法:朱-刘算法( ...
- poj3164(最小树形图&朱刘算法模板)
题目链接:http://poj.org/problem?id=3164 题意:第一行为n, m,接下来n行为n个点的二维坐标, 再接下来m行每行输入两个数u, v,表点u到点v是单向可达的,求这个有向 ...
- POJ - 3164-Command Network 最小树形图——朱刘算法
POJ - 3164 题意: 一个有向图,存在从某个点为根的,可以到达所有点的一个最小生成树,则它就是最小树形图. 题目就是求这个最小的树形图. 参考资料:https://blog.csdn.net/ ...
- bzoj4349: 最小树形图
最小树形图模板题…… 这种\(O(nm)\)的东西真的能考到么…… #include <bits/stdc++.h> #define N 60 #define INF 1000000000 ...
- hdu 4966 GGS-DDU (最小树形图)
比较好的讲解:http://blog.csdn.net/wsniyufang/article/details/6747392 view code//首先为除根之外的每个点选定一条入边,这条入边一定要是 ...
随机推荐
- jquery -- jquery控制只能输入数字和小数点
控制文本框只能输入数字是一个很常见的需求,比如电话号码的输入.数量的输入等,这时候就需要我们控制文本框只能输入数字.在用js控制之后在英文输入法的状态下去敲击键盘上的非数字键是输不进去的,然而当你转到 ...
- MJRefresh原理分析
MJRefresh是流行的下拉刷新控件.前段时间为了修复一个BUG.读了它的源代码.本文总结一下实现的原理 下拉刷新的基本原理 大部分的下拉刷新控件.都是用contentInset实现的.默认情况下. ...
- cVim——Chrome上更强大的vim插件
ref: http://www.cnblogs.com/voidsky/p/5490787.html 介绍 也许很多人在chrome上都用过类似Vimium, ViChrome的插件,这些插件的目的都 ...
- CentOS和Ubuntu安装软件命令对比(区别)
此表内容来自<Ubuntu Server最佳方案>,CentOS和Ubuntu(Debian)是VPS最常见的系统,这份表很实用,分享下
- hadoop本地测试命令
http://www.cnblogs.com/shishanyuan/p/4190403.html if have assign the /etc/profile: hadoop jar /usr/l ...
- 使用MFC WinInet进行FTP中文件的简单上传和下载功能
建立基于对话框的MFC应用程序CMfcFtpWinInetDlg: 1.首先Dlg类中包含头文件 #include "afxinet.h" 2.添加成员变量: C++ Code ...
- SVN目录权限设置
---恢复内容开始--- 如图,这里我建的项目库为myRepositories,其下边又有许多文件,现在要分别对每个文件进行svn权限配置. 配置 进入上面生成的文件夹conf下,进行配置.有以下几个 ...
- fildder教程
转载地址:写得很不错的fildder教程 http://kb.cnblogs.com/page/130367/ Fiddler的基本介绍 Fiddler的官方网站: www.fiddler2.c ...
- Oracle会话及连接数优化
一.改动Oracle会话及最大连接数 1.查看最大连接数 SQL> show parameter processes; NAME ...
- ZooKeeper(六)-- CAP和BASE理论、ZAB协议
一.CAP理论和BASE理论 1.CAP理论 CAP理论,指的是在一个分布式系统中,不可能同时满足Consistency(一致性). Availability(可用性).Partition toler ...