【POJ】【3164】Commond Network
最小树形图
最小树形图模板题,朱-刘算法。
题解:http://blog.csdn.net/shuangde800/article/details/8039359
这位大神代码写的非常通俗易懂,而且这种代码风格也很值得学习……面向对象?= =听说这样封装起来可以避免using namespace std;出现的奇葩错误
写错的地方:一开始找最小前驱边的时候 把 “!inc[i]"的叹号丢了……
//POJ 3164
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#define rep(i,n) for(int i=0;i<n;++i)
#define F(i,j,n) for(int i=j;i<=n;++i)
#define D(i,j,n) for(int i=j;i>=n;--i)
using namespace std;
void read(int &v){
v=; int sign=; char ch=getchar();
while(ch<''||ch>''){ if (ch=='-') sign=-; ch=getchar();}
while(ch>=''&&ch<=''){ v=v*+ch-''; ch=getchar();}
v*=sign;
}
/******************tamplate*********************/
const int N=;
const int INF=~0u>>;
template<typename Type>
class Directed_MST{
public:
void init(int _n){
n=_n;
ans=;
memset(vis,,sizeof vis);
memset(inc,,sizeof inc);
F(i,,n){
w[i][i]=INF;
F(j,i+,n) w[i][j]=w[j][i]=INF;
}
}
void insert(int x,int y,Type _w){
if (w[x][y]>_w) w[x][y]=_w;
}
Type directed_mst(int x){
#ifdef debug
F(i,,n){
F(j,,n)
if (w[i][j]==INF) printf(" INF ");
else printf("%.2f ",w[i][j]);
printf("\n");
}
#endif
// step1 判断能否形成最小树形图,直接dfs遍历
dfs(x);
F(i,,n) if(!vis[i]) return -; // 如果可以形成最小树形图,继续
// step2
memset(vis,,sizeof vis);
while(){
//1.找最小前驱边
F(i,,n) if (i!=x && !inc[i]){
w[i][i]=INF, pre[i]=i;
F(j,,n)
if(!inc[j] && w[j][i]<w[pre[i]][i])
pre[i]=j;
}
//2.判断是否有环
int i;
for(i=;i<=n;++i) if (i!=x && !inc[i]){
int j=i,cnt=;
while(j!=x && pre[j]!=i && cnt<=n) j=pre[j],++cnt;
if (j==x || cnt>n) continue;
break;
} //没有找到环,找到答案
if (i>n){
F(i,,n)
if (i!=x && !inc[i]) ans+=w[pre[i]][i];
return ans;
}
//有环,进行收缩
int j=i;
memset(vis,,sizeof vis);
do{
ans+=w[pre[j]][j],j=pre[j],vis[j]=inc[j]=true;
}while(j!=i);
inc[i]=false;//!!!!环缩成了点i,点i依然存在 //收缩
F(k,,n) if(vis[k])
F(j,,n) 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;
} private:
void dfs(int x){
vis[x]=;
F(i,,n) if (!vis[i] && w[x][i]<INF)
dfs(i);
}
private:
Type ans;
int n;
int pre[N];
bool vis[N],inc[N];
Type w[N][N];
};
struct node{
double x,y;
double operator - (const node&now)const{
return sqrt( (x-now.x)*(x-now.x)+(y-now.y)*(y-now.y) );
}
}a[N]; Directed_MST<double>G; int main(){
int n,m,x,y;
while(scanf("%d%d",&n,&m)!=EOF){
G.init(n);
F(i,,n) scanf("%lf%lf",&a[i].x,&a[i].y); F(i,,m){
scanf("%d%d",&x,&y);
if (x==y) continue;
G.insert(x,y,a[x]-a[y]);
}
double ans=G.directed_mst();
if (ans<) puts("poor snoopy");
else printf("%.2f\n",ans);
}
return ;
}
【POJ】【3164】Commond Network的更多相关文章
- 【 POJ - 1204 Word Puzzles】(Trie+爆搜|AC自动机)
Word Puzzles Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 10782 Accepted: 4076 Special ...
- 【POJ 2728 Desert King】
Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 27109Accepted: 7527 Description David the ...
- 【POJ 2976 Dropping tests】
Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 13849Accepted: 4851 Description In a certa ...
- 【POJ 3080 Blue Jeans】
Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 19026Accepted: 8466 Description The Genogr ...
- 【POJ各种模板汇总】(写在逆风省选前)(不断更新中)
1.POJ1258 水水的prim……不过poj上硬是没过,wikioi上的原题却过了 #include<cstring> #include<algorithm> #inclu ...
- 【POJ 3669 Meteor Shower】简单BFS
流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封 ...
- 【POJ 2823 Sliding Window】 单调队列
题目大意:给n个数,一个长度为k(k<n)的闭区间从0滑动到n,求滑动中区间的最大值序列和最小值序列. 最大值和最小值是类似的,在此以最大值为例分析. 数据结构要求:能保存最多k个元素,快速取得 ...
- 【POJ 2406 Power Strings】
Time Limit: 3000MSMemory Limit: 65536K Description Given two strings a and b we define a*b to be the ...
- 【POJ 3694】 Network(割边<桥>+LCA)
[POJ 3694] Network(割边+LCA) Network Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 7971 ...
- 【poj 1988】Cube Stacking(图论--带权并查集)
题意:有N个方块,M个操作{"C x":查询方块x上的方块数:"M x y":移动方块x所在的整个方块堆到方块y所在的整个方块堆之上}.输出相应的答案. 解法: ...
随机推荐
- js 数组的length(javascript教程四)
这是一个简单的函数,就是利用length来判断数组再遍历数组了. <script language="javascript" type="text/javascri ...
- 20141017--循环语句whlie,do
int n = 0; ; ) {//n必须小于100,如果等于100则会再进来进行一次运算,变为101. //因为下面的语句中用到了continue,状态改变n++不能用到最后 n++; ) { m ...
- android ListView的介绍和优化
xml设计 <?xml version="1.0"?> -<RelativeLayout tools:context=".MainActivity&qu ...
- 济南学习 Day 3 T3 pm
仙人掌(cactus)Time Limit:1000ms Memory Limit:64MB题目描述LYK 在冲刺清华集训(THUSC) !于是它开始研究仙人掌,它想来和你一起分享它最近研究的结果. ...
- Linux platform设备简介
Technorati 标签: Linux platform Linux在2.6内核中,针对一系列设备驱动,提供新的管理框架,成为platform机制,推出的目的,在于隔离驱动的资源和实现,使得 ...
- Speeding up AngularJS apps with simple optimizations
AngularJS is a huge framework with that already has many performance enhancements built in, but they ...
- Apache+lvs高可用+keepalive(主从+双主模型)
Apache+lvs高可用+keepalive(主从+双主模型) keepalive实验准备环境: httpd-2.2.15-39.el6.centos.x86_64 keepalived-1 ...
- Crusher Django Tutorial(5) 使用内置管理员系统
http://crusher-milling.blogspot.com/2013/09/crusher-django-tutorial5-using-admin.html 顺便学习一下FQ Crush ...
- wxPython + Boa 练习程序
最近需要做点支持linux的跨平台gui,网上查到了wxPython及Boa,感觉不错,照着Boa文档做做练习. 代码: App: #!/usr/bin/env python #Boa:App:Boa ...
- WPF 使用定时器
WPF 使用定时器:<ProgressBar Height="10" HorizontalAlignment="Left" Margin="28 ...