洛谷 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 ...
随机推荐
- Linux下捕捉信号
关于 信号signal的知识铺垫 点这里 信号由三种处理方式: 忽略 执行该信号的默认处理动作 捕捉信号 如果信号的处理动作是用户自定义函数,在信号递达时就调用这个自定义函数,这称为捕捉信号. 进程收 ...
- 实验四Android开发
实验四Java Android简易开发 实验准备 Android Studio 的下载: Android Studio 安装教程 在安装过程中的问题: 在下载了官网上的说明包含sdk的安装包之后找不到 ...
- Spring_通过 FactoryBean 配置 Bean
beans-factorybean.xml <?xml version="1.0" encoding="UTF-8"?><beans xmln ...
- mysql分库分表(一)
mysql分库分表 参考: https://blog.csdn.net/xlgen157387/article/details/53976153 https://blog.csdn.net/cleve ...
- AngularJS Source code
Angular.js 辅助函数 lowercase hasOwnProperty uppercase mannualLowercase mannualUppercase isArrayLike for ...
- mysql——jdbc驱动下载&连接mysql例子
mysql-connector-java-5.1.46.zip[解压后里面jar文件就是所需要的] https://dev.mysql.com/get/Downloads/Connector-J/my ...
- MySql 存储过程实例 - 转载
MySql 存储过程实例 将下面的语句复制粘贴可以一次性执行完,我已经测试过,没有问题! MySql存储过程简单实例: ...
- NumPy位操作
NumPy - 位操作 下面是 NumPy 包中可用的位操作函数. 序号 操作及描述 1. bitwise_and 对数组元素执行位与操作 2. bitwise_or 对数组元素执行位或操作 3. i ...
- Spring + Spring MVC + MyBatis框架整合
---恢复内容开始--- 一.Maven Web项目创建 如有需要,请参考:使用maven创建web项目 二.Spring + Spring MVC + MyBatis整合 1.Maven引入需要的J ...
- 用java实现单链表
对于一个单链表来说,要求有最基本的数据节点以及一些重要的方法. 方法应该有增删改查.定位.输出.获取链表长度.排序.链表读入.链表输出.下面是我用java写的单链表 public class List ...