BZOJ 1626 [Usaco2007 Dec]Building Roads 修建道路:kruskal(最小生成树)
题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=1626
题意:
有n个农场,坐标为(x[i],y[i])。
有m条原先就修好的路,连接农场(a[i],b[i])。
现在要修一些路(首尾连接两个农场,长度为欧几里得距离),使得所有农场互相连通。
问修路的最短总距离。
题解:
最小生成树。
提前将m对点合并,再求最小生成树。
注:最后所有选出的边(包括原先的边)构成的并不一定是一棵树,因为原先的路中可能有环。
所以kruskal中不用判断cnt == n-1。
AC Code:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <vector>
#define MAX_N 1005 using namespace std; struct Edge
{
int sour;
int dest;
double len;
Edge(int _sour,int _dest,double _len)
{
sour=_sour;
dest=_dest;
len=_len;
}
Edge(){}
friend bool operator < (const Edge &a,const Edge &b)
{
return a.len<b.len;
}
}; int n,m;
int x[MAX_N];
int y[MAX_N];
int par[MAX_N];
double ans;
vector<Edge> edge; void init_union_find()
{
for(int i=;i<=n;i++)
{
par[i]=i;
}
} int find(int x)
{
return par[x]==x?x:par[x]=find(par[x]);
} void unite(int x,int y)
{
int px=find(x);
int py=find(y);
if(px==py) return;
par[px]=py;
} bool same(int x,int y)
{
return find(x)==find(y);
} void read()
{
// cin>>n>>m;
scanf("%d%d",&n,&m);
init_union_find();
for(int i=;i<=n;i++)
{
// cin>>x[i]>>y[i];
scanf("%d%d",&x[i],&y[i]);
}
int a,b;
for(int i=;i<m;i++)
{
// cin>>a>>b;
scanf("%d%d",&a,&b);
unite(a,b);
}
} inline double cal_len(int a,int b)
{
long long v1=(long long)x[a]-x[b];
long long v2=(long long)y[a]-y[b];
return sqrt(v1*v1+v2*v2);
} void build_graph()
{
for(int i=;i<=n;i++)
{
for(int j=;j<i;j++)
{
edge.push_back(Edge(i,j,cal_len(i,j)));
}
}
} double kruskal()
{
sort(edge.begin(),edge.end());
double res=;
for(int i=;i<edge.size();i++)
{
Edge temp=edge[i];
if(!same(temp.sour,temp.dest))
{
res+=temp.len;
unite(temp.sour,temp.dest);
}
}
return res;
} void solve()
{
build_graph();
ans=kruskal();
} void print()
{
printf("%.2f\n",ans);
} int main()
{
read();
solve();
print();
}
BZOJ 1626 [Usaco2007 Dec]Building Roads 修建道路:kruskal(最小生成树)的更多相关文章
- bzoj 1626: [Usaco2007 Dec]Building Roads 修建道路【最小生成树】
先把已有的边并查集了,然后MST即可 记得开double #include<iostream> #include<cstdio> #include<algorithm&g ...
- BZOJ 1626: [Usaco2007 Dec]Building Roads 修建道路( MST )
计算距离时平方爆了int结果就WA了一次...... ------------------------------------------------------------------------- ...
- bzoj 1626: [Usaco2007 Dec]Building Roads 修建道路 -- 最小生成树
1626: [Usaco2007 Dec]Building Roads 修建道路 Time Limit: 5 Sec Memory Limit: 64 MB Description Farmer J ...
- BZOJ——1626: [Usaco2007 Dec]Building Roads 修建道路
http://www.lydsy.com/JudgeOnline/problem.php?id=1626 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 1 ...
- 【BZOJ】1626: [Usaco2007 Dec]Building Roads 修建道路(kruskal)
http://www.lydsy.com/JudgeOnline/problem.php?id=1626 依旧是水题..太水了.. #include <cstdio> #include & ...
- [Usaco2007 Dec]Building Roads 修建道路[最小生成树]
Description Farmer John最近得到了一些新的农场,他想新修一些道路使得他的所有农场可以经过原有的或是新修的道路互达(也就是说,从任一个农场都可以经过一些首尾相连道路到达剩下的所有农 ...
- bzoj1626[Usaco2007 Dec]Building Roads 修建道路
Description Farmer John最近得到了一些新的农场,他想新修一些道路使得他的所有农场可以经过原有的或是新修的道路互达(也就是说,从任一个农场都可以经过一些首尾相连道路到达剩下的所有农 ...
- [Usaco2007 Dec]Building Roads 修建道路
题目描述 Farmer John最近得到了一些新的农场,他想新修一些道路使得他的所有农场可以经过原有的或是新修的道路互达(也就是说,从任一个农场都可以经过一些首尾相连道路到达剩下的所有农场).有些农场 ...
- BZOJ 1692: [Usaco2007 Dec]队列变换( 贪心 )
数据 n <= 30000 , 然后 O( n² ) 的贪心也过了..... USACO 数据是有多弱啊 = = ( ps : BZOJ 1640 和此题一模一样 , 双倍经验 ) ------ ...
随机推荐
- vue.js+koa2项目实战(五)axios 及 vue2.0 子组件和父组件之间的传值
axios 用法: 1.安装 npm install axios --save-dev 2.导入 import axios from 'axios'; 3.使用 axios.post(url,para ...
- 动态载入Layout 与 论Activity、 Window、View的关系
1)动态载入Layout的代码是 getWindow().setContentView(LayoutInflater.from(this).inflate(R.layout.main, null)); ...
- javascript 关于弹出新页面始终在正中央方法
记录一个关于弹出新页面始终在正中央方法 function openwindow(url, name, iWidth, iHeight) { var url; ...
- java拷贝构造函数
浅拷贝就是指两个对象共同拥有同一个值,一个对象改变了该值,也会影响到另一个对象. 深拷贝就是两个对象的值相等,但是互相独立. 构造函数的参数是该类的一个实例. Operator = 拷贝构造函数 ...
- Struts2学习七----------Struts2后缀
© 版权声明:本文为博主原创文章,转载请注明出处 Struts2后缀 - Struts2默认后缀是action - Struts2使用默认后缀时*.action和*都是同一个请求 - Struts2自 ...
- 多通道 移位寄存器 verilog
// Quartus II Verilog Template // Basic 64-stage shift register with multiple taps module basic_shif ...
- apt-mirror 校验错误文件处理
apt-mirror是一个用来将Debian或Ubuntu的软件源镜像到本地的工具,这个工具工作得非常好,不过有的时候由于网络问题,会有一些文件的校验是失败的,但apt-mirror并不能发现,等到最 ...
- c++引用返回值
引用作为函数的返回值时,函数的返回值能够理解为函数返回了一个变量(事实上,函数返回引用时,它返回的是一个指向返回值的隐式指针),因此,值为引用的函数能够用作赋值运算符的左操作数.另外,用引用返回一个函 ...
- mongodb的启动参数--quiet
”mongo群友在群里问了个问题,问的是--quiet启动参数如何用的? 如何理解安静的输出?“ 看到这个问题,之前看过--quiet这个参数,没有认真研究过,也没在生产中使用过. 在mongodb启 ...
- PS CC 破解安装教程(亲测可用)
PS CC版本新增了一些更高效的切图工具,比如可以直接右击图层转化为PNG图像 下面介绍一种亲测可用的破解安装教程 软件下载地址:https://pan.baidu.com/s/1dFJFqhj 一. ...