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 ≤ xiyi ≤ 10000). The following M lines describe the both ends of the fences. Each line contains two integers pj and qj (1 ≤ pjqj ≤ 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

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

题解:
  这个应该算是水题了吧,但我还是在别人的启发下想出来的。
  把模型抽象出来,就是一个图,让你拆一些边,使得这个图不存在环,求最小拆去边的权值和。
  从无向图拆到没有环,不就是拆成一棵树,那么肯定是最大生成树最优吧,那么我们用总权值-最大生成树的权值就可以了。 代码:
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cmath>
#include <iostream>
#define MAXN 50100
using namespace std;
int x[MAXN],y[MAXN];
struct edge{
int from,to;double quan;
}e[MAXN*];
int n,m;int fa[MAXN]; int find(int x){
if(fa[x]!=x) fa[x]=find(fa[x]);
return fa[x];
} double getdis(int f,int s){
return sqrt((x[f]-x[s])*(x[f]-x[s])+(y[f]-y[s])*(y[f]-y[s]));
} bool cmp(edge x,edge y){
return x.quan>y.quan;
} int main()
{
scanf("%d%d",&n,&m);
for(int i=;i<=n;i++) cin>>x[i]>>y[i];//scanf("%f%f",&x[i],&y[i]);
double ans=;
for(int i=;i<=n;i++) fa[i]=i;
for(int i=;i<=m;i++){
int xx,yy;scanf("%d%d",&xx,&yy);
double dis=getdis(xx,yy);
e[i].from=xx,e[i].to=yy,e[i].quan=dis;
ans+=dis;
}
sort(e+,e+m+,cmp);
for(int i=;i<=m;i++){
int xx=find(e[i].from),yy=find(e[i].to);
if(fa[xx]!=fa[yy]){
fa[xx]=yy;
ans-=e[i].quan;
}
}
printf("%0.3f",ans);
return ;
}
												

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

  1. AOJ 2224 Save your cats (Kruskal)

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

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

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

  3. Aizu2224 Save your cats(最大生成树)

    https://vjudge.net/problem/Aizu-2224 场景嵌入得很好,如果不是再最小生成树专题里,我可能就想不到解法了. 对所有的边(栅栏)求最大生成树,剩下来的长度即解(也就是需 ...

  4. Aizu - 2224

    题目链接:https://vjudge.net/problem/Aizu-2224 题目大意: 先给出 N 个点的坐标(x,y),这N个点之间有且只有M条边,接下来给出 M 条边的两端点,每条边对应的 ...

  5. Aizu:2224-Save your cats

    Save your cats Time limit 8000 ms Memory limit 131072 kB Problem Description Nicholas Y. Alford was ...

  6. Aizu-2224Save your cats并查集+最小生成树

    Save your cats 题意:存在n个点,有m条边( input中读入的是 边的端点,要先转化为边的长度 ),做一个最小生成树,使得要去除的边的长度总和最小: 思路:利用并查集和求最小生成树的方 ...

  7. AOJ - 2224 Save your cat(最小生成树)

    http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=45524 NY在自己的花园里养了很多猫.有一天,一个巫婆在N个点设置了魔法,然 ...

  8. [POJ 3735] Training little cats (结构矩阵、矩阵高速功率)

    Training little cats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9613   Accepted: 2 ...

  9. Training little cats poj3735

    Training little cats Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9299   Accepted: 2 ...

随机推荐

  1. 【1】KNN(K-nearest neighbors algorithm)

    基本原理 KNN算法又叫最近邻居法,是一种非常简单易于掌握的分类算法. 其基本原理是,存在一个已知标签的数据集合,也就是训练样本集. 这个样本集中的每一个数据所属的分类都是已知的. 当一个没有标签的新 ...

  2. Go操作kafka

    Kafka是一种高吞吐量的分布式发布订阅消息系统,它可以处理消费者规模的网站中的所有动作流数据,具有高性能.持久化.多副本备份.横向扩展等特点.本文介绍了如何使用Go语言发送和接收kafka消息. s ...

  3. C# Post Get 方式发送请求

    httpPost 方式发送请求 不带参数 /// <summary> /// 没有参数的post请求 /// </summary> public void HttpPostNo ...

  4. apache ignite系列(九):ignite调优

    1,配置文件调优 1.1 设置页面大小(pagesize) 先查看系统pagesiz,使用PAGE_SIZE或者PAGESIZE # getconf PAGE_SIZE 4096 # getconf ...

  5. Hessian 接口使用示例总结(转载)

    一.使用hessian接口准备 首先,hessian接口的使用,必须要准备hessian接口的jar包,本文使用的jar包如下:hessian-4.0.7.jar; Hessian接口的使用一般是在两 ...

  6. HTML 框架导航

    初次学习HTML,在www.w3school.com.cn看到了框架导航,上面的例子没有看懂所以搜了一下相应的问题,最后弄懂了怎么实现同一界面下的框架导航. 首先是www.w3school.com.c ...

  7. 花果山第一届猿类分级考试实录--Talk is cheap,Show me the code

    本故事纯属虚构,如有雷同,纯属巧合! 故事背景 悟空师徒4人取经回来后,因不耐收到管教,就回到了花果山,带领一帮猴子猴孙逍遥自在的过日子,奈何因在阎王殿里将生死薄中的猴子猴孙的名字都划去了,猴子猴孙是 ...

  8. 微信小程序商城构建全栈应用 Thinkphp5

    课程——微信小程序商城构建全栈应用[目录]第1章 前言:不同的时代,不同的Web第2章 环境,工具与准备工作第3章 模块,路由与获取请求参数第4章 构建验证层第5章 REST与RESTFul第6章 A ...

  9. RocksDB线程局部缓存

    概述 在开发过程中,我们经常会遇到并发问题,解决并发问题通常的方法是加锁保护,比如常用的spinlock,mutex或者rwlock,当然也可以采用无锁编程,对实现要求就比较高了.对于任何一个共享变量 ...

  10. Python 踩坑之旅文件系统篇其一文件夹也是个文件

    目录 1.1 案例 1.2 分析 1.3 扩展 1.4 技术关键字 下期预告 代码示例支持 平台: Mac OS Python: 2.7.10 代码示例: - wx: 菜单 - Python踩坑指南代 ...