[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 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.

农民约翰想建立一个灌溉系统,给他的N(1 <= N <= 2000)块田送水。农田在一个二维平面上,第i块农田坐标为(xi, yi)(0 <= xi, yi <= 1000),在农田i和农田j自己铺设水管的费用是这两块农田的欧几里得距离(xi - xj)^2 + (yi - yj)^2。

农民约翰希望所有的农田之间都能通水,而且希望花费最少的钱。但是安装工人拒绝安装费用小于C的水管(1 <= C <= 1,000,000)。

请帮助农民约翰建立一个花费最小的灌溉网络。

输入输出格式

输入格式:

  • Line 1: The integers N and C.
  • Lines 2..1+N: Line i+1 contains the integers xi and yi.

输出格式:

  • 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

输出样例#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

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <cstdio>
 4 #include <cmath>
 5 #define cnt 2005
 6
 7 using namespace std;
 8
 9 int c,n,tot,ans,num;
10 int fa[cnt],x[cnt],y[cnt];
11 struct node
12 {
13     int u,v,w;
14 }e[cnt*cnt];
15
16 void add(int a,int b,int d)
17 {
18     tot++;
19     e[tot].u=a;
20     e[tot].v=b;
21     e[tot].w=d;
22 }
23
24 int find(int x)
25 {
26     return x==fa[x]?x:fa[x]=find(fa[x]);
27 }
28
29 bool cmp(node aa,node bb)
30 {
31     return aa.w<bb.w;
32 }
33
34 void Kruskal()
35 {
36     for(int i=1;i<=cnt;i++)    fa[i]=i;
37     sort(e+1,e+tot+1,cmp);
38     for(int i=1;i<=tot;i++)
39     {
40         int fx=find(e[i].u),fy=find(e[i].v);
41         if(fx!=fy)
42         {
43             num++;
44             fa[fx]=fy;
45             ans+=e[i].w;
46         }
47         if(num==n-1)    return ;
48     }
49     ans=-1;
50     return ;
51 }
52
53 int main()
54 {
55     cin>>n>>c;
56     for(int i=1;i<=n;i++)
57         cin>>x[i]>>y[i];
58     for(int i=1;i<=n;i++)
59         for(int j=1;j<=n;j++)
60         {
61             int dis=pow((x[i]-x[j]),2)+pow((y[i]-y[j]),2);
62             if(c<=dis)
63             add(i,j,dis);
64         }
65     Kruskal();
66     printf("%d",ans);
67     return 0;
68 }

Kruskal,恶心的坑了我一晚上

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 int n,m,k,hh,x[2005],y[2005],cnt,v[2005],t[2005];
 7 int pd(int a,int b)
 8 {
 9     hh=(x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b]);
10     if(hh<k)
11     return 0;
12     return 1;
13 }
14 int main()
15 {
16     int i,j;
17     cin>>n>>k;
18     for(i=1;i<=n;i++)
19     {
20         scanf("%d%d",&x[i],&y[i]);
21         v[i]=188888888;
22     }
23     v[1]=0;
24     t[1]=1;
25     for(i=2;i<=n;i++)
26     if(pd(i,1))
27     v[i]=hh;
28     long long ans=0;
29     for(i=2;i<=n;i++)
30     {
31         int cnt=188888887,pos=0;
32         for(int i=1;i<=n;i++)
33         if(!t[i]&&v[i]<cnt)
34         {
35             cnt=v[i];
36             pos=i;
37         }
38         if(!pos)
39         {
40             cout<<-1<<endl;
41             return 0;
42         }
43         t[pos]=1;
44         ans+=cnt;
45         for(int i=1;i<=n;i++)
46         if(!t[i]&&pd(pos,i)&&v[i]>hh)
47         v[i]=hh;
48     }
49     cout<<ans<<endl;
50     return 0;
51 }

Prime 心累

时间: 2024-10-10 22:45:08

[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

【题解】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(

(寒假集训)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

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

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

大神刷题表

9月27日 后缀数组:[wikioi3160]最长公共子串 dp:NOIP2001统计单词个数 后缀自动机:[spoj1812]Longest Common Substring II [wikioi3160]最长公共子串 [spoj7258]Lexicographical Substring Search 扫描线+set:[poj2932]Coneology 扫描线+set+树上删边游戏:[FJOI2013]圆形游戏 结论:[bzoj3706][FJ2014集训]反色刷 最小环:[poj1734

uva 10382 Watering Grass(贪心)

uva 10382 Watering Grass n sprinklers are installed in a horizontal strip of grass l meters long and w meters wide. Each sprinkler is installed at the horizontal center line of the strip. For each sprinkler we are given its position as the distance f

Poj 3254 Corn Fields(状态压缩)

Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8291   Accepted: 4409 Description Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parcels. He wants to grow some yumm