【题解】Luogu P2212 [USACO14MAR] 浇地 Watering the Fields 最小生成树

裸的板子,判一下d和c的大小

我菜死了,kruskal写挂的原因竟然是,sort的范围错了

WA到爆了

kruskal都忘了怎么写,赶快滚去复习

code

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 namespace gengyf{
 4 #define ll long long
 5 const int maxn=1e6+10;
 6 inline int read(){
 7     int x=0,f=1;
 8     char c=getchar();
 9     while(c<‘0‘||c>‘9‘){if(c==‘-‘)f=-1;c=getchar();}
10     while(c>=‘0‘&&c<=‘9‘){x=(x*10)+c-‘0‘;c=getchar();}
11     return x*f;
12 }
13 int n,c,x[maxn],y[maxn],fa[maxn];
14 struct edge{
15     int u,v;
16     ll w;
17 }e[maxn*2];
18 int head[maxn],cnt;
19 inline void add(int u,int v,int w){
20     e[++cnt].u=u;e[cnt].v=v;e[cnt].w=w;
21 }
22 bool cmp(edge a,edge b){
23     return a.w<b.w;
24 }
25 int find(int x){
26     return x==fa[x]?x:fa[x]=find(fa[x]);
27 }
28 void kruskal(){
29     int ans=0,tot=0;
30     for(int i=1;i<=cnt;i++){
31         int u=find(e[i].u),v=find(e[i].v);
32         if(u!=v){
33             fa[u]=v;
34             ans+=e[i].w;tot++;
35         }
36         if(tot==n-1)break;
37     }
38     if(tot==n-1)printf("%d\n",ans);
39     else puts("-1");
40 }
41 int main(){
42     n=read();c=read();
43     for(int i=1;i<=n;i++){
44         x[i]=read();y[i]=read();
45         for(int j=1;j<i;j++){
46             int d=(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);
47             if(d>=c){
48                 add(i,j,d);
49             }
50         }
51     }
52     sort(e+1,e+1+cnt,cmp);
53     for(int i=1;i<=n;i++)fa[i]=i;
54     kruskal();
55     return 0;
56 }
57 }
58 signed main(){
59   gengyf::main();
60   return 0;
61 }

原文地址:https://www.cnblogs.com/gengyf/p/11629882.html

时间: 2024-11-15 06:16:21

【题解】Luogu P2212 [USACO14MAR] 浇地 Watering the Fields 最小生成树的相关文章

P2212 [USACO14MAR]浇地Watering the Fields

题目描述 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 buil

[USACO14MAR]浇地Watering the Fields

https://www.luogu.org/problem/show?pid=2212 题目描述 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

bzoj3479[Usaco2014 Mar]Watering the Fields*

bzoj3479[Usaco2014 Mar]Watering the Fields 题意: 草坪上有N个水龙头,修剪两个水管费用为欧几里得距离的平方. 修水管的人只愿意修费用大于等于c的水管,问将水龙头联通的最小总费用.N≤2000 题解: 最小生成树. 代码: 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 #define inc(i,j,k) for(int i=j;i<=k

(寒假集训)Watering the Fields (最小生成树)

Watering the Fields 时间限制: 1 Sec  内存限制: 64 MB提交: 26  解决: 10[提交][状态][讨论版] 题目描述 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 (x

bzoj 3479: [Usaco2014 Mar]Watering the Fields

3479: [Usaco2014 Mar]Watering the Fields Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 174  Solved: 97[Submit][Status][Discuss] Description Due to a lack of rain, Farmer John wants to build an irrigation system to send water between his N fields (1

BZOJ 3479: [Usaco2014 Mar]Watering the Fields( MST )

MST...一开始没注意-1结果就WA了... ---------------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<vector> #include<cmath> #include<algorithm> #include<iostream> #define rep

题解 luogu P1850 【换教室】

题解 luogu P1850 [换教室] 时间:2019.8.6 一晚上(约 3.5h 写完) 题目描述 对于刚上大学的牛牛来说,他面临的第一个问题是如何根据实际情况申请合适的课程. 在可以选择的课程中,有 \(2n\) 节课程安排在 \(n\) 个时间段上.在第 \(i\)(\(1 \leq i \leq n\))个时间段上,两节内容相同的课程同时在不同的地点进行,其中,牛牛预先被安排在教室 \(c_i\) 上课,而另一节课程在教室 \(d_i\) 进行. 在不提交任何申请的情况下,学生们需要

题解 luogu P5021 【赛道修建】

题解 luogu P5021 [赛道修建] 时间:2019.8.9 20:40 时间:2019.8.12 题目描述 C 城将要举办一系列的赛车比赛.在比赛前,需要在城内修建 \(m\) 条赛道. C 城一共有 \(n\) 个路口,这些路口编号为 \(1,2,\dots,n\),有 \(n-1\) 条适合于修建赛道的双向通行的道路,每条道路连接着两个路口.其中,第 \(i\) 条道路连接的两个路口编号为 \(a_i\) 和 \(b_i\),该道路的长度为 \(l_i\).借助这 \(n-1\) 条

【luogu P1879 [USACO06NOV]玉米田Corn Fields】 题解

题目链接:https://www.luogu.org/problemnew/show/P1879 状压DP. 设dp[i][j]表示第i行,状态为j的方案数 初始dp[0][0] = 1 这样一共12行12列,最多1<<12. 这样转移时,只要满足上下没有两个1,这两行分别满足没有相邻1. 加法原理转移. $ j&k==0 $ $ dp[i][j] += dp[i-1][k] $ #include <cstdio> #include <cstring> #inc