传送门

题解:计算欧几里得距离,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的更多相关文章

  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

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

  4. 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 ...

  5. luogu题解 P2212 【浇地Watering the Fields】

    题目链接: https://www.luogu.org/problemnew/show/P2212 思路: 一道最小生成树裸题(最近居然变得这么水了),但是因为我太蒻,搞了好久,不过借此加深了对最小生 ...

  6. [USACO14MAR]浇地Watering the Fields

    题目描述 Due to a lack of rain, Farmer John wants to build an irrigation system tosend water between his ...

  7. 洛谷P1879 [USACO06NOV]玉米田Corn Fields(状压dp)

    洛谷P1879 [USACO06NOV]玉米田Corn Fields \(f[i][j]\) 表示前 \(i\) 行且第 \(i\) 行状态为 \(j\) 的方案总数.\(j\) 的大小为 \(0 \ ...

  8. 洛谷 P2212 【[USACO14MAR]Watering the Fields S】

    一道最小生成树模板题,这里用的Kruskal算法,把每两点就加一条边,跑一遍最小生成树即可. #include <bits/stdc++.h> using namespace std; s ...

  9. 洛谷P1550 [USACO08OCT]打井Watering Hole

    P1550 [USACO08OCT]打井Watering Hole 题目背景 John的农场缺水了!!! 题目描述 Farmer John has decided to bring water to ...

随机推荐

  1. winform webbrowser禁用网页target=blank

    /// <summary> /// 屏蔽target=_blank 的弹出窗口 /// </summary> /// <param name="sender&q ...

  2. jar包错误

    Exception in thread "main" java.lang.NoSuchMethodError: org.slf4j.spi.LocationAwareLogger. ...

  3. viewport大白话

    以下所有内容均是我自己理解的,可能有误,懂得大佬希望指点一下我.. 首先,写一个简单的页面.里面只有1个200*200的div <html lang="en"> < ...

  4. JSP Tomcat8.0运行连接池时发生异常【AbstractMethodError oracle.jdbc.driver.T4CConnection.isValid(I)Z】

    原创 2015年12月28日 11:38:01 2004 一.Tomcat8.0运行连接池时发生异常: AbstractMethodError oracle.jdbc.driver.T4CConnec ...

  5. Latex排版全解【转载】

    Latex排版全解 https://www.cnblogs.com/jingwhale/p/4250296.html

  6. Java 正则表达式 Pattern & Matcher

    通常会有这种需求: 在文档中搜索有规律的字符串,然后进行统计或者替换.Java.util.regex包下的Pattern和Matcher这两个类提供了通过正则表达式来匹配查询,甚至替换的功能.那么我们 ...

  7. bzoj 2190: [SDOI2008]仪仗队 线性欧拉函数

    2190: [SDOI2008]仪仗队 Time Limit: 10 Sec  Memory Limit: 259 MB[Submit][Status][Discuss] Description 作为 ...

  8. Pandas分类数据

    通常实时的数据包括重复的文本列.例如:性别,国家和代码等特征总是重复的.这些是分类数据的例子. 分类变量只能采用有限的数量,而且通常是固定的数量.除了固定长度,分类数据可能有顺序,但不能执行数字操作. ...

  9. spring3: AOP 之代理机制

    Spring AOP通过代理模式实现,目前支持两种代理:JDK动态代理.CGLIB代理来创建AOP代理,Spring建议优先使用JDK动态代理. JDK动态代理:使用java.lang.reflect ...

  10. SmsUtils 发送短信

    import java.io.InputStream; import java.io.OutputStream; import java.io.OutputStreamWriter; import j ...