最小树形图

  最小树形图模板题,朱-刘算法。

  题解: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的更多相关文章

  1. 【 POJ - 1204 Word Puzzles】(Trie+爆搜|AC自动机)

    Word Puzzles Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 10782 Accepted: 4076 Special ...

  2. 【POJ 2728 Desert King】

    Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 27109Accepted: 7527 Description David the ...

  3. 【POJ 2976 Dropping tests】

    Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 13849Accepted: 4851 Description In a certa ...

  4. 【POJ 3080 Blue Jeans】

    Time Limit: 1000MSMemory Limit: 65536K Total Submissions: 19026Accepted: 8466 Description The Genogr ...

  5. 【POJ各种模板汇总】(写在逆风省选前)(不断更新中)

    1.POJ1258 水水的prim……不过poj上硬是没过,wikioi上的原题却过了 #include<cstring> #include<algorithm> #inclu ...

  6. 【POJ 3669 Meteor Shower】简单BFS

    流星雨撞击地球(平面直角坐标第一象限),问到达安全地带的最少时间. 对于每颗流星雨i,在ti时刻撞击(xi,yi)点,同时导致(xi,yi)和上下左右相邻的点在ti以后的时刻(包括t)不能再经过(被封 ...

  7. 【POJ 2823 Sliding Window】 单调队列

    题目大意:给n个数,一个长度为k(k<n)的闭区间从0滑动到n,求滑动中区间的最大值序列和最小值序列. 最大值和最小值是类似的,在此以最大值为例分析. 数据结构要求:能保存最多k个元素,快速取得 ...

  8. 【POJ 2406 Power Strings】

    Time Limit: 3000MSMemory Limit: 65536K Description Given two strings a and b we define a*b to be the ...

  9. 【POJ 3694】 Network(割边&lt;桥&gt;+LCA)

    [POJ 3694] Network(割边+LCA) Network Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 7971 ...

  10. 【poj 1988】Cube Stacking(图论--带权并查集)

    题意:有N个方块,M个操作{"C x":查询方块x上的方块数:"M x y":移动方块x所在的整个方块堆到方块y所在的整个方块堆之上}.输出相应的答案. 解法: ...

随机推荐

  1. iOS在照片上添加水印

    在做项目的时候我们需要将拍摄的照片做上标记防止图片被他人盗用,所以这就需要在照片的上面加上水印,以表示此照片的独一无二. 加水印不是要在上面添加上几个Label,而是我们要把字画到图片上成为一个整体. ...

  2. MySQL 5.7 Zip 安装(win7)

    参考官方文档 http://dev.mysql.com/doc/refman/5.7/en/windows-install-archive.html 2.3.5.1 Extracting the In ...

  3. 1 . Robberies (hdu 2955)

    The aspiring Roy the Robber has seen a lot of American movies, and knows that the bad guys usually g ...

  4. Java通过代理类实现数据库DAO操作

    下面的所有代码示例都取自李兴华的<Java Web开发实战经典>的随书源码,因为觉得设计得很好,所以将代码摘录下来作成笔记. 首先,我们在一个java文件中定义要存储的结构类型: impo ...

  5. 驾照理论模拟考试系统Android源码下载

    ‍‍‍驾照理论模拟考试系统Android源码下载 <ignore_js_op> 9.png (55.77 KB, 下载次数: 0) <ignore_js_op> 10.png ...

  6. WP开发笔记——页面传参

    WP APP页面与页面之间参数的传递可以通过程序的App类设置全局变量. 由于App 类继承自Application类,而通过Application的Current属性可以获取到与当前程序关联的App ...

  7. 下拉刷新--第三方开源--PullToRefresh

    效果预览图: 下载地址:https://github.com/chrisbanes/Android-PullToRefresh activity_main.xml: <RelativeLayou ...

  8. Windows C盘格式化或者同平台迁移oracle数据库

    我们知道如果是Linux 同平台迁移oracle数据库.只要是安全关闭了数据库,在新机器上创建用户组,配置了环境变量,将数据库安装目录拷贝到对应的目录就好用了. 一直在寻求Windows平台上这类的解 ...

  9. C#学习笔记一

    c#学习笔记一 c#学习笔记一    1 1.    注释    3 1.1.    ///是文档注释,用于类和方法的说明    3 1.2.    #region #endregion可以折叠代码  ...

  10. 1101. Quick Sort (25)

    There is a classical process named partition in the famous quick sort algorithm. In this process we ...