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

相关内容

热门资讯

导游强逼旅客购物,香港严惩旅游... 来源:环球时报【环球时报综合报道】香港特区政府严惩旅游业“害群之马”,又有两家旅行代理商以及一名导游...
沙虫酸奶拉高股价?燕塘乳业回应... 产品口味颠覆性创新,正成为区域乳企突围的新尝试。 近日,广东老牌乳企燕塘乳业旗下子公司推出的沙虫、生...
13道 风味热卖菜 百宴技为先、修得方成艺——马涛! 我是马涛,深耕酒店、餐饮一线工作至今30年,致力于酒店、餐饮经营有...
扬州人在上海大厦当主厨,把大闸... 外白渡桥畔,苏州河与黄浦江在此交汇。上海大厦在一旁已矗立近百年,二楼藏着一家叫“Le Reflet”...