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 和此题一模一样 , 双倍经验 ) ------ ...
随机推荐
- http Keep-Alive
1.什么是Keep-Alive模式? 我们知道HTTP协议采用“请求-应答”模式,当使用普通模式,即非KeepAlive模式时,每个请求/应答客户和服务器都要新建一个连接,完成之后立即断开连接(HTT ...
- $ is not defined
$ is not defined 引入Jquery的顺序不正确,要把它放在第一个引入
- es6 includes(), startsWith(), endsWith()
传统上,JavaScript 只有indexOf方法,可以用来确定一个字符串是否包含在另一个字符串中.ES6 又提供了三种新方法. includes():返回布尔值,表示是否找到了参数字符串. sta ...
- 【Python】matplotlib绘制折线图
一.绘制简单的折线图 import matplotlib.pyplot as plt squares=[1,4,9,16,25] plt.plot(squares) plt.show() 我们首先导入 ...
- kettle转换之多线程
kettle转换之多线程 ETL项目中性能方面的考虑一般是最重要的.特别是所讨论的任务频繁运行,或一些列的任务必须在固定的时间内运行.本文重点介绍利用kettle转换的多线程特性.以优化其性能. ...
- Mjpg_Streamer 的移植
1. 移植mjpg-streamer a.1 移植libjpeg tar zxf libjpeg-turbo-1.2.1.tar.gz cd libjpeg-turbo-1.2.1 ./configu ...
- .net 添加web引用和添加服务引用有什么区别?
添加web引用和添加服务引用有什么区别, Add Service References 和 Add Web References 有啥区别? 参考 http://social.microsoft.co ...
- 通过PHP获取文件创建与修改时间
1.获取文件创建时间示例: 1 2 $ctime=filectime("chinawinxp.txt"); echo "创建时间:".date("Y- ...
- ios -- 极光推送《1》
昨天公司项目要加入远程推送功能,自己做显然会很麻烦,所以用了极光的远程推送,下面我会讲如何制作推送证书并使用极光推送进行远程推送. 先讲讲怎么下载推送证书吧(前面的很简单要是知道的可以直接往下滑,简书 ...
- CF459C Pashmak and Buses 打印全排列
这题假设将终于的结果竖着看,每一列构成的数能够看成是k进制的数.一共同拥有d列,随意两列都不同样,所以这就是一个d位k进制数全排列的问题,一共同拥有k ^ d个排列.假设k ^ d < n,则打 ...