bzoj 3479: [Usaco2014 Mar]Watering the Fields
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
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
居然当成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的更多相关文章
- BZOJ 3479: [Usaco2014 Mar]Watering the Fields( MST )
MST...一开始没注意-1结果就WA了... ---------------------------------------------------------------------------- ...
- 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 < ...
- 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_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.保罗阴谋破坏一些机器,使得约翰的工作效率变低.保罗可 ...
- DP经典 BZOJ 1584: [Usaco2009 Mar]Cleaning Up 打扫卫生
BZOJ 1584: [Usaco2009 Mar]Cleaning Up 打扫卫生 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 419 Solve ...
- P2212 [USACO14MAR]浇地Watering the Fields
P2212 [USACO14MAR]浇地Watering the Fields 题目描述 Due to a lack of rain, Farmer John wants to build an ir ...
随机推荐
- [越狱开发] theOpenDev配置与搭建碰到的问题
CodeSign error: code signing is required for product type 'Dynamic Library' in SDK 'iOS 6.1' 如何解决?
- Volley使用详细说明
前一篇粗略的介绍了一下Volley,并在最后附上了一段使用代码,这一篇详细的介绍一下Volley的使用.前面也说了Volley主要获取JSON对象和图片加载,这里也分为两部分介绍. 1.获取JSON对 ...
- windows安装oracle11g
windows上安装oracle11g 1.下载Oracle 11g R2 for Windows的版本 下载地址:https://www.oracle.com/technetwork/datab ...
- cocos2d-X学习之主要类介绍:动作:CCAction
引用自:http://www.cnblogs.com/lhming/archive/2012/07/01/2572238.html 类继承图: 主要函数: virtual CCObject * co ...
- 带jsk证书,请求https接口
首先是三个返回的实体类 BaseVo.java package https2; import java.io.Serializable; import java.lang.reflect.Invoca ...
- CodeForces 668B Little Artem and Dance
B. Little Artem and Dance time limit per test 2 second memory limit per test 256 megabytes input sta ...
- BLOCK方式实现OC程序中多个页面判定用户是否登录
在程序中经常会遇到这种情况,用户刚进入我们软件的时候我们是无需要求用户登录的,但是在下面的页面中,例如收藏,购买等页面的时候,显然在多个页面需要多次判定用户是否登录.试着用block简单的实现了一下该 ...
- simplest_ffmpeg_grabdesktop:屏幕录制。 simplest_ffmpeg_readcamera:读取摄像头
最简单的基于FFmpeg的AVDevice例子(屏幕录制) - 雷霄骅(leixiaohua1020)的专栏 - CSDN博客 https://blog.csdn.net/leixiaohua1020 ...
- Vue中动态添加多个class
vue中可以通过 :class=""这样来根据一定的条件来动态添加class,但是有时候需要判断的条件比较多,需要动态添加的class也比较多,这个时候其实也很简单 先看一下示例: ...
- hibernate detached分离查询 与 抓取策略注意事项
1.detached在抓取策略为 jion显式左外连接查询情况下 会产生笛卡儿积现象 DetachedCriteria dc = DetachedCriteria.forClass(Topic.cla ...