bzoj3479【Usaco2014 Mar】Watering the Fields
来源:互联网 发布:linux unrar 编辑:程序博客网 时间:2024/06/05 02:49
3479: [Usaco2014 Mar]Watering the Fields
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 213 Solved: 117
[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
Silver 译文By Hta
裸最小生成树,优先队列优化prim。
#include<iostream>#include<cstdio>#include<algorithm>#include<cmath>#include<cstring>#include<queue>#define F(i,j,n) for(int i=j;i<=n;i++)#define D(i,j,n) for(int i=j;i>=n;i--)#define LL long long#define pa pair<int,int>#define MAXN 2005#define INF 1000000005using namespace std;struct data{int x,y;}a[MAXN];struct edge_type{int to,next,w;}e[MAXN*MAXN];int n,c,cnt=0,ans=0,sum=0,head[MAXN],dis[MAXN];bool vst[MAXN];priority_queue<pa,vector<pa>,greater<pa> > q;void add_edge(int u,int v,int w){e[++cnt].to=v;e[cnt].w=w;e[cnt].next=head[u];head[u]=cnt;}int read(){int ret=0,flag=1;char ch=getchar();while (ch<'0'||ch>'9'){if (ch=='-') flag=-1;ch=getchar();}while (ch>='0'&&ch<='9'){ret=ret*10+ch-'0';ch=getchar();}return ret*flag;}void prim(){memset(vst,false,sizeof(vst));F(i,0,n) dis[i]=INF;dis[1]=0;q.push(make_pair(0,1));while (!q.empty()){int tmp=q.top().second;q.pop();if (vst[tmp]) continue;vst[tmp]=true;ans+=dis[tmp];sum++;for(int i=head[tmp];i;i=e[i].next){int to=e[i].to;if (!vst[to]&&dis[to]>e[i].w){dis[to]=e[i].w;q.push(make_pair(dis[to],to));}}}}int main(){n=read();c=read();F(i,1,n){a[i].x=read();a[i].y=read();}F(i,1,n-1) F(j,i+1,n){int tmp=(a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y);if (tmp>=c){add_edge(i,j,tmp);add_edge(j,i,tmp);}}prim();if (sum==n) printf("%d\n",ans);else puts("-1");}
- bzoj3479 [Usaco2014 Mar]Watering the Fields
- bzoj3479【Usaco2014 Mar】Watering the Fields
- 【BZOJ3479】[Usaco2014 Mar]Watering the Fields【最小生成树】
- bzoj3479 [Usaco2014 Mar]Watering the Fields(prim裸题)
- bzoj 3479: [Usaco2014 Mar]Watering the Fields 最小生成树
- 高中OJ3810. 【USACO2014 3月】Watering the Fields
- USACO2014MarchSilver Watering the Fields
- Watering the Fields题解
- jzoj 3810 Watering the Fields
- BZOJ 3476: [Usaco2014 Mar]The Lazy Cow
- Watering the Fields (最小生成树)
- 洛谷P2212 [USACO14MAR]浇地Watering the Fields
- bzoj3480【Usaco2014 Mar】Mooo
- [Usaco2014 Mar]Sabotage(dp)
- 3477: [Usaco2014 Mar]Sabotage
- 2014.3 USACO月赛T1 Watering the Fields
- bzoj 3480 [Usaco2014 Mar]Mooo
- 二分答案 [Usaco2014 Mar]Sabotage
- 过滤器实现跨域问题
- 我的QT学习笔记-02-QTimerEvent类定时器
- 二分图判定和割顶与桥的判断
- 仅通过崩溃地址找出源代码的出错行
- MySQL数据表的基本操作二:表结构查看、修改与表操作
- bzoj3479【Usaco2014 Mar】Watering the Fields
- JavaMap以及Java.Entry详解
- C语言之任意进制的转换
- iOS开发: 使用ARC的工程怎么同时再引用MRC的第三方类库
- BP神经网络的设计实例(MATLAB编程) .
- 《Spring JDBC详解》
- JQuery解析XML数据的几个例子
- java中输入一行字符,分别统计英文字母、空格、数字的个数
- [Java]读取文件方法大全