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 和此题一模一样 , 双倍经验 ) ------ ...
随机推荐
- FreeMark的list应用
语法:<#if></#if>后台传送List,前台html页面中获取该list并显示: <#if userList?exists> <#list userLi ...
- 新版本号的tlplayer for android ,TigerLeapMC for windows公布了
tlplayer for android 新版本号修正了图像倾斜等等问题,添加了动态水印功能. 支持hls(m3u8),http,rtsp,mms,rtmp等网络协议. 声明tlplayer 上的变速 ...
- JAVA中大数经常使用的函数
声明为 BigInteger 的 java.math 中的字段 static BigInteger BigInteger. ONE BigInteger 的常量 1. static BigIntege ...
- csdn开源夏令营-ospaf中期报告
1.背景 随着将中期的代码托管到CSDN的平台上,ospaf(开源项目成熟度分析工具)已经有了小小的雏形.当然还远远不够. 首先还是要感谢这次活动组织方CSDN,感觉挺有G ...
- sql字符串处理
sql字符串截取 substring(ParamName,start,length) start开始位置,length结束位置 DECLARE @Tel NVARCHAR(20) SET @Tel=' ...
- mysql 时间函数date_format
http://toptree.iteye.com/blog/812642今天,在开发邮件系统的时候发现有很多的邮件没有发送成功,想手动把数据修改.找了mysql 的日期函数 获得的pubtime为St ...
- ORA-02050故障诊断一例
http://czmmiao.iteye.com/blog/1474678昨天客户反映说在下午某时间段有几个事务失败了,让我查下当时数据库系统的负载是否正常,看了下CPU的历史负载,很正常,于是看了下 ...
- 在dev目录创建一个字符设备驱动的流程
1.struct file_operations 字符设备文件接口 1: static int mpu_open(struct inode *inode, struct file *file) 2: ...
- coreos 之flannel
提要: coreos 中 flannel 工具是coreos 网络划分工具.通过flannel 划分子网并向etcd 注册网络信息.可以做到宿主机集群中容器间网络通信. 1. 启动etcd2 服务: ...
- 【PHP开发】用curl向https发请求时的35号错误
放了个假发现以前写的程序的模拟登陆不管用了,中间输出,发现curl向https发请求时没有返回数据,输出错误信息,得到: curl_errno($ch) -----> 35 curl_error ...