洛谷 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 ...
随机推荐
- Nginx 301与302配置
说明 1.首先看一个完整代码示例,关于nginx 301 302跳转的. 301跳转设置: server { listen 80; server_name 123.com; rewrite ^/(.* ...
- oracle 11 g数据库卸载(方法二)
1.开始->设置->控制面板->管理工具->服务 停止所有 Oracle服务. 2.开始->程序->Oracle - OraHome81->Oracle In ...
- BUG: scheduling while atomic 分析【转】
本文转载自:https://blog.csdn.net/cfy_phonex/article/details/12090943 遇到一个典型的schedule问题. <3>[26578 ...
- Count and Say,统计并输出,利用递归,和斐波那契数列原理一样。
问题描述:n=1,返回“1”:n=2,返回“11”:n=3,返回“21”:n=4,返回1211,.... 算法分析:和斐波那契数列道理差不多,都是后一个要依赖前一个元素.因此可以使用递归,也可以使用迭 ...
- ZC_01_获取Class对象
1. package reflectionZ; public class TreflectionZ { public static void main(String[] args) throws Cl ...
- ios通知使用 书上案例 简单易懂
/* The notification name */const NSString *ResultOfAppendingTwoStringsNotification =@"ResultOfA ...
- 014对象——对象 __isset __unset __sleep __wakeup
<?php /** * */ /*class lantian { public $name; public $age; private $money; public $c; function _ ...
- python基础之socket编程(TCP三次握手和四次挥手)
TCP协议中中的三次握手和四次挥手 建立TCP需要三次握手才能建立,而断开连接则需要四次握手.整个过程如下图所示: 先来看看如何建立连接的. 首先Client端发送连接请求报文,Server段接受连接 ...
- http://www.cnblogs.com/peida/archive/2013/05/31/3070790.html深入理解Java:SimpleDateFormat安全的时间格式化
http://www.cnblogs.com/peida/archive/2013/05/31/3070790.html
- Qt 出现“undefined reference to `vtable for”原因总结
http://blog.csdn.net/chenlong12580/article/details/7431104