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表示不能种 相邻的 ...
随机推荐
- 关于jquery获取单选框value属性值为on的问题
当取单选框的value值的时候,前提是要有value这个属性,如果没有value属性那么取出来的就会为on 取value值的常见三种方式为 $("input[name='XXX']:chec ...
- 2017团体程序设计天梯赛大区赛 L3-3 球队“食物链”
思路: 状压dp. 实现: #include <iostream> #include <cstdio> #include <cstring> using names ...
- poj1930 Dead Fraction
思路: 循环小数化分数,枚举所有可能的循环节,取分母最小的那个. 实现: #include <iostream> #include <cstdio> #include < ...
- jQuery核心语法
.each只是处理jQuery对象的方法,jQuery还提供了一个通用的jQuery.each方法,用来处理对象和数组的遍历 jQuery/($).each(array, callback )jQue ...
- .NET Core MVC Web最最最基础的框架搭建
1. 使用VS创建.NET Core MVC Web项目 创建完成就是酱紫的了 2. 用NuGet把这些全部都安装了 Install-Package Microsoft.EntityFramework ...
- 迅为IMX6UL工业级商业扩展级核心板兼容同一底板
商业级IMX6UL核心板: ARM Cortex-A7架构 主频高达528 MHz 核心板512M DDR内存 8G EMMC 存储 运行温度:-20℃ ~ +80℃ CPU集成电源管理 核心板尺寸仅 ...
- C# 获取文件编码
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Tex ...
- python appium自动化,走过的坑
使用的夜神模拟器,使用android5.1.1 第一坑:使用的android7.1.2,刚开始写好了登录的代码,需要的是滑屏进入到登录界面,结果运行的时候,没有自动滑屏就报错:因为运行时,报了一个进程 ...
- 使用webpack搭建react项目 webpack-react-project
webpack-react-project 使用webpack搭建react项目 webpack搭建react项目 github源码 具体配置信息参照package.json和webpack.conf ...
- 循环中i++和++i哪个好
推荐使用++i,因为不需要返回临时对象,执行效率更高.