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 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
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的更多相关文章
- BZOJ3479: [Usaco2014 Mar]Watering the Fields
3479: [Usaco2014 Mar]Watering the Fields Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 81 Solved: ...
- BZOJ 3479: [Usaco2014 Mar]Watering the Fields( MST )
MST...一开始没注意-1结果就WA了... ---------------------------------------------------------------------------- ...
- bzoj 3479: [Usaco2014 Mar]Watering the Fields
3479: [Usaco2014 Mar]Watering the Fields Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 174 Solved ...
- BZOJ 3479: [Usaco2014 Mar]Watering the Fields(最小生成树)
这个= =最近刷的都是水题啊QAQ 排除掉不可能的边然后就最小生成树就行了= = CODE: #include<cstdio>#include<iostream>#includ ...
- 【BZOJ】3479: [Usaco2014 Mar]Watering the Fields(kruskal)
http://www.lydsy.com/JudgeOnline/problem.php?id=3479 这个还用说吗.... #include <cstdio> #include < ...
- BZOJ 3477: [Usaco2014 Mar]Sabotage( 二分答案 )
先二分答案m, 然后对于原序列 A[i] = A[i] - m, 然后O(n)找最大连续子序列和, 那么此时序列由 L + mx + R组成. L + mx + R = sum - n * m, s ...
- BZOJ_3477_[Usaco2014 Mar]Sabotage_二分答案
BZOJ_3477_[Usaco2014 Mar]Sabotage_二分答案 题意: 约翰的牧场里有 N 台机器,第 i 台机器的工作能力为 Ai.保罗阴谋破坏一些机器,使得约翰的工作效率变低.保罗可 ...
- 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 ...
- [Usaco2014 Mar]Sabotage
[Usaco2014 Mar]Sabotage 题目 Farmer John"s arch-nemesis, Farmer Paul, has decided to sabotage Far ...
随机推荐
- sql通配符+sql中查询条件包含下划线等通配符的写法
一.SQL 通配符 在搜索数据库中的数据时,SQL 通配符可以替代一个或多个字符. SQL 通配符必须与 LIKE 运算符一起使用. 在 SQL 中,可使用以下通配符: 通配符 描述 % 替代一个或多 ...
- oracle 9i/10g/11g(11.2.0.3)安装包和PATCH下载地址汇总
今天上PUB看见一位热心人汇总了这么个地址列表,转发来空间: 把下面的地址复制到讯雷里就可以下载. -------------------------------------------------- ...
- 一篇文章告诉你,TLS 1.3 如何用性能为 HTTPS 正名
序•魔戒再现 几天前,OpenSSL 官方宣布即将发布的新版本 (OpenSSL 1.1.1) 将会提供 TLS 1.3 的支持,而且还会和之前的 1.1.0 版本完全兼容,这当然是个好消息. ...
- K/3Cloud 分页报表示例参考
分页报表首先需要实现的方法就是GetList,这个方法用来获得分页的条件. 其他的就和其他报表类似了. using System; using System.Collections.Generic; ...
- OpenJudge 6042 雇佣兵
37:雇佣兵 提问 总时间限制: 1000ms 内存限制: 65536kB 描述 雇佣兵的体力最大值为M,初始体力值为0.战斗力为N.拥有X个能量元素. 当雇佣兵的体力值恰好为M时,才可以参加一个 ...
- for-else和wihle-else组合用法
当for和else组合一起使用的时候,for循环正常执行完毕,会执行else语句,否则,不会执行else语句 for i in range(10): if i == 2: break else: pr ...
- XCode warning:“View Controller” is unreachable because it has no entry points
Unsupported Configuration: “View Controller” is unreachable because it has no entry points, and no i ...
- CentOS 7.0安装Zimbra 8.6邮件服务器
Zimbra的核心产品是Zimbra协作套件(Zimbra Collaboration Suite,简称ZCS). 系统:Centos7 ip地址:192.168.127.131 安装前准备 1.关闭 ...
- Scala入门到精通——第二十四节 高级类型 (三)
作者:摆摆少年梦 视频地址:http://blog.csdn.net/wsscy2004/article/details/38440247 本节主要内容 Type Specialization Man ...
- [RxJS] `add` Inner Subscriptions to Outer Subscribers to `unsubscribe` in RxJS
When subscribers create new "inner" sources and subscriptions, you run the risk of losing ...