洛谷 P2212 【[USACO14MAR]Watering the Fields S】
一道最小生成树模板题,这里用的Kruskal算法,把每两点就加一条边,跑一遍最小生成树即可。
#include <bits/stdc++.h>
using namespace std;
struct node{
int l , r , w;
};
node e[4000010];
int n , maxx , tot , now , ans;
int fa[2010] , a[2010] , b[2010];
int find(int x){
if(x == fa[x]) return x;
return fa[x] = find(fa[x]);
}
bool cmp(node x , node y){
return x.w < y.w;
}
int work(int i , int j){
return (a[i] - a[j]) * (a[i] - a[j]) + (b[i] - b[j]) * (b[i] - b[j]);
}
int main(){
cin >> n >> maxx;
for(int i = 1; i <= n; i++){
fa[i] = i;
cin >> a[i] >> b[i];
}
for(int i = 1; i <= n; i++)
for(int j = 1; j < i; j++){ //只用输入一半
int x = work(i , j);
if(x < maxx) continue;
tot++;
e[tot].l = i;
e[tot].r = j;
e[tot].w = x;
}
sort(e + 1 , e + tot + 1 , cmp);
for(int i = 1; i <= tot; i++){
if(now == n - 1) break;
int x = find(e[i].l) , y = find(e[i].r);
if(x == y) continue;
fa[x] = y;
now++;
ans += e[i].w;
}
if(now != n - 1) cout << -1; //边数不够
else cout << ans;
return 0;
}
洛谷 P2212 【[USACO14MAR]Watering the Fields S】的更多相关文章
- 洛谷——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
传送门 题解:计算欧几里得距离,Krusal加入边权大于等于c的边,统计最后树的边权和. 代码: #include<iostream> #include<cstdio> #in ...
- 洛谷2115 [USACO14MAR]破坏Sabotage
https://www.luogu.org/problem/show?pid=2115 题目描述 Farmer John's arch-nemesis, Farmer Paul, has decide ...
- 洛谷 2213 [USACO14MAR]懒惰的牛The Lazy Cow_Sliver
[题解] 每个格子可以到达的区域是一个菱形,但是我们并不能快速的求和,所以我们可以把原来的草地旋转45度,用二维前缀和快速处理菱形的区域的和. #include<cstdio> #incl ...
- 洛谷P2115 [USACO14MAR]破坏Sabotage
题目描述 Farmer John's arch-nemesis, Farmer Paul, has decided to sabotage Farmer John's milking equipmen ...
- 洛谷 P1879 玉米田Corn Fields 题解
题面 一道思维难度不大的状态压缩,也并不卡常,但细节处理要格外注意: f[i][j]表示前i行最后一行状态是j的方案数 #include <bits/stdc++.h> #define p ...
- 【学术篇】洛谷1550——打井Watering Hole
题目の传送门:https://www.luogu.org/problem/show?pid=1550 精简版题意(本来就精简了不是么):n个点,每个点可以选择打井或从别的有水的点引水,求所有点都有水用 ...
- P2212 [USACO14MAR]浇地Watering the Fields
P2212 [USACO14MAR]浇地Watering the Fields 题目描述 Due to a lack of rain, Farmer John wants to build an ir ...
随机推荐
- Java实现 LeetCode 650 只有两个键的键盘(递归 || 数学)
650. 只有两个键的键盘 最初在一个记事本上只有一个字符 'A'.你每次可以对这个记事本进行两种操作: Copy All (复制全部) : 你可以复制这个记事本中的所有字符(部分的复制是不允许的). ...
- Java实现 LeetCode 452 用最少数量的箭引爆气球
452. 用最少数量的箭引爆气球 在二维空间中有许多球形的气球.对于每个气球,提供的输入是水平方向上,气球直径的开始和结束坐标.由于它是水平的,所以y坐标并不重要,因此只要知道开始和结束的x坐标就足够 ...
- Java实现 LeetCode 229 求众数 II(二)
229. 求众数 II 给定一个大小为 n 的数组,找出其中所有出现超过 ⌊ n/3 ⌋ 次的元素. 说明: 要求算法的时间复杂度为 O(n),空间复杂度为 O(1). 示例 1: 输入: [3,2, ...
- Java实现 LeetCode 86 分割链表
86. 分隔链表 给定一个链表和一个特定值 x,对链表进行分隔,使得所有小于 x 的节点都在大于或等于 x 的节点之前. 你应当保留两个分区中每个节点的初始相对位置. 示例: 输入: head = 1 ...
- Java实现最长回文串
1 问题描述 给定一个字符串,求它的最长回文子串的长度. 2 解决方案 2.1 中心扩展法 此处,首先枚举出回文串的中心位置,然后,再在该位置上分别向左和向右扩展,记录并更新得到的最长回文串的长度. ...
- Unable to open debugger port (127.0.0.1:55119): java.net.SocketException "Socket closed"
1.端口问题 排查端口,lsof -i:8080 修改端口等 2.权限问题 端口排查无解的话,查看idea Event Log(View->Tool Window->Event Log) ...
- 君荣一卡通软件mysql转sqlserver 教程
Mysql数据库转sql数据库方法 注意:新建的SQL数据库一得先登录一次后再做迁移!!!!特别注意 如果客户以前安装的是mysql数据库,现在希望把mysql数据库转换的sql数据库,方法如下: 1 ...
- kali设置NAT模式,无法正常上网请试试这个办法
1.释放网卡: dhclient -r eth0 2.自动获取网络 dhclient -v eth0 3.开启22端口 lsof -i :22 4.打开ssh service ssh start sy ...
- python3 中调用post和get接口
用了很多方法都没有这个实用 POST API接口: import jsonimport requestsif __name__ == '__main__': url = "http://12 ...
- CocosCreator实现微信排行榜
1. 概述 不管是在现实生活还是当今游戏中,各式各样的排名层出不穷.如果我们做好一款游戏,却没有实现排行榜,一定是不完美的.排行榜不仅是玩家了解自己实力的途径,也是游戏运营刺激用户留存的一种途径.在微 ...