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.

输入输出样例

输入样例#1:

3 11
0 2
5 0
4 3
输出样例#1:

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 洛谷的更多相关文章

  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. P2212 [USACO14MAR]浇地Watering the Fields

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

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

    传送门 题解:计算欧几里得距离,Krusal加入边权大于等于c的边,统计最后树的边权和. 代码: #include<iostream> #include<cstdio> #in ...

  5. luogu题解 P2212 【浇地Watering the Fields】

    题目链接: https://www.luogu.org/problemnew/show/P2212 思路: 一道最小生成树裸题(最近居然变得这么水了),但是因为我太蒻,搞了好久,不过借此加深了对最小生 ...

  6. [USACO14MAR]浇地Watering the Fields

    题目描述 Due to a lack of rain, Farmer John wants to build an irrigation system tosend water between his ...

  7. 洛谷P1879 [USACO06NOV]玉米田Corn Fields(状压dp)

    洛谷P1879 [USACO06NOV]玉米田Corn Fields \(f[i][j]\) 表示前 \(i\) 行且第 \(i\) 行状态为 \(j\) 的方案总数.\(j\) 的大小为 \(0 \ ...

  8. POJ3254或洛谷1879 Corn Fields

    一道状压\(DP\) POJ原题链接 洛谷原题链接 很显然的状压,\(1\)表示种植,\(0\)表示荒废. 将输入直接进行状压,而要满足分配的草场是适合种草的土地,即是分配时的状态中的\(1\),在输 ...

  9. 【题解】洛谷P1879 [USACO06NOV] Corn Fields(状压DP)

    洛谷P1879:https://www.luogu.org/problemnew/show/P1879 思路 把题目翻译成人话 在n*m的棋盘 每个格子不是0就是1 1表示可以种 0表示不能种 相邻的 ...

随机推荐

  1. wordpress在撰写新文章界面的显示选项按钮点击无反应的解决办法

    原文链接:wordpress在撰写新文章界面的显示选项按钮点击无反应的解决办法 最近升级wordpress之后,发现在文章编辑界面的添加新媒体和可视化按钮点击无反应,如下:  然后就在网上找解决办法, ...

  2. Android学习笔记-事件处理之Handler消息传递机制

    内容摘要:Android Handler消息传递机制的学习总结.问题记录 Handler消息传递机制的目的: 1.实现线程间通信(如:Android平台只允许主线程(UI线程)修改Activity里的 ...

  3. R in action读书笔记(3)-第六章:基本图形

    第六章  基本图形 6.1条形图 条形图通过垂直的或水平的条形展示了类别型变量的分布(频数).函数:barplot(height) 6.1.1简单的条形图 6.1.2推砌条形图和分组条形图 如果hei ...

  4. GIS在石油行业中的应用

    在石油工业中,发现新的石油资源,取得竞争优势,是成功的关键之一.GIS系统能帮助评估潜在的石油资源,及时.准确.直观地定位油气资源的空间分布及其特征,以正确有效地开展部署勘探开发工作,占领市场先机. ...

  5. linux 安装 mongo 3.4

    要求:linux 安装 mongo 3.4 大体上,按照官网提供的方法来做. 系统是ubuntu 16.04 安装的是mongo3.4.8 社区版 1.         导入导入包管理系统使用的公钥 ...

  6. Farseer.net轻量级开源框架 中级篇:探究ORM(Mapping)

    导航 目   录:Farseer.net轻量级开源框架 目录 上一篇:Farseer.net轻量级开源框架 中级篇: SQL执行报告 下一篇:Farseer.net轻量级开源框架 中级篇: Cooki ...

  7. ubuntu下pycharm无法使用pip安装python包的修复方案

    1. 在pycharm 中安装python包会报错“pycharm ModuleNotFoundError: No module named 'distutils.core'”: 2. 可能原因:in ...

  8. iTOP-6818开发板-Android4.4系统下RFID射频模块测试例程

    平台:迅为iTOP-6818开发板 系统:Android4.4版本 例程:RFID射频模块测试例程 rc522 驱动在 Android 系统的内核是默认集成的,用户可以在开发板上使用命令“ls /de ...

  9. iTOP-4412开发板网盘资料介绍

    iTOP-4412开发板网盘视频资料内容如下: 01-烧写.编译以及基础知识视频 02-嵌入式Linux 视频 03-iTOP-4412 开发板硬件设计指导视频 04-Android 应用程序视频 0 ...

  10. H5 canvas 直线和三角形

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...