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 和此题一模一样 , 双倍经验 ) ------ ...
随机推荐
- CentOS下配置iptables防火墙 linux NAT(iptables)配置
CentOS下配置防火墙 配置nat转发服务CentOS下配置iptables防火墙 linux NAT(iptables)配置 CentOS下配置iptables 1,vim /etc/syscon ...
- PM2.5
http://baike.baidu.com/view/1423678.htm PM2.5是指大气中直径小于或等于2.5微米的颗粒物,也称为可入肺颗粒物.虽然PM2.5只是地球大气成分中含量很少的组分 ...
- (九)jQuery中的动画(载)
原文链接:http://blog.csdn.net/zfy865628361/article/details/50358367 首先,用jQuery做动画效果要求在标准模式下,否则可能会引起动画抖动. ...
- SQL系列函数--字符串函数
1.charindex函数用来寻找一个指定的字符(串)在另一个字符串中的起始位置,返回一个整数,没找到就返回0 select CHARINDEX('SQL','Microsoft SQL SERVER ...
- VueJS计算属性: computed
computed属性 HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"&g ...
- C# 请求Web Api 接口,返回的json数据直接反序列化为实体类
须要的引用的dll类: Newtonsoft.Json.dll.System.Net.Http.dll.System.Net.Http.Formatting.dll Web Api接口为GET形式: ...
- 一致性哈希算法(c#版)
最近在研究"一致性HASH算法"(Consistent Hashing),用于解决memcached集群中当服务器出现增减变动时对散列值的影响.后来 在JAVAEYE上的一篇文章中 ...
- java 中的final
在编程语言中都有某种方式,告知编译器一块数据是恒定不变的.有两个需求 1. 一个永不改变的编译器常量 2. 一个在运行时被初始化的值,而这个值不会被改变 在Java中,使用final修饰变量实现这两个 ...
- JavaScritpt的DOM初探之Node(一)
DOM(文档对象模型)是针对HTML和XML文档的一个API. DOM描绘了一个层次化的节点树.同意开发者加入,移除和改动页面的某一部分.DOM脱胎于微软公司的DHTLM(动态HTML),可是如今它已 ...
- WPF converter(包含传递复杂参数)
单值转换器 将单一值转换为特定类型的值,以日期转换为例如下: 1.定制DateConverter类,其中当值从绑定源传播给绑定目标时,调用方法Convert. 1 public class DateC ...