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
。
3 11
0 2
5 0
4 3
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 按照他说的去建,几个循环的问题 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
上一篇:闭园两个半月的深圳欢乐谷重新营业?客服:系员工亲友体验日,开业时间未定 深圳欢乐谷预计恢复营业时间 深圳欢乐谷什么时候营业
下一篇:19.5万内地客赴港跨年赏烟花 上半年过夜客人均消费7605港元 19.5万内地客赴港跨年赏烟花 上半年过夜客人均消费7605港元