BZOJ3479: [Usaco2014 Mar]Watering the Fields
3479: [Usaco2014 Mar]Watering the Fields
Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 81 Solved: 42
[Submit][Status]
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
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
题解:
呵呵,裸MST
代码:
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<string>
#define inf 1000000000
#define maxn 2500
#define maxm 500+100
#define eps 1e-10
#define ll long long
#define pa pair<int,int>
#define for0(i,n) for(int i=0;i<=(n);i++)
#define for1(i,n) for(int i=1;i<=(n);i++)
#define for2(i,x,y) for(int i=(x);i<=(y);i++)
#define for3(i,x,y) for(int i=(x);i>=(y);i--)
#define mod 1000000007
#define sqr(x) (x)*(x)
using namespace std;
inline int read()
{
int x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=*x+ch-'';ch=getchar();}
return x*f;
}
struct rec{int x,y;}a[maxn];
struct edge{int x,y,w;}e[maxn*maxn];
int n,k,ans,tot,fa[maxn];
inline bool cmp(edge a,edge b)
{
return a.w<b.w;
}
inline int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);}
int main()
{
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
n=read();k=read();
for1(i,n)
{
a[i].x=read();a[i].y=read();
for1(j,i-)
e[++tot].x=i,e[tot].y=j,e[tot].w=sqr(a[i].x-a[j].x)+sqr(a[i].y-a[j].y);
}
sort(e+,e+tot+,cmp);
int j=,i;
while(e[j].w<k)j++;
for1(i,n)fa[i]=i;
for(i=;i<n;i++)
{
while(j<=tot&&find(e[j].x)==find(e[j].y))j++;
if(j>tot)break;
fa[find(e[j].x)]=find(e[j].y);
ans+=e[j].w;
j++;
}
if(i<n)printf("-1\n");else printf("%d\n",ans);
return ;
}
BZOJ3479: [Usaco2014 Mar]Watering the Fields的更多相关文章
- 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(kruskal)
http://www.lydsy.com/JudgeOnline/problem.php?id=3479 这个还用说吗.... #include <cstdio> #include < ...
- BZOJ 3479: [Usaco2014 Mar]Watering the Fields(最小生成树)
这个= =最近刷的都是水题啊QAQ 排除掉不可能的边然后就最小生成树就行了= = CODE: #include<cstdio>#include<iostream>#includ ...
- 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 ...
- 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.保罗阴谋破坏一些机器,使得约翰的工作效率变低.保罗可 ...
- P2212 [USACO14MAR]浇地Watering the Fields
P2212 [USACO14MAR]浇地Watering the Fields 题目描述 Due to a lack of rain, Farmer John wants to build an ir ...
- (寒假集训)Watering the Fields (最小生成树)
Watering the Fields 时间限制: 1 Sec 内存限制: 64 MB提交: 26 解决: 10[提交][状态][讨论版] 题目描述 Due to a lack of rain, ...
随机推荐
- 利用CSS边框合并属性打造table细边框
CSS代码: <style> table{ border-collapse: collapse;/* 边框合并属性 */ width:200px; } th{ border: 1px so ...
- SKShapeNode类
继承自 SKNode:UIResponder:NSObject 符合 NSCoding(SKNode)NSCopying(SKNode)NSObject(NSObject) 框架 /System/L ...
- 机房收费系统——在VB中将MSHFlexGrid控件中的数据导出到Excel
机房收费系统中,好多查询的窗体都包含同一个功能:将数据库中查询到的数据显示在MSHFlexGrid控件中,然后再把MSHFlexGrid控件中的数据导出到Excel表格中. 虽然之前做过学生信息管理系 ...
- [Angular 2] Using Two Reducers Together
Add another reducer: export const SECOND = "SECOND"; export const HOUR = "HOUR"; ...
- PHP5.4的变化关注---What has changed in PHP 5.4.x(转)
What has changed in PHP 5.4.x Most improvements in PHP 5.4.x have no impact on existing code. There ...
- 关于虚拟继承类的大小问题探索,VC++ 和 G++ 结果是有区别的
昨天笔试遇到个 关于类占用的空间大小的问题,以前没怎么重视,回来做个试验,还真发现了问题,以后各位笔试考官门,出题时请注明是用什么编译器. vc6/vc8 cl 和 Dev-C 的g++ 来做的测试: ...
- WordPress程序流程分析
index.php 统一入口文件 包含wp-blog-heaer.php 包含wp-load.php 包含wp-config.php 数据库.语言包配置等 包含wp-setting.php 对各种运行 ...
- Android学习手记(5) 基本UI布局
1.View和ViewGroup Activity是Android应用程序的基本管理单元,Android的每一个窗口都是通过一个Activity来定义的,但是Activity并不能直接用来显示窗口.我 ...
- effective C#之 - 使用属性代替成员变量
使用属性代替公共成员变量,一个很明显的好处是,很容易在一个地方对成员变量进行控制,例如: class Customer { private string name; public string Nam ...
- Spring + CXF(REST):webservice not found
可能原因:spring的bean:SpringBus的创建晚于spring的bean:Server的创建 解决办法:指定依赖注入顺序@DependsOn 先写着,留个空,后面补充