题目描述

给定n个点,第i个点的坐标为(xi,yi)(xi,yi​),如果想连通第i个点与第j个点,需要耗费的代价为两点的距离。第i个点与第j个点之间的距离使用欧几里得距离进行计算,即:(xi-xj)2+(yi-yj)2

我们规定耗费代价小于c的两点无法连通,求使得每两点都能连通下的最小代价,如果无法连通输出-1

输入格式

第一行两个整数n,c代表点数与想要连通代价不能少于的一个数。
接下来n行每行两个整数xi,yi描述第i个点。

输出格式

一行一个整数代表使得每两点都能连通下的最小代价,如果无法连通输出 -1

数据规模

1≤n≤2000,0≤xi,yi≤1000,1≤c≤106

思路:

既然我们想要让这张图中的每两个点之间都能联通,那么这道题很明显就是一道使用最小生成树解决的问题。我们一共有最多2000个点,所以我们可以枚举每两个点之间的距离,然后判断距离是否满足≥c的条件。如果满足,则我们将这条边存储下来。

这里存储边我们仍然使用结构体存储起点、终点和边权的方法。

接下来我们还是要对边依照边权从小到大进行排序。

最后我们只需要跑一遍克鲁斯卡尔算法即可。

完整代码:

 1 #include<iostream>
2 #include<algorithm>
3 using namespace std;
4 int n,c;
5 struct dian{
6 int x,y;
7 }a[2005];
8 int fa[2005];
9 struct bian{
10 int start;
11 int end;
12 int dis;
13 }b[4000005];
14 bool cmp(bian a,bian b){
15 return a.dis<b.dis;
16 }
17 int find(int x){
18 if(x==fa[x]){
19 return x;
20 }else{
21 return fa[x]=find(fa[x]);
22 }
23 }
24 void unionn(int x,int y){
25 int r1=find(x);
26 int r2=find(y);
27 fa[r1]=r2;
28 }
29 int main(){
30 cin>>n>>c;
31 for(int i=1;i<=n;i++){
32 cin>>a[i].x>>a[i].y;
33 }
34 int cnt=1;
35 for(int i=1;i<=n;i++){
36 for(int j=i+1;j<=n;j++){
37 if((a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y)>=c){
38 b[cnt].start=i;
39 b[cnt].end=j;
40 b[cnt].dis=(a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y);
41 cnt++;
42 }
43 }
44 }
45 cnt--;
46 sort(b+1,b+cnt+1,cmp);
47 for(int i=1;i<=n;i++){
48 fa[i]=i;
49 }
50 int sum=0;//统计加入连通图的点数
51 int ans=0;//统计最小代价
52 for(int i=1;i<=cnt;i++){
53 if(find(b[i].start)!=find(b[i].end)){
54 sum++;
55 ans+=b[i].dis;
56 unionn(b[i].start,b[i].end);
57 }
58 if(sum==n-1){
59 break;
60 }
61 }
62 if(sum<n-1){
63 cout<<-1<<endl;
64 }else{
65 cout<<ans<<endl;
66 }
67 return 0;
68 }

这里要注意一点:两点间的距离在题目的定义中是直接的平方和,所以我们不需要使用double,但同时我们也要注意应当让边权直接与c比较,在比较时也不必对c取平方。

P2212 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 题解

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

  4. BZOJ3479: [Usaco2014 Mar]Watering the Fields

    3479: [Usaco2014 Mar]Watering the Fields Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 81  Solved: ...

  5. BZOJ 3479: [Usaco2014 Mar]Watering the Fields( MST )

    MST...一开始没注意-1结果就WA了... ---------------------------------------------------------------------------- ...

  6. bzoj 3479: [Usaco2014 Mar]Watering the Fields

    3479: [Usaco2014 Mar]Watering the Fields Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 174  Solved ...

  7. (寒假集训)Watering the Fields (最小生成树)

    Watering the Fields 时间限制: 1 Sec  内存限制: 64 MB提交: 26  解决: 10[提交][状态][讨论版] 题目描述 Due to a lack of rain, ...

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

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

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

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

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

随机推荐

  1. CAD中相交线怎样打断?CAD打断相交线步骤

    在CAD设计过程中,如果想要打断图纸中相交线该如何操作呢?大家第一个想到的是不是CAD打断命令?没错,CAD打断命令是可以实现的,但是过于麻烦,今天小编来给大家分享一个更简单的方法,那就是浩辰CAD软 ...

  2. lua-路径加载lua文件-函数返回值,访问lua文件中的变量

    lua文件如下: print("ddhdhh")function login(username,pswd)if username =="ms" and pswd ...

  3. 蓝桥杯训练赛二-1467 问题 F: 蓝桥杯基础练习VIP-完美的代价

    题目描述 回文串,是一种特殊的字符串,它从左往右读和从右往左读是一样的.小龙龙认为回文串才是完美的.现在给你一个串,它不一定是回文的,请你计算最少的交换次数使得该串变成一个完美的回文串.交换的定义是: ...

  4. 使用phpexcel导出excel和phpword导出word--简单使用

    <?php namespace app\index\controller; //离线环境不能使用composer安装,只能下载包文件,然后放在vendor下,代码中require使用 requi ...

  5. GIT迁移仓库地址时如何保留分支和历史记录

    需求背景 GIT仓库(GitLab)所在服务器因某些原因要关停,相关服务需转移到另外一台机器上. 操作步骤 # clone项目 git clone --mirror http://192.168.12 ...

  6. Java设计模式之抽象工厂(02)

    对工厂方法进行抽象.当增加新的产品时,不用改动工厂类.而是集成已有的工厂接口或者抽象工厂,创建新的工厂.这就是对扩展开发,对修改封闭. 1 package Pak; 2 3 public abstra ...

  7. HDLbits——Lfsr5

    Build this LFSR. The reset should reset the LFSR to 1 module top_module( input clk, input reset, // ...

  8. oracle中将同一组的数据拼接(转)

    需要用wm_concat函数来实现. 如目前在emp表中查询数据如下: 要按照deptno相同的将ename以字符串形式合并,可用如下语句: 1 select deptno,wm_concat(ena ...

  9. 大规模人脸分类—allgather操作(2)

    腾讯开源人脸识别训练代码TFace 中关于all_gather层的实现如下.接下来解释为什么backward要进行reduce相加操作. https://github.com/Tencent/TFac ...

  10. 肖sir__面试笔试题__阿里笔试题

    第一题: #给定一个无序数组nums和一个目标值target,返回数组中两个元素的和为target的算法,时间复杂度为O(1);def func1(nums,target):    dict1 = { ...