给出平面上一些点,和连接它们的带权有向边,求把所有点连起来的最小总权值。

分析

由于这里边是有向的(unidirectional),所以这是经典的最小树形图问题,可以说是最小树形图的模板题。

代码

这个写法是我乱想的。最近写图啊树啊都喜欢开一个结构体~

这个代码在poj上交,如果用g++的话会WA,因为奇怪的poj需要用%f输出浮点数。更奇怪的是c++直接编译错误,不想说了。

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int maxn=202;
const double inf=1e100;
bool bian[maxn][maxn];
struct node {
double x,y;
} a[maxn];
double dist(node &a,node &b) {
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
struct edge {
int v;
double w;
int nxt;
};
struct graph {
edge e[maxn*maxn];
int h[maxn],tot,n,from[maxn],tic[maxn],tim,root,sta[maxn],top;
double in[maxn],mi[maxn];
bool vis[maxn],able[maxn];
void clear() {
memset(h,0,sizeof h);
tot=0;
}
void add(int u,int v,double w) {
e[++tot]=(edge){v,w,h[u]};
h[u]=tot;
}
void dfs(int x) {
vis[x]=true;
for (int i=h[x],v=e[i].v;i;i=e[i].nxt,v=e[i].v) if (able[v]) {
if (v!=root && in[v]>e[i].w) in[v]=e[i].w,from[v]=x;
if (!vis[v]) dfs(v);
}
}
bool circle() {
memset(tic,0,sizeof tic),tim=0;
for (int i=1;i<=n;++i) if (able[i]) {
++tim;
top=0;
for (int j=i;j;j=from[j]) if (!tic[j]) tic[j]=tim,sta[++top]=j; else if (tic[j]==tim) {
for (int k=1;k<=n;++k) if (tic[k]==tim) tic[k]=0;
do {
tic[sta[top--]]=tim;
} while (sta[top+1]!=j);
return true;
}
}
return false;
}
bool work(double &ret) {
memset(vis,0,sizeof vis);
fill(in+1,in+n+1,inf);
dfs(root);
for (int i=1;i<=n;++i) if (able[i] && !vis[i]) {
ret=-1;
return false;
}
bool cc=circle();
if (cc) {
for (int i=1;i<=n;++i) if (able[i] && i!=root && tic[i]==tim) ret+=in[i];
} else {
for (int i=1;i<=n;++i) if (able[i] && i!=root) ret+=in[i];
}
return cc;
}
void reduce() {
fill(mi+1,mi+n+1,inf);
for (int i=1;i<=n;++i) if (able[i] && tic[i]!=tim) {
double w=inf;
for (int j=h[i],v=e[j].v;j;j=e[j].nxt,v=e[j].v) if (tic[v]==tim) w=min(w,e[j].w-in[v]);
if (w!=inf) add(i,n+1,w);
}
for (int i=1;i<=n;++i) if (able[i] && tic[i]==tim) for (int j=h[i],v=e[j].v;j;j=e[j].nxt,v=e[j].v) if (able[v] && tic[v]!=tim) mi[v]=min(mi[v],e[j].w);
for (int i=1;i<=n;++i) if (able[i] && tic[i]!=tim && mi[i]!=inf) add(n+1,i,mi[i]);
for (int i=1;i<=n;++i) if (tic[i]==tim) able[i]=false;
++n;
if (!able[root]) root=n;
}
double MGT(int _n) {
memset(able,true,sizeof able),n=_n,root=1;
double ret=0;
while (work(ret)) {
reduce();
}
return ret;
}
} A;
int main() {
#ifndef ONLINE_JUDGE
freopen("test.in","r",stdin);
freopen("my.out","w",stdout);
#endif
int n,m;
while (~scanf("%d%d",&n,&m)) {
A.clear();
memset(bian,0,sizeof bian);
for (int i=1;i<=n;++i) scanf("%lf%lf",&a[i].x,&a[i].y);
for (int i=1;i<=m;++i) {
int u,v;
scanf("%d%d",&u,&v);
if (u==v || bian[u][v]) continue;
bian[u][v]=true;
double d=dist(a[u],a[v]);
A.add(u,v,d);
}
double ans=A.MGT(n);
if (ans<0) puts("poor snoopy"); else printf("%.2lf\n",ans);
}
return 0;
}

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. POJ3164:Command Network(有向图的最小生成树)

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

  4. Command Network

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

  5. POJ 3164 Command Network 最小树形图

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

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

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

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

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

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

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

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

随机推荐

  1. 《C++ Primer》第II部分:C++标准库

    <C++ Primer>第II部分:C++标准库 前言 把<C++ Primer>读薄系列笔记.本篇为第II部分C++标准库,包含全书第8-12章重难点: IO库 顺序容器 范 ...

  2. Mac下node.js安装与卸载

    安装: 访问 http://nodejs.org/ 进入官网,下载 Mac 版本的 node.js,双击打开安装即可. 通过终端输入命令 node -v 验证 node 是否安装正确:npm -v 验 ...

  3. springBoot 自定义redisTemplate

    package com.atirm.mybatismutiplesource.config.RedisConfig; import com.atirm.mybatismutiplesource.ent ...

  4. WPF Style Setter use a TemplateBinding?

    <Style TargetType="{x:Type local:ImageButton}"> <Setter Property="Horizontal ...

  5. Ruby基础教程 1-10

    类结构 1.数值类结构     Fixnum到Bignum会自动转换   2.常用数值表示   3. ans=10.divmod(3) ans[0]是商  ans[1]是余数   4.实例方法roun ...

  6. 通过批处理命令for提取数据

    前两天有这么个小需求: 在cmd中运行某测试工具后,会返回一个json结果,其中有一个参数的值每次都变且经常要用,正常情况复制粘贴就好了,但这个值非常长,配上cmd的标记+粘贴的行为,就很酸爽了.然后 ...

  7. JDK11安装后,环境变量的坑

    安装了最新的JDK11,安装完后设置环境变量,打开CMD,没生效 检查了3遍,都没发现问题,在PATH中将JAVA设置移到第一也不行 最后偶然发现,在点击如图右下的‘编辑文本’,用文本方式编辑时,发现 ...

  8. Objective-C Block数据类型 @protocol关键字

    Block数据类型 Block封装了一段代码 可以在任何时候执行 Block可以作为函数参数或者函数的返回值 而其本身又可以带输入参数或返回值 苹果官方建议尽量多用Block 在多线程 异步任务 集合 ...

  9. Python数学运算入门把Python当作计算器

    让我们尝试一些简单的 Python 命令.启动解释器,等待界面中的提示符,>>> (这应该花不了多少时间). 3.1.1. 数字 解释器就像一个简单的计算器一样:你可以在里面输入一个 ...

  10. 2018牛客多校第二场a题

    一个人可以走一步或者跳x步,但不能连着跳,问到这个区间里有几种走法 考虑两种状态  对于这一点,我可以走过来,前面是怎么样的我不用管,也可以跳过来但是,跳过来必须保证前一步是走的 dp[i][0]表示 ...