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. node中的require和exports

    http://cnodejs.org/topic/4f16442ccae1f4aa270010e9

  2. javascript 字符串进行 utf8 编码的方法(转)

    实践中碰到了一个大问题,在 javascript 中,可能有一些中文字符串,我们想将其进行二进制流编码的时候,需要将其转换为 utf8 的编码. 也就是说,输入的是一个字符串:'呆滞的慢板今天挣了10 ...

  3. Mysql数据库存储是乱码问题(或者在查询时无法加载数据)

    在连接数据库时添加一行代码即可解决:?useUnicode=true&characterEncoding=utf8 截图如下:

  4. VR应用开发遍地走的日子还有多远

    从上世纪60年代美国计算机科学家Ivan Sutherland发明的第一款真正意义上的虚拟现实头盔,到Facebook以20亿美元收购"虚拟现实之眼"Oculus Rift,大批厂 ...

  5. python数据类型及运算符

    python 的数据类型:数字.字符串.元组.列表.字典   type(a)  查看a的类型 数字: 整形int:-2 **32 ~+2**32 次方 长整形 a=123L 浮点float 复数型 - ...

  6. C语言函数重入

    C语言函数重入 可重入函数:可以被中断的函数,即这个函数执行时,可以中断其执行,可以由一个或多个任务并发使用,而不比担心数据错误. 不可重入函数(不安全函数) 不能运行在多任务环境下,除非能保证互斥( ...

  7. c++ new(不断跟新)

    1.基础知识 /* 可以定义大小是0的数组,但不能引用,因为没有指向任何对象 new string[10]调用类的默认构造函数 new int[10]没有初始化,但new int[10]()会将数组初 ...

  8. cocos2d-X学习之主要类介绍:动作:CCAction

    引用自:http://www.cnblogs.com/lhming/archive/2012/07/01/2572238.html 类继承图: 主要函数: virtual CCObject *  co ...

  9. 动态代理:JDK原生动态代理(Java Proxy)和CGLIB动态代理原理+附静态态代理

    本文只是对原文的梳理总结,以及自行理解.自己总结的比较简单,而且不深入,不如直接看原文.不过自己梳理一遍更有助于理解. 详细可参考原文:http://www.cnblogs.com/Carpenter ...

  10. ZOJ 3946 Highway Project(Dijkstra)

    Highway Project Time Limit: 2 Seconds      Memory Limit: 65536 KB Edward, the emperor of the Marjar ...