3479: [Usaco2014 Mar]Watering the Fields

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 81  Solved: 42
[Submit][Status]

Description

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个水龙头,位于(xi,yi)

求将n个水龙头连通的最小费用。
任意两个水龙头可以修剪水管,费用为欧几里得距离的平方。
修水管的人只愿意修费用大于等于c的水管。

Input

* Line 1: The integers N and C.

* Lines 2..1+N: Line i+1 contains the integers xi and yi.

Output

* Line 1: The minimum cost of a network of pipes connecting the fields, or -1 if no such network can be built.

Sample Input

3 11
0 2
5 0
4 3

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.

Sample Output

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

HINT

 

Source

Silver 译文By Hta

题解:

呵呵,裸MST

代码:

 #include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#define inf 1000000000
#define maxn 2500
#define maxm 500+100
#define eps 1e-10
#define ll long long
#define pa pair<int,int>
#define for0(i,n) for(int i=0;i<=(n);i++)
#define for1(i,n) for(int i=1;i<=(n);i++)
#define for2(i,x,y) for(int i=(x);i<=(y);i++)
#define for3(i,x,y) for(int i=(x);i>=(y);i--)
#define mod 1000000007
#define sqr(x) (x)*(x)
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
return x*f;
}
struct rec{int x,y;}a[maxn];
struct edge{int x,y,w;}e[maxn*maxn];
int n,k,ans,tot,fa[maxn];
inline bool cmp(edge a,edge b)
{
return a.w<b.w;
}
inline int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
n=read();k=read();
for1(i,n)
{
a[i].x=read();a[i].y=read();
for1(j,i-)
e[++tot].x=i,e[tot].y=j,e[tot].w=sqr(a[i].x-a[j].x)+sqr(a[i].y-a[j].y);
}
sort(e+,e+tot+,cmp);
int j=,i;
while(e[j].w<k)j++;
for1(i,n)fa[i]=i;
for(i=;i<n;i++)
{
while(j<=tot&&find(e[j].x)==find(e[j].y))j++;
if(j>tot)break;
fa[find(e[j].x)]=find(e[j].y);
ans+=e[j].w;
j++;
}
if(i<n)printf("-1\n");else printf("%d\n",ans);
return ;
}

BZOJ3479: [Usaco2014 Mar]Watering the Fields的更多相关文章

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

    MST...一开始没注意-1结果就WA了... ---------------------------------------------------------------------------- ...

  2. bzoj 3479: [Usaco2014 Mar]Watering the Fields

    3479: [Usaco2014 Mar]Watering the Fields Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 174  Solved ...

  3. 【BZOJ】3479: [Usaco2014 Mar]Watering the Fields(kruskal)

    http://www.lydsy.com/JudgeOnline/problem.php?id=3479 这个还用说吗.... #include <cstdio> #include < ...

  4. BZOJ 3479: [Usaco2014 Mar]Watering the Fields(最小生成树)

    这个= =最近刷的都是水题啊QAQ 排除掉不可能的边然后就最小生成树就行了= = CODE: #include<cstdio>#include<iostream>#includ ...

  5. BZOJ_3479_[Usaco2014 Mar]Watering the Fields_Prim

    BZOJ_3479_[Usaco2014 Mar]Watering the Fields_Prim Description Due to a lack of rain, Farmer John wan ...

  6. BZOJ 3477: [Usaco2014 Mar]Sabotage( 二分答案 )

    先二分答案m, 然后对于原序列 A[i] = A[i] - m,  然后O(n)找最大连续子序列和, 那么此时序列由 L + mx + R组成. L + mx + R = sum - n * m, s ...

  7. BZOJ_3477_[Usaco2014 Mar]Sabotage_二分答案

    BZOJ_3477_[Usaco2014 Mar]Sabotage_二分答案 题意: 约翰的牧场里有 N 台机器,第 i 台机器的工作能力为 Ai.保罗阴谋破坏一些机器,使得约翰的工作效率变低.保罗可 ...

  8. P2212 [USACO14MAR]浇地Watering the Fields

    P2212 [USACO14MAR]浇地Watering the Fields 题目描述 Due to a lack of rain, Farmer John wants to build an ir ...

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

    Watering the Fields 时间限制: 1 Sec  内存限制: 64 MB提交: 26  解决: 10[提交][状态][讨论版] 题目描述 Due to a lack of rain, ...

随机推荐

  1. Core Services 层

    Core Services层为所有的应用程序提供基础系统服务.可能应用程序并不直接使用这些服务,但它们是系统很多部分赖以建构的基础. 高阶特性 下面的部分描述一些比较常见特性,也许您正打算让您的应用程 ...

  2. 与Scheme共舞

    发表在<程序猿>2007年7月刊上.不log上写帖子不用考虑版面限制,所以这里的帖子比发表的啰嗦点.赵健平编辑,Jacky,和刘未鹏都给了我非常多帮助,在这里一并谢了.免费的Scheme实 ...

  3. C#之Linq学习笔记【转】

    写在前面 其实在09年就已经学习过Linq了,并被她那优美的语法所吸引,只是现在所在的公司还在使用VS2005在.Net2.0的框架下面的开发,所以Linq也很久没有用过了,最近看部门的同事对这个有些 ...

  4. 在公网上布署Web Api的时候,不能调用,返回404

    在internet上布署web API做的站点时,发现不能调用web api的任何action, 返回404. 经过很多的努力,也找不到原因,环境是win server 2008, IIS 75. n ...

  5. POJ2449

    #include<stdio.h> #include<iostream> #include<queue> #include<vector> using ...

  6. mockito学习笔记

    mockito http://mockito.github.io/mockito/docs/current/org/mockito/Mockito.html

  7. asp.net中应用JQuery.pagination分页

    JQuery.pagination这款分页控件非常好用,可实现无刷新分页,为了方便下次做项目便于拷贝,所以在此发布一下,具体的实现流程如下: 效果图: JQuery.pagination的各个参数的说 ...

  8. ASP.NET中的SQL注入攻击与防护

    什么是SQL注入攻击? 它是在执行SQL查询的时候,由于接收了用户的非法参数从而导致,所执行的SQL语义与业务逻辑原本所要查询的语义不相符,从而实现的攻击. 例如我们经常使用的用户登录,通常会出现这样 ...

  9. mySQL优化 my.ini 配置说明

    [mysqld] port = 3306 serverid = 1 socket = /tmp/mysql.sock skip-name-resolve #禁止MySQL对外部连接进行DNS解析,使用 ...

  10. zookeeper集群的安装

    顾名思义zookeeper就是动物园管理员,他是用来管hadoop(大象).Hive(蜜蜂).pig(小猪)的管理员, Apache Hbase和 Apache Solr 的分布式集群都用到了zook ...