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 ...
随机推荐
- POJ-2386Lake Counting,搜索题。。
Lake Counting Time Limit: 1000MS Memory Limit: 65536K Description Due to recent rains, w ...
- j_spring_security_check 404错误
折腾了好久,还是写一篇备忘 折腾了好久,还是写一篇备忘 首先检查路径 <form class="form-signin" method="POST" ac ...
- 【HDOJ6298】Maximum Multiple(数论)
题意:给定n,求x,y,z三个整数,使得x|n,y|n,z|n,且xyz最小 n<=1e6 思路: 不定方程1/x+1/y+1/z=1 只有(2,3,6)(2,4,4) (3,3,3)三组正整数 ...
- ListView更新问题
ListView和Adapter对象均具备有对象更新方法 ListView对象列表的更新方法1.invalidate();--重绘组件2.invlidateView()--重绘组件并包含所有的View ...
- 2015轻院校赛 D 社交网络(排列组合)
http://acm.zznu.edu.cn/problem.php?id=1964 题目描述 输入 输出 样例输入 2 2 1 0 1 1 0 3 1 0 1 1 1 0 1 1 1 0 样例输出 ...
- Unique Binary Search Trees(dp)
Given n, how many structurally unique BST's (binary search trees) that store values 1...n? For examp ...
- 从头开始学Android之(二)—— Android版本
前面大致的介绍了一下Android的Linux内核层,知道Android是Google在Linux基础上创建的一个应用于移动设备的系统,并在针对移动设备的特殊性,在Linux上做了一些相应的改动建立起 ...
- 【转】nginx 和 php-fpm 通信使用unix socket还是TCP,及其配置
原文: http://blog.csdn.net/pcyph/article/details/46513521 -------------------------------------------- ...
- sudo 用户添加
sudo 用户添加 /etc/sudoers 在 ## Allow root to run any commands anywhere root ALL=(ALL) ALL 下面加上 xxx ...
- Navicat for MySQL无法连接到数据库怎么办
注意端口就是3306,不要改成80之类的,访问数据库就是从这个端口过去的