P2212 Watering the Fields S
题目描述
给定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的更多相关文章
- 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 ...
- BZOJ3479: [Usaco2014 Mar]Watering the Fields
3479: [Usaco2014 Mar]Watering the Fields Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 81 Solved: ...
- BZOJ 3479: [Usaco2014 Mar]Watering the Fields( MST )
MST...一开始没注意-1结果就WA了... ---------------------------------------------------------------------------- ...
- bzoj 3479: [Usaco2014 Mar]Watering the Fields
3479: [Usaco2014 Mar]Watering the Fields Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 174 Solved ...
- (寒假集训)Watering the Fields (最小生成树)
Watering the Fields 时间限制: 1 Sec 内存限制: 64 MB提交: 26 解决: 10[提交][状态][讨论版] 题目描述 Due to a lack of rain, ...
- 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 思路: 一道最小生成树裸题(最近居然变得这么水了),但是因为我太蒻,搞了好久,不过借此加深了对最小生 ...
- 洛谷 P2212 [USACO14MAR]浇地Watering the Fields
传送门 题解:计算欧几里得距离,Krusal加入边权大于等于c的边,统计最后树的边权和. 代码: #include<iostream> #include<cstdio> #in ...
随机推荐
- NetBeans的一些快捷键
Alt + Insert getter 覆盖toString方法 Shift+ F6 运行当前文件 F6 运行当前项目 Tab 完成代码片段 ctrl+\ 代码补全 Alt + Shift + F格式 ...
- PyTables学习 (数据保存形式,对象树结构)
参考自http://www.pytables.org/usersguide/introduction.html PyTables的主要目的是提供一个好的操作HDF5文件的方法. HDF文件是分层数据格 ...
- unity ScriptObject使用
转自 https://www.jianshu.com/p/77fabc83555b using UnityEngine; [CreateAssetMenu(fileName = "Untit ...
- liunx部署flask项目
如何在linux上部署flask项目 Python3.7 + virtualenv + uwsgi + git + mysql-5.6.45 + nginx 源码编译安装所需要的环境 yum inst ...
- c原因学习---指针作为函数的形参
指针作为函数的形参, 可以改变实参的值. #include<stdio.h> // 交换两个变量的值 int swap(int x, int y) { int k = y; y = x; ...
- python连接Oracel、postgreSQL、SQLserver、Mysql、mongodb、redis等常用数据库方法汇总
python对接常用数据库 python有着极其丰富的第三方的库,如此强大的python语言操作各大数据库,不管你使用的关系型数据库是oracle,mysql, sqlserver,还是关系型数据库r ...
- js内置方法
数组: 1.push()数组最后添加元素,pop()数组删除最后一个: unshift()数组开头添加元素,shift()删除数字第一个: 注意:push()和unshift()可以添加 ...
- 312. 戳气球 (Hard)
问题描述 312. 戳气球 (Hard) 有 n 个气球,编号为 0 到 n - 1,每个气球上都标有一个数字,这些数字存在数组 nums 中. 现在要求你戳破所有的气球.戳破第 i 个气球,你可以获 ...
- php基础教程(三)
PHP语法概述 A. 基本语法 即使初次接触PHP的用户也会发现自己对PHP的语法风格并不陌生. 例如:<?php echo "Hello!": ?> 显示结果为&qu ...
- node.js 新手快速入门
我当初学的时候,是在大大们的指导下开始学习的,用了3天搞定大大们给的任务.下面我就把这个经历分享出来,让大家借鉴一下.欢迎吐槽~~ 任务如下: 根据Node js 开发入门教程第五章的一个使用node ...