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. time模块、datetime模块讲解

    time模块清楚三种格式的时间相互转换 import time# 时间分为三种格式#1.时间戳start= time.time()time.sleep(3)stop= time.time()print ...

  2. [BZOJ2809][Apio2012]dispatching 贪心+可并堆

    题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2809 我们考虑以每一个节点作为管理者所得的最优答案,一定是优先选择所要薪水少的忍者.那么首 ...

  3. STM32编程环境配置(kile5)

    2018-08-2513:53:33 折腾了很久,花了两天的空闲时间终于烧进去程序了.完成了kile5对stm32编程的环境配置. 1.下载kile5 激活破解 2.安装stm32配置环境 3.加载工 ...

  4. Win10 1803更新UWP无法安装的解决办法|错误代码0x80073D0D

    升级Win10 1803后,出现了之前安装的UWP.应用无法更新,再此安装失败的现象. 应用商店错误代码为:0x80073D0D,尝试卸载重装商店,清除应用缓存也无法解决. 最终解决办法: 下载Eve ...

  5. 绿化VSCode

    通过啃源码, 终于找到了解决办法, 设置环境变量: VSCODE_APPDATA=C:\Program Files\VSCode\UserData VSCODE_EXTENSIONS=%VSCODE_ ...

  6. struts2 源码地址

    http://archive.apache.org/dist/struts/

  7. rabiitmq

    Rabbitmq集群高可用 RabbitMQ是用erlang开发的,集群非常方便,因为erlang天生就是一门分布式语言,但其本身并不支持负载均衡. Rabbit模式大概分为以下三种:单一模式.普通模 ...

  8. servlet 常用指令

    一.设置响应内容类型 response.setContentType("text/html;charset=UTF-8"); 二.设置请求的字符编码 request.setChar ...

  9. python常见问题一(安装报错)

    常见问题一:我在安装python2.7时,提示错误:'An error occurred during the installation of assembly 'Microsoft.VC90.CRT ...

  10. Win10上 visual studio设置为本地IIS运行网站时 必须以管理员身份加载项目的解决方法

    右键,选择“兼容性疑难解答”. 选择“疑难解答程序” 选择“该程序需要附加权限” 点击测试程序 点击下一步 选择 是,为此程序保存这些设置