Save your cats

Time limit 8000 ms

Memory limit 131072 kB

Problem Description

Nicholas Y. Alford was a cat lover. He had a garden in a village and kept many cats in his garden. The cats were so cute that people in the village also loved them.

One day, an evil witch visited the village. She envied the cats for being loved by everyone. She drove magical piles in his garden and enclosed the cats with magical fences running between the piles. She said “Your cats are shut away in the fences until they become ugly old cats.” like a curse and went away.

Nicholas tried to break the fences with a hummer, but the fences are impregnable against his effort. He went to a church and asked a priest help. The priest looked for how to destroy the magical fences in books and found they could be destroyed by holy water. The Required amount of the holy water to destroy a fence was proportional to the length of the fence. The holy water was, however, fairly expensive. So he decided to buy exactly the minimum amount of the holy water required to save all his cats. How much holy water would be required?

Input

The input has the following format:

N M

x1 y1

.

.

.

xN yN

p1 q1

.

.

.

pM qM

The first line of the input contains two integers N (2 ≤ N ≤ 10000) and M (1 ≤ M). N indicates the number of magical piles and M indicates the number of magical fences. The following N lines describe the coordinates of the piles. Each line contains two integers xi and yi (-10000 ≤ xi, yi ≤ 10000). The following M lines describe the both ends of the fences. Each line contains two integers pj and qj (1 ≤ pj, qj ≤ N). It indicates a fence runs between the pj-th pile and the qj-th pile.

You can assume the following:

No Piles have the same coordinates.
A pile doesn’t lie on the middle of fence.
No Fences cross each other.
There is at least one cat in each enclosed area.
It is impossible to destroy a fence partially.
A unit of holy water is required to destroy a unit length of magical fence.

Output

Output a line containing the minimum amount of the holy water required to save all his cats. Your program may output an arbitrary number of digits after the decimal point. However, the absolute error should be 0.001 or less.

Sample Input 1

3 3

0 0

3 0

0 4

1 2

2 3

3 1

Example

Output for the Sample Input 1

3.000

Sample Input 2

4 3

0 0

-100 0

100 0

0 100

1 2

1 3

1 4

Output for the Sample Input 2

0.000

Sample Input 3

6 7

2 0

6 0

8 2

6 3

0 5

1 7

1 2

2 3

3 4

4 1

5 1

5 4

5 6

Output for the Sample Input 3

7.236

Sample Input 4

6 6

0 0

0 1

1 0

30 0

0 40

30 40

1 2

2 3

3 1

4 5

5 6

6 4

Output for the Sample Input 4

31.000


解题心得:

  1. 很有意思的一个题,说是一个人养了很多的猫,巫师看不惯,用很多桩子和墙将这些猫全部围困了起来(每个墙的起点和终点都在桩子上,每堵墙不交叉,每个围起来的格子里面都有猫),每个桩子有一个坐标,你需要用圣水去将墙消融,将猫放出来,每单位长度墙要用一单位圣水,问最少需要多少圣水。
  2. 首先要明白要将所有的猫都放出来就不能有格子,怎么看有没有格子(也就是环),使用并查集,然后要转换一个思想,那就是不能想怎么去拆除墙,因为拆除墙是没有办法倒着来使用并查集的。可以先得到所有的墙的长度的总和,然后建立一个最”大“生成树,用所有墙长度的总和减去建立最大生成树使用的长度就是需要拆除的长度。

#include <math.h>
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1e6+100; struct Pile{
int x,y;
}p[maxn]; struct Fence{
double len;
int s,e; bool operator < (const Fence &a) const {
return a.len < len;
}
}f[maxn]; int n,m,father[maxn];
double tot; double get_len(int x,int y){
double x1 = p[x].x;
double y1 = p[x].y;
double x2 = p[y].x;
double y2 = p[y].y; double len = (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2);
len = sqrt(len); return len;
} void init() {
tot = 0;
for(int i=1;i<=n;i++)
father[i] = i;
for(int i=1;i<=n;i++)
scanf("%d%d",&p[i].x,&p[i].y);
for(int i=0;i<m;i++) {
scanf("%d%d", &f[i].s, &f[i].e);
f[i].len = get_len(f[i].s,f[i].e);
tot += f[i].len;
}
sort(f,f+m);
} int find(int x) {
if(x == father[x])
return x;
return father[x] = find(father[x]);
} void merge(int x,int y){
int fx = find(x);
int fy = find(y);
father[fx] = fy;
} void build_tree() {
double add = 0;
for(int i=0;i<m;i++) {
if(find(f[i].s) != find(f[i].e)) {
add += f[i].len;
merge(f[i].e,f[i].s);
}
}
printf("%.3f\n",tot-add);
} int main() {
while(scanf("%d%d",&n,&m) != EOF) {
init();
build_tree();
}
return 0;
}

Aizu:2224-Save your cats的更多相关文章

  1. AOJ 2224 Save your cats (Kruskal)

    题意:给出一个图,去除每条边的花费为边的长度,求用最少的花费去除部分边使得图中无圈. 思路:先将所有的边长加起来,然后减去最大生成树,即得出最小需要破坏的篱笆长度. #include <cstd ...

  2. AOJ 2224 Save your cats( 最小生成树 )

    链接:传送门 题意:有个女巫把猫全部抓走放在一个由 n 个木桩(xi,yi),m 个篱笆(起点终点木桩的编号)围成的法术领域内,我们必须用圣水才能将篱笆打开,然而圣水非常贵,所以我们尽量想降低花费来解 ...

  3. Save your cats Aizu - 2224

    Nicholas Y. Alford was a cat lover. He had a garden in a village and kept many cats in his garden. T ...

  4. Django:form.save()方法

    参考:https://blog.csdn.net/it_yuan/article/details/53580756 背景: 之前的博客是不支持上传文章缩略图的,后来新增了此功能,但是发现修改老的文章时 ...

  5. Django:model.save()的时候在干什么

    转:https://www.cnblogs.com/zywscq/p/5397439.html Model.save(force_insert=False, force_update=False, u ...

  6. loadrunner在win10破解提示:Cannot save the license information because acceses to the registry is denied的解决办法

    方法1 下图1-1中提示就是说明了破解的时候权限不足导致,解决办法就是使用管理员权限打开loadrunner破解就好了,但是右键“以管理员身份运行”选项打开loadrunner又是会提示1-2中的问题 ...

  7. Aizu:0009- Prime Number

    Prime Number Time limit 1000 ms Memory limit 131072 kB Problem Description Write a program which rea ...

  8. Aizu:0005-GCD and LCM

    GCD and LCM Time limit 1000 ms Memory limit 131072 kB Problem Description Write a program which comp ...

  9. Aizu:2170-Marked Ancestor

    Marked Ancestor Time limit 8000 ms Memory limit 131072 kB Problem Description You are given a tree T ...

随机推荐

  1. 多个ModelForm组合成一个表单

    打个比方: 我将用户的基本信息 如用户名密码存在继承了Django auth认证组件中的 AbstractUser 类的模型中,并和第二个存了Details模型中,此模型继承UserInfo模型 继承 ...

  2. MySQL(五)

    一.视图 视图是一个虚拟表(非真实存在),其本质是根据SQL语句获取动态的数据集,并为其命名,用户使用时只需使用名称即可获取结果集,可以将该结果集当做表来使用. 使用视图我们可以把查询过程中的临时表摘 ...

  3. CSS基础必备盒模型及清除浮动

    盒模型 盒模型是有两种标准的,一个是标准模型,一个是IE模型. css如何设置两种模型 这里用到了CSS3 的属性 box-sizing /* 标准模型 */ box-sizing:content-b ...

  4. vue2.0高仿饿了么better-scroll

    首先安装better-scroll npm i better-scroll -S goods页面模板 <template> <div class="goods"& ...

  5. C++基础--sizeof和strlen的区别

    首先,来运行一段程序: #include "stdafx.h" #include <stdio.h> #include <string.h> int mai ...

  6. isee图片专家批量处理图片大小教程

    经常用手机.照相机外出拍照片,然后再弄到电脑上面很占硬盘空间了,isee图片专家是一款非常专业的批量压缩图片大小工具,方便储存,给电脑减压,具有一次自动处理N张图片:程序小巧,资源占用低,处理速度快等 ...

  7. 洛谷 P1509 找啊找啊找GF

    题目背景 "找啊找啊找GF,找到一个好GF,吃顿饭啊拉拉手,你是我的好GF.再见." "诶,别再见啊..." 七夕...七夕...七夕这个日子,对于sqybi这 ...

  8. CRM WebUI and Hybris的Product页面标题实现

    CRM Controller只需实现IF_BSP_WD_HISTORY_STATE_DESCR~GET_STATE_DESCRIPTION方法: 上图在ABAP调试器里观察到的这个字符即出现在最终页面 ...

  9. 委托代码func和Action的基本用法

    这是微软简化后的委托写法,其中,func适合带返回参数的方法调用,action适合没有返回参数的方法调用 FUNC方法代码: public string GetPeopleInfo(string na ...

  10. 【BZOJ4487】[JSOI2015] 染色问题(高维容斥)

    点此看题面 大致题意: 有一个\(n*m\)的矩形,先让你用\(C\)种颜色给它染色.每个格子可染色可不染色,但要求每行每列至少有一个小方格被染色,且每种颜色至少出现一次.求方案数. 高维容斥 显然题 ...