poj3164-Command Network
给出平面上一些点,和连接它们的带权有向边,求把所有点连起来的最小总权值。
分析
由于这里边是有向的(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的更多相关文章
- POJ3164 Command Network —— 最小树形图
题目链接:https://vjudge.net/problem/POJ-3164 Command Network Time Limit: 1000MS Memory Limit: 131072K ...
- POJ3164 Command Network(最小树形图)
图论填个小坑.以前就一直在想,无向图有最小生成树,那么有向图是不是也有最小生成树呢,想不到还真的有,叫做最小树形图,网上的介绍有很多,感觉下面这个博客介绍的靠谱点: http://www.cnblog ...
- POJ3164:Command Network(有向图的最小生成树)
Command Network Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 20766 Accepted: 5920 ...
- Command Network
Command Network Time Limit: 1000MSMemory Limit: 131072K Total Submissions: 11970Accepted: 3482 Descr ...
- POJ 3164 Command Network 最小树形图
题目链接: 题目 Command Network Time Limit: 1000MS Memory Limit: 131072K 问题描述 After a long lasting war on w ...
- POJ3436 Command Network [最小树形图]
POJ3436 Command Network 最小树形图裸题 傻逼poj回我青春 wa wa wa 的原因竟然是需要%.2f而不是.2lf 我还有英语作业音乐作业写不完了啊啊啊啊啊啊啊啊啊 #inc ...
- POJ 3164 Command Network ( 最小树形图 朱刘算法)
题目链接 Description After a long lasting war on words, a war on arms finally breaks out between littlek ...
- Command Network OpenJ_Bailian - 3436(最小有向生成树模板题)
链接: http://poj.org/problem?id=3164 题目: Command Network Time Limit: 1000MS Memory Limit: 131072K To ...
- poj 3164 Command Network(最小树形图模板)
Command Network http://poj.org/problem?id=3164 Time Limit: 1000MS Memory Limit: 131072K Total Subm ...
- POJ 3164——Command Network——————【最小树形图、固定根】
Command Network Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 15080 Accepted: 4331 ...
随机推荐
- xshell5连接虚拟机的小问题处理
1.首先确保虚拟机是桥接状态,然后在虚拟机下用ifconfig查看ip地址(当然是默认你虚拟机下是linux) 2.确保虚拟机安装了ssh...安装openssh-server: 对应的sudo ap ...
- browser-cookies 一个管理cookies的插件,好用
一.browser-cookies 地址:https://github.com/voltace/browser-cookies 用法 存放cookies是 cookies.set('firstName ...
- PIE currently adds full or partial support to IE 6 through 8 for the following CSS3 features
PIE stands for Progressive Internet Explorer. It is an IE attached behavior which, when applied to a ...
- Java设计模式(7)——结构型模式之适配器模式(Adapter)
一.概述 概念 其实,举个生活中的例子的话,适配器模式可以类比转接头,比如typeC和USB的转接头,把原本只能接typeC的接口,拓展为可以接普通USB:这里的转接头一方面需要查在typeC上,一方 ...
- Android零碎知识点
1.android:foreground="?attr/selectableItemBackground" ###设置水波纹效果 2.android:contentDescri ...
- Android UI控件:TextView
TextVIew的属性详解 android:autoLink设置是否当文本为URL链接/email/电话号码/map时,文本显示为可点击的链接.可选值(none/web /email/phone/ma ...
- ROS(二)Service通信
使用自定义的消息类型,实现service方式的节点间双向通信 在package目录下创建msg和srv目录,存放package需要使用的.msg和.srv文件. 在ROS中,message被设计为一种 ...
- jQuery wordexport导出 word
同事给我说了简单的导出word的插件,亲测了下,做个随笔. 这个导出插件是jQuery自带的的插件,通过调用wordexport.js来实现导出功能. 1.引入的js <script type= ...
- http tcp udp
HTTP连接 1.HTTP协议即超文本传送协议(Hypertext Transfer Protocol ),是Web联网的基础,也是手机联网常用的协议之一,HTTP协议是建立在TCP协议之上的一种应用 ...
- 「日常训练」Kefa and Park(Codeforces Round #321 Div. 2 C)
题意与分析(CodeForces 580C) 给你一棵树,然后每个叶子节点会有一家餐馆:你讨厌猫(waht?怎么会有人讨厌猫),就不会走有连续超过m个节点有猫的路.然后问你最多去几家饭店. 这题我写的 ...