P2212 [USACO14MAR]浇地Watering the Fields 洛谷
https://www.luogu.org/problem/show?pid=2212
题目描述
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(1 <= N <= 2000)块田送水。农田在一个二维平面上,第i块农田坐标为(xi, yi)(0 <= xi, yi <= 1000),在农田i和农田j自己铺设水管的费用是这两块农田的欧几里得距离(xi - xj)^2 + (yi - yj)^2。
农民约翰希望所有的农田之间都能通水,而且希望花费最少的钱。但是安装工人拒绝安装费用小于C的水管(1 <= C <= 1,000,000)。
请帮助农民约翰建立一个花费最小的灌溉网络。
输入输出格式
输入格式:
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
说明
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.
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.
Source: USACO 2014 March Contest, Silver
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#define cnt 2005 using namespace std; int c,n,tot,ans,num;
int fa[cnt],x[cnt],y[cnt];
struct node
{
int u,v,w;
}e[cnt*cnt]; void add(int a,int b,int d)
{
tot++;
e[tot].u=a;
e[tot].v=b;
e[tot].w=d;
} int find(int x)
{
return x==fa[x]?x:fa[x]=find(fa[x]);
} bool cmp(node aa,node bb)
{
return aa.w<bb.w;
} void Kruskal()
{
for(int i=;i<=cnt;i++) fa[i]=i;
sort(e+,e+tot+,cmp);
for(int i=;i<=tot;i++)
{
int fx=find(e[i].u),fy=find(e[i].v);
if(fx!=fy)
{
num++;
fa[fx]=fy;
ans+=e[i].w;
}
if(num==n-) return ;
}
ans=-;
return ;
} int main()
{
cin>>n>>c;
for(int i=;i<=n;i++)
cin>>x[i]>>y[i];
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
int dis=pow((x[i]-x[j]),)+pow((y[i]-y[j]),);
if(c<=dis)
add(i,j,dis);
}
Kruskal();
printf("%d",ans);
return ;
}
Kruskal,恶心的坑了我一晚上
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int n,m,k,hh,x[],y[],cnt,v[],t[];
int pd(int a,int b)
{
hh=(x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b]);
if(hh<k)
return ;
return ;
}
int main()
{
int i,j;
cin>>n>>k;
for(i=;i<=n;i++)
{
scanf("%d%d",&x[i],&y[i]);
v[i]=;
}
v[]=;
t[]=;
for(i=;i<=n;i++)
if(pd(i,))
v[i]=hh;
long long ans=;
for(i=;i<=n;i++)
{
int cnt=,pos=;
for(int i=;i<=n;i++)
if(!t[i]&&v[i]<cnt)
{
cnt=v[i];
pos=i;
}
if(!pos)
{
cout<<-<<endl;
return ;
}
t[pos]=;
ans+=cnt;
for(int i=;i<=n;i++)
if(!t[i]&&pd(pos,i)&&v[i]>hh)
v[i]=hh;
}
cout<<ans<<endl;
return ;
}
Prime 心累
P2212 [USACO14MAR]浇地Watering the Fields 洛谷的更多相关文章
- 洛谷——P2212 [USACO14MAR]浇地Watering the Fields
P2212 [USACO14MAR]浇地Watering the Fields 题目描述 Due to a lack of rain, Farmer John wants to build an ir ...
- 洛谷 P2212 [USACO14MAR]浇地Watering the Fields 题解
P2212 [USACO14MAR]浇地Watering the Fields 题目描述 Due to a lack of rain, Farmer John wants to build an ir ...
- P2212 [USACO14MAR]浇地Watering the Fields
P2212 [USACO14MAR]浇地Watering the Fields 题目描述 Due to a lack of rain, Farmer John wants to build an ir ...
- 洛谷 P2212 [USACO14MAR]浇地Watering the Fields
传送门 题解:计算欧几里得距离,Krusal加入边权大于等于c的边,统计最后树的边权和. 代码: #include<iostream> #include<cstdio> #in ...
- luogu题解 P2212 【浇地Watering the Fields】
题目链接: https://www.luogu.org/problemnew/show/P2212 思路: 一道最小生成树裸题(最近居然变得这么水了),但是因为我太蒻,搞了好久,不过借此加深了对最小生 ...
- [USACO14MAR]浇地Watering the Fields
题目描述 Due to a lack of rain, Farmer John wants to build an irrigation system tosend water between his ...
- 洛谷P1879 [USACO06NOV]玉米田Corn Fields(状压dp)
洛谷P1879 [USACO06NOV]玉米田Corn Fields \(f[i][j]\) 表示前 \(i\) 行且第 \(i\) 行状态为 \(j\) 的方案总数.\(j\) 的大小为 \(0 \ ...
- POJ3254或洛谷1879 Corn Fields
一道状压\(DP\) POJ原题链接 洛谷原题链接 很显然的状压,\(1\)表示种植,\(0\)表示荒废. 将输入直接进行状压,而要满足分配的草场是适合种草的土地,即是分配时的状态中的\(1\),在输 ...
- 【题解】洛谷P1879 [USACO06NOV] Corn Fields(状压DP)
洛谷P1879:https://www.luogu.org/problemnew/show/P1879 思路 把题目翻译成人话 在n*m的棋盘 每个格子不是0就是1 1表示可以种 0表示不能种 相邻的 ...
随机推荐
- leetcode764 Largest Plus Sign
思路: 首先使用dp计算出在每个位置(i, j)上下左右最多有多少个连续的1,得到up[i][j], down[i][j], left[i][j], right[i][j].然后计算这四个值中的最小值 ...
- CF792C Divide by Three
思路: dp. 实现: #include <iostream> #include <cstdio> #include <cstring> #include < ...
- Android基础TOP3:线性布局的特点,常用属性,及权重值
线性布局是一种让视图水平或者垂直布排列的布局: 常用属性: androuid:orientation :表示布局方向 取值vertical表示垂直布局 取值horizontal表示水平布局 andro ...
- oracle 创建表
--创建表 create table browser_track( btId number not null , opend_id ) not null, url_address ) not null ...
- R in action 读书笔记(1)--第五章:高级数据管理
5.2.1数学函数 函数 描述 abs(x) 绝对值 sqrt(x) 平方根 ceiling(x) 不小于x的最小整数 floor(x) 不大于x的最大整数 trunc(x) 向0的方向截取的X中的整 ...
- Nodejs AES加密不一致问题的解决
最近在做android游戏,客户端与Nodejs服务端数据的交互用AES进行加密,发现Nodejs与java的加密形式不一样.查询N久资料发现java端需要对密钥再MD5加密一遍(我了个大擦),本来对 ...
- ajax跨域上传图片
前台页面 var data = new FormData(); data.append('file', file); data.append('app', 'goods'); $.ajax({ url ...
- nginx教程从入门到精通
[转]nginx教程从入门到精通 nginx教程写了一段时间,无意中发现,nginx相关文章已经达到了近100篇了.觉得很有必要汇总到一起,它是我们运维生存时间的一片心血,他是学习nginx的同学必看 ...
- 合并百度影音的离线数据 with python 第二版 基于yield
重新整理了一下代码. 增加了bdv,mkv的处理流程. 目前暂时支持windows平台. bdv,mkv,rmvb的不同处理流程 # -*- coding: UTF-8 -*- import os i ...
- 迅为i.MX6Q嵌入式开发板
工业级核心板:核心板10层高速PCB设计,充分保证电磁兼容. 01. 处理器:开发板默认是四核商业扩展级芯片,可根据用户需求更换单核.双核.工业级.汽车级处理器,批量更省成本. 02. 扩展引脚:32 ...