洛谷 P2212 [USACO14MAR]浇地Watering the Fields
题解:计算欧几里得距离,Krusal加入边权大于等于c的边,统计最后树的边权和。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define maxn 2009
using namespace std; int n,c,cnt,tot,ans;
int xi[maxn],yi[maxn],fa[maxn]; struct Edge{
int x,y,z;
}e[maxn*maxn]; bool cmp(Edge a,Edge b){
return a.z<b.z;
} int f(int x){
return fa[x]==x?x:fa[x]=f(fa[x]);
} int dis(int i,int j){
return (xi[i]-xi[j])*(xi[i]-xi[j])+(yi[i]-yi[j])*(yi[i]-yi[j]);
} int main(){
scanf("%d%d",&n,&c);
for(int i=;i<=n;i++){
fa[i]=i;
scanf("%d%d",&xi[i],&yi[i]);
for(int j=;j<i;j++){
e[++cnt].x=i;e[cnt].y=j;e[cnt].z=dis(i,j);
}
}
sort(e+,e+cnt+,cmp);
for(int i=;i<=cnt;i++){
if(e[i].z<c)continue;
int fx=f(e[i].x),fy=f(e[i].y);
if(fx!=fy){
tot++;
fa[fx]=fy;
ans+=e[i].z;
if(tot==n-)break;
}
}
if(tot==n-)
printf("%d\n",ans);
else puts("-1");
return ;
}
AC
洛谷 P2212 [USACO14MAR]浇地Watering the Fields的更多相关文章
- 洛谷——P2212 [USACO14MAR]浇地Watering the Fields
P2212 [USACO14MAR]浇地Watering the Fields 题目描述 Due to a lack of rain, Farmer John wants to build an ir ...
- 洛谷 P2212 [USACO14MAR]浇地Watering the Fields 题解
P2212 [USACO14MAR]浇地Watering the Fields 题目描述 Due to a lack of rain, Farmer John wants to build an ir ...
- P2212 [USACO14MAR]浇地Watering the Fields
P2212 [USACO14MAR]浇地Watering the Fields 题目描述 Due to a lack of rain, Farmer John wants to build an ir ...
- P2212 [USACO14MAR]浇地Watering the Fields 洛谷
https://www.luogu.org/problem/show?pid=2212 题目描述 Due to a lack of rain, Farmer John wants to build a ...
- luogu题解 P2212 【浇地Watering the Fields】
题目链接: https://www.luogu.org/problemnew/show/P2212 思路: 一道最小生成树裸题(最近居然变得这么水了),但是因为我太蒻,搞了好久,不过借此加深了对最小生 ...
- [USACO14MAR]浇地Watering the Fields
题目描述 Due to a lack of rain, Farmer John wants to build an irrigation system tosend water between his ...
- 洛谷P1879 [USACO06NOV]玉米田Corn Fields(状压dp)
洛谷P1879 [USACO06NOV]玉米田Corn Fields \(f[i][j]\) 表示前 \(i\) 行且第 \(i\) 行状态为 \(j\) 的方案总数.\(j\) 的大小为 \(0 \ ...
- 洛谷 P2212 【[USACO14MAR]Watering the Fields S】
一道最小生成树模板题,这里用的Kruskal算法,把每两点就加一条边,跑一遍最小生成树即可. #include <bits/stdc++.h> using namespace std; s ...
- 洛谷P1550 [USACO08OCT]打井Watering Hole
P1550 [USACO08OCT]打井Watering Hole 题目背景 John的农场缺水了!!! 题目描述 Farmer John has decided to bring water to ...
随机推荐
- Apache 虚拟主机配置
开放虚拟主机文件 修改主配置文件 解开注释,使用虚拟主机配置文件. vim /usr/local/apache2/conf/httpd.conf Include conf/extra/httpd-vh ...
- adjacent cache line prefetch
adjacent cache line prefetch 预读取邻近的缓存数据. 计算机在读取数据时,会智能的认为要读取的数据旁边或邻近的数据也是需要的, 那么其在处理的时候就会将这些邻近的数据预先读 ...
- IE开发人员工具手册
The DOM Explorer tool (CTRL + 1) The The DOM Explorer tool shows the structure of your webpage as it ...
- Contest-hunter 暑假送温暖 SRM08
01-07都没写...然后突然来写貌似有点突兀啊...不管了,难得前排记录一下... 吐槽一下赛制...不得不说很强... cf 套oi...很创新...不过还是兹磁ACM或者CF A-1 数据才2& ...
- Pandas字符串和文本数据
在本章中,我们将使用基本系列/索引来讨论字符串操作.在随后的章节中,将学习如何将这些字符串函数应用于数据帧(DataFrame). Pandas提供了一组字符串函数,可以方便地对字符串数据进行操作. ...
- glance cache
用在多个glance API server 中,对相同的image文件提供服务.该cache对用户透明. 配置文件有一个image_cache_max_size,超过的话image cache会被修剪 ...
- Spark- 根据ip地址计算归属地
主要考察的是广播变量的使用: 1.将要广播的数据 IP 规则数据存放在HDFS上,(广播出去的内容一旦广播出去产就不能改变了,如果需要实时改变的规则,可以将规则放到Redis中) 2.在Spark中转 ...
- Android两个Activity之间的数据交换
1. 不带数据 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceS ...
- 计蒜客button 概率
中文题意不多说,这题主要是数据太大,无法递推,所以用欧拉常数来解决 对于调和级数1/1+1/2+...+1/n=ln(n)+C+1/(2*n),C为欧拉常数,再加上ceil向上取整就可以了 #incl ...
- New Concept English three (40)
23w/m 48 errors It has never been explained why university students seem to enjoy practical jokes mo ...