P2212 [USACO14MAR]Watering the Fields S 题解
admin
2024-01-16 16:36:45

P2212 [USACO14MAR]Watering the Fields S 题解

  • 题目
    • 链接
    • 字面描述
      • 题目描述
      • 输入格式
      • 输出格式
      • 样例 #1
        • 样例输入 #1
        • 样例输出 #1
      • 提示
        • 数据规模与约定
        • 说明
  • 难点
  • 代码实现

题目

链接

https://www.luogu.com.cn/problem/P2212

字面描述

题目描述

Due to a lack of rain, Farmer John wants to build an irrigation system to

send water between his N fields (1 <= N <= 2000).

Each field i is described by a distinct point (xi, yi) in the 2D plane,

with 0 <= xi, yi <= 1000. The cost of building a water pipe between two

fields i and j is equal to the squared Euclidean distance between them:

(xi - xj)^2 + (yi - yj)^2

FJ would like to build a minimum-cost system of pipes so that all of his

fields are linked together – so that water in any field can follow a

sequence of pipes to reach any other field.

Unfortunately, the contractor who is helping FJ install his irrigation

system refuses to install any pipe unless its cost (squared Euclidean

length) is at least C (1 <= C <= 1,000,000).

Please help FJ compute the minimum amount he will need pay to connect all

his fields with a network of pipes.

给定 nnn 个点,第 iii 个点的坐标为 (xi,yi)(x_i,y_i)(xi​,yi​),如果想连通第 iii 个点与第 jjj 个点,需要耗费的代价为两点的距离。第 iii 个点与第 jjj 个点之间的距离使用欧几里得距离的平方进行计算,即:
(xi−xj)2+(yi−yj)2(x_i-x_j)^2+(y_i-y_j)^2(xi​−xj​)2+(yi​−yj​)2
我们规定耗费代价小于 ccc 的两点无法连通,求使得每两点都能连通下的最小代价,如果无法连通输出 -1。

输入格式

* Line 1: The integers N and C.

* Lines 2…1+N: Line i+1 contains the integers xi and yi.

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

输出格式

* Line 1: The minimum cost of a network of pipes connecting the

fields, or -1 if no such network can be built.

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

样例 #1

样例输入 #1

3 11
0 2
5 0
4 3

样例输出 #1

46

提示

INPUT DETAILS:

There are 3 fields, at locations (0,2), (5,0), and (4,3). The contractor

will only install pipes of cost at least 11.

OUTPUT DETAILS:

FJ cannot build a pipe between the fields at (4,3) and (5,0), since its

cost would be only 10. He therefore builds a pipe between (0,2) and (5,0)

at cost 29, and a pipe between (0,2) and (4,3) at cost 17.

Source: USACO 2014 March Contest, Silver

数据规模与约定

对于 100%100\%100% 的数据,1≤n≤20001 \le n \le 20001≤n≤2000,0≤xi,yi≤10000 \le x_i,y_i \le 10000≤xi​,yi​≤1000,1≤c≤1061 \le c \le 10^61≤c≤106。

说明

Translated by 一只书虫仔。

难点

这道题相比其他模板最小生成树题目,唯一多了两个难点

  1. 没有给图
  2. 边权小于c的点不能纳入图中

针对难点1 按照他说的去建,几个循环的问题 2000^2 不会MLE
针对难点2 在算出两点之间的距离后,和c比较,大于等于在建边

ok,解决下,写代码

代码实现

#include
#define ll long long
using namespace std;const int maxn=2e3+10;
const int inf=2e9;
int n,m,cnt;
ll ans;
int dis[maxn],s[maxn],e[maxn];
int c[maxn][maxn];
bool vis[maxn];
inline int distance(int x1,int y1,int x2,int y2){return (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2);}
inline bool Prim(){vis[1]=true;for(int i=0;i<=n;i++)dis[i]=c[1][i];for(int i=1;iint temp=0;for(int j=1;j<=n;j++){if(!vis[j]&&dis[j]if(!vis[j]&&dis[j]>c[temp][j])dis[j]=c[temp][j];}} return true;
}
int main(){scanf("%d%d",&n,&m);for(int i=0;i<=n;i++){for(int j=0;j<=n;j++)c[i][j]=inf;}for(int i=1;i<=n;i++){scanf("%d%d",&s[i],&e[i]);// 难点1解决方案for(int j=1;jint ds=distance(s[i],e[i],s[j],e[j]);//难点2 解决方案if(ds

相关内容

热门资讯

山水皆有情!丽江两山一江的千年... 去过丽江、走过虎跳峡的人,大概率都听过哈巴雪山和金沙江的传说。很多人站在峡谷之下,望着奔腾不息的金沙...
活力中国调研行丨“殷”你而来 ... 9月12日,游客在殷墟博物馆“甲骨文展厅”内参观。位于河南省安阳市的殷墟博物馆新馆是首个全景式展现商...
浦江之夜赴美好相逢,第37届上... 9月12日晚,黄浦江畔灯火璀璨,第37届上海旅游节大巡游在外滩中山东一路上演。不少观众早早便来到外滩...
美丽中国丨秋韵紫鹊界 梯田入画... 这是9月11日拍摄的紫鹊界梯田(无人机照片)。 金秋九月,湖南省娄底市新化县紫鹊界梯田稻谷渐次成熟。...
中国之巅奏响《共同的节日》,千... 千吨阻尼器,“迎世赛、游上海旅游节”。9月13日,由上海市文化和旅游局指导推出上海旅游·会客│巅峰之...