Watering the Fields

时间限制: 1 Sec  内存限制: 64 MB
提交: 26  解决: 10
[提交][状态][讨论版]

题目描述

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.

输入

* Line 1: The integers N and C.
* Lines 2..1+N: Line i+1 contains the integers xi and yi.

输出

* Line 1: The minimum cost of a network of pipes connecting the fields, or -1 if no such network can be built.

样例输入

3 11
0 2
5 0
4 3

样例输出

46

提示

There are 3 fields, at locations (0,2), (5,0), and (4,3).  The contractor will only install pipes of cost at least 11.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.

【分析】最小生成树(裸)。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <queue>
#include <vector>
#define inf 0x3f3f3f3f
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
typedef long long ll;
using namespace std;
const int N = 4e3;
const int M = ;
int n,m,k,edg[N][N],lowcost[N],pre[N];
void Prim() {
for(int i=;i<=n;i++){
lowcost[i]=edg[][i];
}
lowcost[]=-;
int sum=;
for(int i=;i<n;i++){
int minn=;
for(int j=;j<=n;j++){
if(lowcost[j]!=-&&lowcost[j]<minn){
minn=lowcost[j];
k=j;
}
}
if(minn>=){
puts("-1");
return;
}
sum+=minn;
lowcost[k]=-;
for(int j=;j<=n;j++){
if(edg[j][k]<lowcost[j]){
lowcost[j]=edg[j][k];
}
}
}
printf("%d\n",sum);
}
int main()
{
for(int i=;i<N;i++)for(int j=;j<N;j++)edg[i][j]=;
int u,v,x[N],y[N];
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++){
scanf("%d%d",&x[i],&y[i]);
}
for(int i=;i<=n;i++){
for(int j=i+;j<=n;j++){
int s=(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]);
if(s>=m)edg[i][j]=edg[j][i]=s;
}
}
Prim();
return ;
}

(寒假集训)Watering the Fields (最小生成树)的更多相关文章

  1. P2212 [USACO14MAR]浇地Watering the Fields

    P2212 [USACO14MAR]浇地Watering the Fields 题目描述 Due to a lack of rain, Farmer John wants to build an ir ...

  2. 洛谷 P2212 [USACO14MAR]浇地Watering the Fields 题解

    P2212 [USACO14MAR]浇地Watering the Fields 题目描述 Due to a lack of rain, Farmer John wants to build an ir ...

  3. BZOJ3479: [Usaco2014 Mar]Watering the Fields

    3479: [Usaco2014 Mar]Watering the Fields Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 81  Solved: ...

  4. BZOJ 3479: [Usaco2014 Mar]Watering the Fields( MST )

    MST...一开始没注意-1结果就WA了... ---------------------------------------------------------------------------- ...

  5. bzoj 3479: [Usaco2014 Mar]Watering the Fields

    3479: [Usaco2014 Mar]Watering the Fields Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 174  Solved ...

  6. CSU-ACM寒假集训选拔-入门题

    CSU-ACM寒假集训选拔-入门题 仅选择部分有价值的题 J(2165): 时间旅行 Description 假设 Bobo 位于时间轴(数轴)上 t0 点,他要使用时间机器回到区间 (0, h] 中 ...

  7. 洛谷——P2212 [USACO14MAR]浇地Watering the Fields

    P2212 [USACO14MAR]浇地Watering the Fields 题目描述 Due to a lack of rain, Farmer John wants to build an ir ...

  8. BZOJ 3479: [Usaco2014 Mar]Watering the Fields(最小生成树)

    这个= =最近刷的都是水题啊QAQ 排除掉不可能的边然后就最小生成树就行了= = CODE: #include<cstdio>#include<iostream>#includ ...

  9. 【2018寒假集训Day 8】【最小生成树】Prim和Kruskal算法模板

    Luogu最小生成树模板题 Prim 原理与dijkstra几乎相同,每次找最优的点,用这个点去松弛未连接的点,也就是用这个点去与未连接的点连接. #include<cstdio> #in ...

随机推荐

  1. 使用pip命令报You are using pip version 9.0.3, however version 18.0 is available pip版本过期.解决方案

    使用pip命令安装或卸载第三方库时报You are using pip version 9.0.3, however version 18.0 is available.错误,一般情况下是pip版本过 ...

  2. Python全栈工程师(字符串/序列)

    ParisGabriel     Python 入门基础       字符串:str用来记录文本信息字符串的表示方式:在非注释中凡是用引号括起来的部分都是字符串‘’ 单引号“” 双引号''' ''' ...

  3. 稀疏矩阵相乘-Python版

                                          稀疏矩阵相乘-Python版 Given two sparse matrices A and B, return the r ...

  4. springboot08 jdbc

    一.JDBC&连接池 1. jdbc介绍 ​ JDBC(Java DataBase Connectivity ,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数 ...

  5. iptables的配置文件/etc/sysconfig/iptables不存在 linux防火墙开关命令

    某linux服务器,使用 cat /etc/sysconfig/iptables命令时, 找不到文件. 1. service iptables status 使用该命令检查状态 如果之前找不到配置文件 ...

  6. Spring 笔记(三)Bean 装配

    前言 Spring 有两大核心,也就分成两份笔记分别记录. 其一是管理应用中对象之间的协作关系,实现方式是依赖注入(DI),注入依赖的过程也被称为装配(Wiring). 基于 JavaConfig 的 ...

  7. [poj] 3977 Subset || 折半搜索MITM

    原题 给定N个整数组成的数列(N<=35),从中选出一个子集,使得这个子集的所有元素的值的和的绝对值最小,如果有多组数据满足的话,选择子集元素最少的那个. n<=35,所以双向dfs的O( ...

  8. 洛谷 P1903 [国家集训队]数颜色 解题报告

    P1903 [国家集训队]数颜色 题目描述 墨墨购买了一套\(N\)支彩色画笔(其中有些颜色可能相同),摆成一排,你需要回答墨墨的提问.墨墨会向你发布如下指令: 1.Q L R代表询问你从第\(L\) ...

  9. 性能优化-FSL(Force Synchronous Layout)强制同步布局

    通过chrome的Perfermance工具记录程序性能,切换到帧模式,点开其中一帧,看详情,中间为紫色的区块代表Layout,右上角带有红色三角的为警告,是chrome告知的强制同步布局,即FSL. ...

  10. display:table-cell的min-height

    table-cell的元素min-height是不起作用的,直接使用height就行,当高度不足时,table会自动拉伸cell元素. 此时,height相当于min-height. 出处:https ...