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

相关内容

热门资讯

敦煌壁画的守护人--敦煌戈徒旅... 昨天刷抖音本来是想随便打发下时间,结果一个“穿越千年的敦煌色彩”的视频直接把我钉在原地,越看越入迷,...
湖南红色旅游主题口号及形象标识... 近日,由湖南省文化和旅游厅主办的湖南红色旅游主题口号及形象标识(LOGO)征集活动圆满结束,获奖作者...
2024年山西省旅游业大数据报... 今天分享的是:2024年山西省旅游业大数据报告 报告共计:20页 2024年山西省旅游业实现高质量增...
中国跻身南极第二大客源国,深圳... 深圳商报·读创客户端记者 范宏韬 11月18日,俞敏洪一连发布10条南极旅行视频,从穿越德雷克海峡到...
螺髻山温泉撷影 螺髻山温泉位于四川省凉山彝族自治州普格县螺髻山镇,该景区是我国山地中极为罕见且保存完好的第四纪古冰川...