3479: [Usaco2014 Mar]Watering the Fields

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 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 <= 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

                          [Submit][Status][Discuss]

  居然当成162M,结果数组开爆了。。。。。

 #include<bits/stdc++.h>
using namespace std;
const int MAX=;
struct node{
int x,y;
};
node pos[MAX];
struct node1{
int left,right;
int cost;
};
node1 edge[MAX*MAX];
int cmp(const node1 &a,const node1 &b){
if(a.cost<b.cost) return ;
return ;
}
int fa[MAX];
int get_fa(int v){
if(v!=fa[v]){
fa[v]=get_fa(fa[v]);
}
return fa[v];
}
int hehe(int m,int n){
if(m>n){
m=m+n;
n=m-n;
m=m-n;
}
int mm=get_fa(m);
int nn=get_fa(n);
fa[mm]=nn;
}
int N;
int C;
int totedge;
int sum;
int tot;
int main(){ cin>>N>>C;
for(int i=;i<=N;i++){
int a,b;
cin>>a>>b;
pos[i].x=a;
pos[i].y=b;
} for(int i=;i<=N;i++){
for(int j=;j<=N;j++){
edge[++totedge].left=i;
edge[totedge].right=j;
edge[totedge].cost=(pos[i].x-pos[j].x)*(pos[i].x-pos[j].x)+
(pos[i].y-pos[j].y)*(pos[i].y-pos[j].y);
}
} for(int i=;i<=N;i++) fa[i]=i;
sort(edge+,edge+totedge+,cmp);
for(int i=;i<=totedge;i++){
if(edge[i].cost>=C){
int h=edge[i].left;
int g=edge[i].right;
if(get_fa(h)!=get_fa(g)){
hehe(h,g);
sum+=edge[i].cost;
tot++;
if(tot==N-) break;
}
}
} if(tot==N-) cout<<sum;
if(tot<N-) cout<<-;
return ;
}

bzoj 3479: [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(最小生成树)

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

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

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

  4. BZOJ3479: [Usaco2014 Mar]Watering the Fields

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

  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. DP经典 BZOJ 1584: [Usaco2009 Mar]Cleaning Up 打扫卫生

    BZOJ 1584: [Usaco2009 Mar]Cleaning Up 打扫卫生 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 419  Solve ...

  9. P2212 [USACO14MAR]浇地Watering the Fields

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

随机推荐

  1. Eclipse下导入外部jar包的3种方式

    http://blog.csdn.net/mazhaojuan/article/details/21403717

  2. django用户认证系统——自定义认证后台8

    Django auth 应用默认支持用户名(username)进行登录.但是在实践中,网站可能还需要邮箱.手机号.身份证号等进行登录,这就需要我们自己写一个认证后台,用于验证用户输入的用户信息是否正确 ...

  3. HTML-HTML链接JavaScript的几种方法

    把JavaScript文件放在head中 标准方法是把JavaScript文件放到head标签内. <head> <script type="text/javascript ...

  4. 面试10大算法汇总——Java篇

    问题导读 1 字符串和数组 2 链表 3 树 4 图 5 排序 6 递归 vs 迭代 7 动态规划 8 位操作 9 概率问题 10 排列组合 11 其他 -- 寻找规律 英文版 以下从Java角度解释 ...

  5. UI层复习笔记

    在main文件中,UIApplicationMain函数一共做了三件事 根据第三个参数创建了一个应用程序对象 默认写nil,即创建的是UIApplication类型的对象,此对象看成是整个应用程序的一 ...

  6. 170209、mysql索引的建立

    用到索引最普通的情况,是为出现在where子句的字段建一个索引.为方便讲述,我们先建立一个如下的表. Code代码如下: CREATE TABLE mytable ( id serial primar ...

  7. 160727、自定义hibernate主键生成策略生成字符串+数字自增长

    需求:需要自增长注解如MyId0001.MyId0002.MyId0003 实现:实现这个接口org.hibernate.id.IdentifierGenerator 一.MyIdGenerator. ...

  8. Python全栈day26-27(面向对象进阶)

    参考 http://www.cnblogs.com/linhaifeng/articles/6204014.html 1,什么是反射 反射的概念是由Smith在1982年首次提出的,主要是指程序可以访 ...

  9. 记录--jquery 获取父级、子级、兄弟元素 + 实例

    需求如下: 三条数据,需点击其中一条数据在其下面展示与此数据关联的图片.主要功能可能是在点击的数据下增加显示行 思路: 把需要点击增加的数据先隐藏.点击后再将其显示出来. 知识点: jQuery.pa ...

  10. 【Python之路】第十八篇--MySQL(一)

    一.概述 1.什么是数据库 ? 答:数据的仓库. 2.什么是 MySQL.Oracle.SQLite.Access.MS SQL Server等 ? 答:他们均是一个软件,都有两个主要的功能: a. ...