BZOJ_3479_[Usaco2014 Mar]Watering the Fields_Prim

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


裸的一个最小生成树,只不过是欧几里得距离的。。不会优化。

于是学了下prim。

基本思路就是每次拿出一个dis最小的然后更新其他所有点的dis。

时间复杂度$O(n^2)$

代码:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define N 2050
int xx[N],yy[N],n,C,vis[N],dis[N];
int di(int i,int j) {
return (xx[i]-xx[j])*(xx[i]-xx[j])+(yy[i]-yy[j])*(yy[i]-yy[j]);
}
int main() {
scanf("%d%d",&n,&C);
int i,j;
for(i=1;i<=n;i++) {
scanf("%d%d",&xx[i],&yy[i]);
}
memset(dis,0x3f,sizeof(dis));
int x=1; vis[1]=1;
long long ans=0;
for(i=1;i<n;i++) {
int nxt=0;
for(j=1;j<=n;j++) {
if(!vis[j]) {
int tmp=di(x,j);
if(tmp>=C) dis[j]=min(dis[j],tmp);
if(!nxt) nxt=j;
else if(dis[j]<dis[nxt]) nxt=j;
}
}
if(dis[nxt]>2000000) {
puts("-1"); return 0;
}
x=nxt; ans+=dis[x]; vis[x]=1;
}
printf("%lld\n",ans);
}

BZOJ_3479_[Usaco2014 Mar]Watering the Fields_Prim的更多相关文章

  1. BZOJ3479: [Usaco2014 Mar]Watering the Fields

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

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

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

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

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

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

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

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

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

  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. BZOJ_3476_[Usaco2014 Mar]The Lazy Cow_扫描线+切比雪夫距离

    BZOJ_3476_[Usaco2014 Mar]The Lazy Cow_扫描线+切比雪夫距离 Description It's a hot summer day, and Bessie the c ...

  9. [Usaco2014 Mar]Sabotage

    [Usaco2014 Mar]Sabotage 题目 Farmer John"s arch-nemesis, Farmer Paul, has decided to sabotage Far ...

随机推荐

  1. bzoj1202:[HNOI2005]狡猾的商人 【并查集】

    Description 刁姹接到一个任务,为税务部门调查一位商人的账本,看看账本是不是伪造的.账本上记录了n个月以来的收入情况,其中第i 个月的收入额为Ai(i=1,2,3...n-1,n), .当 ...

  2. hdu 1565 状态压缩dp

    #include<stdio.h> #include<string.h> int Max(int a,int b) { return a>b?a:b; } int dp] ...

  3. msp430项目编程15

    msp430中项目---简易红外遥控系统 1.红外工作原理 2.电路原理说明 3.代码(显示部分) 4.代码(功能实现) 5.项目总结 msp430项目编程 msp430入门学习

  4. tomcat启动提示java.lang.UnsatisfiedLinkError: D:\soft\devTool\apache-tomcat-7.0.57\bin\tcnative-1.dll: C

    https://blog.csdn.net/a274360781/article/details/52411984

  5. codechef Taxi Driver

    题意: 给N个点求任意两个点的“距离”总和: A,B的“距离”定义为:min(|ax-bx|,|ay-by|) (n<200000) 好题! 解析: 看着没思路 先是公式化简:让 ax=sx+s ...

  6. Spring事务管理概述

    以下内容引用自http://wiki.jikexueyuan.com/project/spring/transaction-management.html: 数据库事务是一个被视为单一的工作单元的操作 ...

  7. 关于maven多个模块的build顺序 [INFO] Reactor Build Order

    对于一个maven项目,如果有多个模块,那么它们的执行顺序是什么样的呢? 在执行mvn操作的时候,你可以看到如下信息,这个便是maven的build顺序 那么maven是如何决定顺序的呢?如下: 在多 ...

  8. Google C++ style guide——C++类

    1.构造函数的职责 构造函数中仅仅进行那些没有实际意义的初始化.由于成员变量的"有意义"的值大多不在构造函数中确定. 能够的话,使用Init()方法集中初始化为有意义的数据. 长处 ...

  9. jason数据格式 -- 扫盲

    JSON是 JavaScript Object Notation的简称,是一种轻量的数据表示方法.jason格式採用key:value的方式记录数据,非常直观,比XML简洁,因而大受欢迎 介绍jaso ...

  10. 看懂JSP声明的格式。。。

    在WebRoot下新建test3.jsp 改动body内容: <%! int a = 3; %>     <% int b = 3; %>     <%= a--  %& ...