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表示不能种 相邻的 ...
随机推荐
- AJPFX:关于面向对象的封装
1.回顾 面向对象 -- 注重的是结果,强调的是具备功能的对象. 面向过程 -- 强调的是函数,注重的实现的过程. 函数:对功能的封装. ...
- GIT配置及用法
ssh配置 TortoiseGit配置 用法: 下面是我整理的常用 Git 命令清单.几个专用名词的译名如下. Workspace:工作区 Index / Stage:暂存区 Repository:仓 ...
- 2017.5.20欢(bei)乐(ju)赛解题报告
预计分数:100+20+50=first 实际分数:20+0+10=gg 水灾(sliker.cpp/c/pas) 1000MS 64MB 大雨应经下了几天雨,却还是没有停的样子.土豪CCY刚从外地 ...
- select在数据库中有两种含义
select在数据库中有两种意思 (1)是赋值的意思(2)是输出,打印的意思我想你问的大概是赋值吧print和 select在数据库中都有打印输出的意思 用法是:select @aa=select* ...
- mysql 5.7安装过程中,初始化的问题
初始化不指定参数文件,如使用以下命令初始化: ./mysqld --initialize --user=mysql --basedir=/data/mysql/barry_mysql --datadi ...
- 玩转CPU运行曲线
Leaf 是不是从来没有想过看看cpu运行曲线啊骚年?顶多也就仅仅是看看cpu利用率,吓自己一跳后感觉关闭几个不该打开的程序~ 然而问题来了,微软公司要让你绘制cpu运行曲线啊!!不仅是固定的直线,还 ...
- leetcode_Stone Game_dp_思维
Alex和Lee玩游戏,共有偶数堆石头,石头总数为奇数,两人每次要么拿第一堆,要么拿最后一堆,两人以最优策略拿石堆(一次拿走完整的一堆),Alex先手,Alex赢返回True,否则返回False. 思 ...
- 反射(hasattr,getattr,delattr,setattr)
反射(hasattr,getattr,setattr,delattr) 反射在类中的使用 反射就是通过字符串来操作类或者对象的属性 反射本质就是在使用内置函数,其中反射有四个内置函数: hasattr ...
- CAD参数绘制直径标注(com接口)
主要用到函数说明: _DMxDrawX::DrawDimDiametric 绘制一个直径标注.详细说明如下: 参数 说明 DOUBLE dChordPointX 在被标注的曲线上的第一个点X值 DOU ...
- MSYS2 使用
在Windows下编译mongo-c-driver 1.3.x 在Windows下编译mongo-c-driver 1.3.x 1.安装 MSYS2https://sourceforge.net/pr ...