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. 富文本编辑期Quill

    官方网站http://quilljs.com/ 使用方法 <!-- Create the toolbar container --> <div id="toolbar&qu ...

  2. $().each() 与 $.each()区别,以及 jquery ajax 应用

    在jquery 中我们可以选择$().each() 与 $.each() 进行迭代对象和数组 $(items).each(function(){ //item })   , 而后者则 $.each(i ...

  3. 在Editplus中Dev C++配置C++的编译运行环境

    1.首先得下载安装DEV-cpp 2.打开Editplus编辑器,工具->配置自定义工具 3.具体配置 编译C:命令:D:\Dev-Cpp\MinGW64\bin\g++.exe参数:" ...

  4. JavaWeb中servlet读取配置文件的方式

    我们在JavaWeb中常常要涉及到一些文件的操作,比如读取配置文件,下载图片等等操作.那我们能不能采用我们以前在Java工程中读取文件的方式呢?废话不多说我们来看看下我们以前在Java工程中读取文件是 ...

  5. IO 流的操作基本规律

    想要知道开发时,使用哪个流对象, 只要通过四个明确即可. 明确源和目的(数据汇) 源: InputStream 或 Reader 目的: OutPutStream 或 Writer 明确数据是否是纯文 ...

  6. 转!!Java的三种代理模式

    转自 http://www.cnblogs.com/cenyu/p/6289209.html 1.代理模式 代理(Proxy)是一种设计模式,提供了对目标对象另外的访问方式;即通过代理对象访问目标对象 ...

  7. django博客项目9

    ................

  8. sql server代理系列

    https://www.cnblogs.com/woodytu/tag/SQL%20Server%E4%BB%A3%E7%90%86/ SQL Server代理(12/12):多服务器管理 Woody ...

  9. 用仿ActionScript的语法来编写html5——第三篇,鼠标事件与游戏人物移动

    第三篇,鼠标事件与游戏人物移动 一,假设假设,所有可添加鼠标事件的对象,都有一个mouseEvent方法,添加的鼠标事件同过这个mouseEvent来调用.这样的话,添加鼠标事件,其实只需要给canv ...

  10. python 测试代码

    1.使用print()打印 测试代码最简单的就是添加一些print()语句.然而产品开发中,需要记住自己添加的所有print()语句并在最后删除,很容易出现失误. 2.使用pylint.pyflake ...