描述


http://www.lydsy.com/JudgeOnline/problem.php?id=1626

给出\(n\)个点的坐标,其中一些点已经连通,现在要把所有点连通,求修路的最小长度.

分析


已经连好一些边的最小生成树问题.

这里顺带复习了一下Prim和Krusakal.

Prim的证明:

设当前已经连好的树为\(T\),当前最小的边为\(e\),我们来证明\(e\)一定在最小生成树\(G\)中.

假设\(e\)不在\(G\)中,则连通\(G-T\)和\(T\)的边\(e'\)一定比\(e\)大(或相等).此时我们在\(G\)中加入\(e\),会形成环,去掉环中的\(e'\),树依然连通,而花费更小了,这与\(G\)是最小生成树矛盾.(如果\(e\)与\(e'\)相等那么虽然花费不会更小,也就是说\(e\)可以不再\(G\)中,但是我们也可以用\(e\)替换\(e'\),换言之,\(e\)在\(G\)中是不错误的.)

所以\(e\)一定在最小生成树\(G\)中.

Kruskal的证明:

设当前连接两个不连通分量的最小的边为\(e\),我们来证明\(e\)一定在最小生成树\(G\)中.

假设\(e\)不再\(G\)中,则连通这两个分量的边\(e'\)一定比\(e\)大(或相等).此时我们 在\(G\)中加入\(e\),会形成环,去掉环中的\(e'\),树依然连通,而花费更小了,这与\(G\)是最小生成树矛盾.(如果\(e\)与 \(e'\)相等那么虽然花费不会更小,也就是说\(e\)可以不再\(G\)中,但是我们也可以用\(e\)替换\(e'\),换言之,\(e\)在 \(G\)中是不错误的.)

 #include <bits/stdc++.h>
using namespace std; const int maxn=+;
struct pt{
double x,y;
pt(double x=,double y=):x(x),y(y){}
}p[maxn];
struct edge{
int from,to;
double d;
edge(){}
edge(int from,int to,double d):from(from),to(to),d(d){}
bool operator < (const edge &rhs) const { return d<rhs.d; }
}g[maxn*maxn];
int n,m,cnt=;
int f[maxn];
double ans;
inline double dis(pt a,pt b){ return sqrt(pow(a.x-b.x,)+pow(a.y-b.y,)); }
inline int find(int x){ return x==f[x]?x:f[x]=find(f[x]); }
int main(){
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
scanf("%lf%lf",&p[i].x,&p[i].y);
f[i]=i;
}
for(int i=;i<=m;i++){
int u,v; scanf("%d%d",&u,&v);
int fu=find(u),fv=find(v);
if(fu!=fv) f[fu]=fv,cnt++;
}
for(int i=;i<=n;i++)for(int j=;j<=n;j++) g[(i-)*n+j]=edge(i,j,dis(p[i],p[j]));
sort(g+,g++n*n);
int tot=n*n;
for(int i=;i<=tot,cnt<=n;i++){
int fx=find(g[i].from),fy=find(g[i].to);
if(fx!=fy){
f[fx]=fy;
ans+=dis(p[g[i].from],p[g[i].to]);
cnt++;
}
}
printf("%.2lf\n",ans);
return ;
}

BZOJ_1626_[Usaco2007_Dec]_Building_Roads_修建道路_(Kruskal)的更多相关文章

  1. 【BZOJ】1626: [Usaco2007 Dec]Building Roads 修建道路(kruskal)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1626 依旧是水题..太水了.. #include <cstdio> #include & ...

  2. BZOJ 1626 [Usaco2007 Dec]Building Roads 修建道路:kruskal(最小生成树)

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1626 题意: 有n个农场,坐标为(x[i],y[i]). 有m条原先就修好的路,连接农场( ...

  3. BZOJ 1626: [Usaco2007 Dec]Building Roads 修建道路( MST )

    计算距离时平方爆了int结果就WA了一次...... ------------------------------------------------------------------------- ...

  4. bzoj 1626: [Usaco2007 Dec]Building Roads 修建道路 -- 最小生成树

    1626: [Usaco2007 Dec]Building Roads 修建道路 Time Limit: 5 Sec  Memory Limit: 64 MB Description Farmer J ...

  5. 【二分+SPFA】修建道路(road)

    (四五年以前的老草稿,作为强迫症还是发布出来吧) 修建道路(road.pas/c/cpp) [问题描述] NOIP2012的参赛者LG异想天开打算修建一条磁悬浮列车的通道连接现代OI王国的首都(编号为 ...

  6. Codevs_1403_新三国争霸_(Kruskal+动态规划)

    描述 http://codevs.cn/problem/1403/ 共t天,n个点,m条边,选择每条边要付出不同的代价,其中某些天某些边不能用,要保证每一天n个点都是连通的,如果换方案要付出额外的代价 ...

  7. [Usaco2007 Dec]Building Roads 修建道路

    题目描述 Farmer John最近得到了一些新的农场,他想新修一些道路使得他的所有农场可以经过原有的或是新修的道路互达(也就是说,从任一个农场都可以经过一些首尾相连道路到达剩下的所有农场).有些农场 ...

  8. BZOJ-1196 公路修建问题 最小生成树Kruskal+(二分??)

    题目中一句话,最大费用最小,这么明显的二分的提示(by 以前morestep学长的经验传授)...但完全没二分,1A后感觉很虚.. 1196: [HNOI2006]公路修建问题 Time Limit: ...

  9. bzoj1626[Usaco2007 Dec]Building Roads 修建道路

    Description Farmer John最近得到了一些新的农场,他想新修一些道路使得他的所有农场可以经过原有的或是新修的道路互达(也就是说,从任一个农场都可以经过一些首尾相连道路到达剩下的所有农 ...

随机推荐

  1. 重置mysql管理员密码

    重置管理员密码 1.关闭mysql 2.开启mysql,跳过授权表mysql服务 提示:如果此步骤操作成功,那么任何用户登陆MySQL都不需要用户名与密码 保持此窗口不能关闭 3.重新cmd,登陆 m ...

  2. Entity Framework 学习笔记(2)

    上期回顾:Entity Framework 学习笔记(1) Entity Framework最主要的东西,就是自己创建的.继承于DbContext的类: /// <summary> /// ...

  3. 省市县 三级 四级联动Javascript JQ 插件PCASClass.js

    想要使用这款组件,需要页面引入 PCASClass.js 核心文件,该文件在您的HTML文档<head>标签之内. <script type="text/javascrip ...

  4. Windows phone 8 安装在 VMWare上错误的各种解决方案

    http://windowsasusual.blogspot.jp/2013/01/how-to-launch-windows-phone-8-emulator.html Hardware requi ...

  5. TDD三大定律

    You must write a failing unit test before you write production code. You must stop writing that unit ...

  6. C# zip/unzip with ICSharpCode.SharpZipLib

    download ICSharpCode and add reference using System; using System.Collections.Generic; using System. ...

  7. cocos2dx3.4 保存json文件

    头文件: #include "json/document.h" #include "json/stringbuffer.h" #include "js ...

  8. Shell编程练习

    1.使用case语句 2.使用while....do....done语句 3.使用

  9. 【转载】Java 升级到jdk7后DbVisualizer 6 启动空指针的处理方案

    将JDK从6升级到了7(或从其他电脑移植DBV文件夹后),每当启动DbVisualizer 6的时候都会报空指针异常 在官网上找到了相关的方案,如下: In the DbVisualizer inst ...

  10. The output char buffer is too small to contain the decoded characters, encoding 'Unicode (UTF-8)' fallback 'System.Text.DecoderReplacementFallback'.

    Exception when executing ) br is a binary reader. The data to peak is D000 (D0=208) The cause is, fo ...