bzoj3479【Usaco2014 Mar】Watering the Fields

3479: [Usaco2014 Mar]Watering the Fields

Time Limit: 10 Sec   Memory Limit: 128 MB
Submit: 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

3 11
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
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




裸最小生成树,优先队列优化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 1000000005
using 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");
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值