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. oralce 术语

    §2.1 术语 l 数据库块(BLOCK) ORACLE 数据库中的最小存储和处理单位,包含块本身的头信息数据或PL/SQL代码. ORACLE 块的大小是可以在安装时选择“自定义安装”来指定,也可以 ...

  2. Log4j 汇总

    一.概念 .1. log4j是 是线程安全的 日志框架,高度可配置,可通过在运行时的外部文件配置. 默认情况下,日志管理在CLASSPATH 查找一个名为 log4j.properties 的文件. ...

  3. vue-infinite-scroll 自动加载

    初次上手vue开发 笑话百出,各种麻爪 在实现上拉加载的时候用的是mint-ui里面的 infinite-scroll 结果在使用的时候不停的自动加载,后来查询了相关资料 原来是控件识别只要没有铺满全 ...

  4. Using Notepad++ To Quickly Format XML

    http://geek.sylvainw.com/2010/03/28/using-notepad-to-quickly-format-xml/ My favorite way to quickly ...

  5. 巨蟒python全栈开发-第4天 列表&元组&range

    今日内容大纲 1. 什么是列表 定义: 能装对象的对象 在python中使用[]来描述列表, 内部元素用逗号隔开. 对数据类型没有要求 列表存在索引和切片. 和字符串是一样的. 2. 相关的增删改查操 ...

  6. java操作mongoDB数据库的简单实例

    首先导入mongoDB的jar包 http://pan.baidu.com/s/1bnGMJRD //DataBase.java package com.mongodb.test; import ja ...

  7. 【Python之路】第二十四篇--爬虫

    网络爬虫(又被称为网页蜘蛛,网络机器人,在FOAF社区中间,更经常的称为网页追逐者),是一种按照一定的规则,自动地抓取万维网信息的程序或者脚本.另外一些不常使用的名字还有蚂蚁.自动索引.模拟程序或者蠕 ...

  8. curl post CURLOPT_POSTFIELDS

    PHP: curl_setopt - Manual http://php.net/manual/en/function.curl-setopt.php CURLOPT_POST TRUE to do ...

  9. 删除Windows服务

    或者开始→运行 ,输入“regedit”,在HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services 下找到需要删除的服务名,直接删除即可.

  10. PHP的几种排序方法

    <?php /** * PHP最常用的四个排序方法及二种查找方法 * 下面的排序方法全部都通过测试 * auther : soulence * date : 2015/06/20 */ //PH ...