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

相关内容

热门资讯

“江湖”好物出征草原 拉满赛场... 5月30日,通辽体育中心体育场,东北超第二轮鸡西对阵通辽的比赛还没开球,赛场外已经先“热”了起来。 ...
原创 告... 编辑:[太阳] 我们现在吃到的很多西红柿,早就不是老味道里的那颗西红柿了,它看着更红、更圆、更硬、更...
原创 中... 说句掏心窝子的话,豆腐脑这玩意儿,是中国人餐桌上最"吵架"的食物,没有之一。 你要是敢在网上发一句"...
张铁林的嘴有多刁?吃月饼只吃品... 吃五仁的。还真是五仁的,我就喜欢吃五仁的。老派,我吃自来红(月饼),西安叫水晶饼。那是我的命,就喜欢...
原创 高... 高考前期,这6道“益智菜”常做给孩子吃,补脑又解压,助力金榜题名! 距离高考只剩最后冲刺阶段,孩子...