洛谷 P2872 [USACO07DEC]道路建设Building Roads

洛谷传送门

JDOJ 2546: USACO 2007 Dec Silver 2.Building Roads

JDOJ传送门

Description

Farmer John had just acquired several new farms! He wants to connect

the farms with roads so that he can travel from any farm to any

other farm via a sequence of roads; roads already connect some of

the farms.

Each of the N (1 <= N <= 1,000) farms (conveniently numbered 1..N)

is represented by a position (X_i, Y_i) on the plane (0 <= X_i <=

1,000,000; 0 <= Y_i <= 1,000,000). Given the preexisting M roads

(1 <= M <= 1,000) as pairs of connected farms, help Farmer John

determine the smallest length of additional roads he must build to

connect all his farms.

Input

* Line 1: Two space-separated integers: N and M

* Lines 2..N+1: Two space-separated integers: X_i and Y_i

* Lines N+2..N+M+2: Two space-separated integers: i and j, indicating

that there is already a road connecting the farm i and farm j.

Output

* Line 1: Smallest length of additional roads required to connect all

farms, printed without rounding to two decimal places. Be sure

to calculate distances as 64-bit floating point numbers.

Sample Input

4 1 1 1 3 1 2 3 4 3 1 4

Sample Output

4.00

HINT

INPUT DETAILS:

Four farms at locations (1,1), (3,1), (2,3), and (4,3). Farms 1 and 4 are

connected by a road.

OUTPUT DETAILS:

Connect farms 1 and 2 with a road that is 2.00 units long, then connect

farms 3 and 4 with a road that is 2.00 units long. This is the best we can

do, and gives us a total of 4.00 unit lengths.

Source

2007~2008

题目翻译:

Farmer John最近得到了一些新的农场,他想新修一些道路使得他的所有农场可以经过原有的或是新修的道路互达(也就是说,从任一个农场都可以经过一些首尾相连道路到达剩下的所有农场)。有些农场之间原本就有道路相连。 所有N(1 <= N <= 1,000)个农场(用1..N顺次编号)在地图上都表示为坐标为(X_i, Y_i)的点(0 <= X_i <= 1,000,000;0 <= Y_i <= 1,000,000),两个农场间道路的长度自然就是代表它们的点之间的距离。现在Farmer John也告诉了你农场间原有的M(1 <= M <= 1,000)条路分别连接了哪两个农场,他希望你计算一下,为了使得所有农场连通,他所需建造道路的最小总长是多少。

题解:

一道最小生成树的题。

这题的大意就是,有一些已经建好的边,问你再建多长的边能够使原图有一个最小生成树。

如果我们先依照题意连边未免太恶心。

所以我们考虑更优秀的一种做法(其实是更巧妙

我们先给题目的边打标记,最后建全图,如果有标记的边我们把它的边权置成0,这样就保证它一定在最小生成树上。

所以就可以A了。

(我用的是KUSKAL)

代码:

#include<cstdio>
#include<algorithm>
#include<cmath>
#define ll long long
using namespace std;
int n,m,vis[1001],tot,fa[1001];
long double ans;
struct node
{
ll x,y;
}a[1001];
struct edge
{
int u,v;
long double val;
}e[1000001<<1];
long double dist(int x,int y,int a,int b)
{
return sqrt((long double)(x-a)*(long double)(x-a)+(long double)(y-b)*(long double)(y-b));
}
void add(int x,int y,int z)
{
e[++tot].u=x;
e[tot].v=y;
e[tot].val=z;
}
bool cmp(edge a,edge b)
{
if(a.val==b.val)
return a.u<b.u;
return a.val<b.val;
}
int find(int x)
{
if(fa[x]==x)
return x;
return fa[x]=find(fa[x]);
}
void unionn(int x,int y)
{
int fx=find(x);
int fy=find(y);
if(fx!=fy)
fa[fx]=fy;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d%d",&a[i].x,&a[i].y);
for(int i=1;i<=n;i++)
fa[i]=i;
for(int i=1;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
vis[u]=vis[v]=1;
}
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++)
{
if(vis[i]==1 && vis[j]==1)
{
add(i,j,0);
add(j,i,0);
}
else
{
add(i,j,dist(a[i].x,a[i].y,a[j].x,a[j].y));
add(j,i,dist(a[i].x,a[i].y,a[j].x,a[j].y));
}
}
sort(e+1,e+tot+1,cmp);
for(int i=1;i<=tot;i++)
if(find(e[i].u)!=find(e[i].v))
{
ans+=e[i].val;
unionn(e[i].u,e[i].v);
}
printf("%.2lf",ans);
return 0;
}

USACO Building Roads的更多相关文章

  1. poj 3625 Building Roads

    题目连接 http://poj.org/problem?id=3625 Building Roads Description Farmer John had just acquired several ...

  2. poj 2749 Building roads (二分+拆点+2-sat)

    Building roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 6229   Accepted: 2093 De ...

  3. BZOJ 1626: [Usaco2007 Dec]Building Roads 修建道路( MST )

    计算距离时平方爆了int结果就WA了一次...... ------------------------------------------------------------------------- ...

  4. HDU 1815, POJ 2749 Building roads(2-sat)

    HDU 1815, POJ 2749 Building roads pid=1815" target="_blank" style="">题目链 ...

  5. Building roads

    Building roads Time Limit: 10000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tot ...

  6. bzoj1626 / P2872 [USACO07DEC]道路建设Building Roads

    P2872 [USACO07DEC]道路建设Building Roads kruskal求最小生成树. #include<iostream> #include<cstdio> ...

  7. [POJ2749]Building roads(2-SAT)

    Building roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8153   Accepted: 2772 De ...

  8. bzoj 1626: [Usaco2007 Dec]Building Roads 修建道路 -- 最小生成树

    1626: [Usaco2007 Dec]Building Roads 修建道路 Time Limit: 5 Sec  Memory Limit: 64 MB Description Farmer J ...

  9. 洛谷——P2872 [USACO07DEC]道路建设Building Roads

    P2872 [USACO07DEC]道路建设Building Roads 题目描述 Farmer John had just acquired several new farms! He wants ...

随机推荐

  1. Java 发红包

    使用Java实现发红包的功能. 结构: package redPocket User.java Manager.java Member.java Demo.java 思路: Manager和Membe ...

  2. Jupyter notebook中的.ipynb文件转换成python的.py文件

    转自:https://blog.csdn.net/wyr_rise/article/details/82656555 Jupyter notebook中.py与.ipynb文件的import问题   ...

  3. prisma反向代理

    概要 为什么要做 prisma 的反向代理 反向代理示例(by golang) prisma 服务 gateway 服务 整体流程 认证 反向代理 权限 总结 概要 接触 prisma 有段时间了, ...

  4. (二十三)golang--内置函数

    1.用于求长度,占多少个字节 2.内置函数new:分配内存,主要用来分配值类型,比如int.float等,其第一个实参为类型,而非值,其返回值为指向该类型的新分配的零值的指针: 3.make:用来分配 ...

  5. D3力布图绘制--节点间的多条关系连接线的方法(转)

    在项目中遇到这样的场景,在使用D3.js绘制力布图的过程中,需要在2个节点间绘制多条连接线,找到一个不错的算法,在此分享下. 效果图: HTML中要连接 <!DOCTYPE html> & ...

  6. pytorch——auto-encoders

    自动编码器的训练方法: (1)Loss function for binary inputs (2)Loss function for real-valued inputs

  7. torch_10_stackGAN-V2

    核心要点 StackGAN旨在生成高分辨率的真实图片. stackGAN-v1架构包含两个阶段:用于文本到图像的合成,阶段1GAN根据给定的文本描述绘制对象的形状和颜色,生成低分辨率图像.阶段2将阶段 ...

  8. 坑爹的京东E卡(京东E卡的正确使用方式)

      前言    今年中秋公司发了200的京东E卡(下面简称礼品卡,京东简称jd)这让喜欢在jd自营购买商品的我很是开心,    兴致勃勃打开官网,当我选好商品准备结算时却发现礼品卡无法使用.    后 ...

  9. poj-2803 Defining Moment

    Defining Moment Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 1660   Accepted: 760 De ...

  10. HDU-1760 A New Tetris Game DFS

    曾经,Lele和他姐姐最喜欢,玩得最久的游戏就是俄罗斯方块(Tetris)了. 渐渐得,Lele发觉,玩这个游戏只需要手快而已,几乎不用经过大脑思考. 所以,Lele想出一个新的玩法. Lele和姐姐 ...