P2212 [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 心累

 

posted @ 2017-03-18 21:52  Aptal丶  阅读(158)  评论(0编辑  收藏  举报