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. js 大量数据优化,通用方法

    当页面渲染太多标签时,会出现卡顿的,典型就是类似table数据太多时,非常卡顿.如果选择分页,没必要讨论,这儿只讨论采用滚动的情况.解决思路很简单,就是页面不展示出来的元素,从页面上删除掉,最难点在于 ...

  2. 【Offer】[38] 【字符串的排列】

    题目描述 思路分析 测试用例 Java代码 代码链接 题目描述 输入一个字符串,打印出该字符串中字符的所有排列.例如,输入字符串abc,则打印出由字符a.b.c所能排列出来的所有字符串abc.acb. ...

  3. Day004作业

    1,写代码,有如下列列表,按照要求实现每⼀一个功能li = ["alex", "WuSir", "ritian", "barry& ...

  4. Intellij IDEA使用restclient测试

    Intellij  IDEA内置了restclient来帮忙我们测试我们的后台代码,让我们可以脱离第三方工具测试,也更不需要我们编写前端代码,直接让我们能想网站发送get,post,put,delet ...

  5. Android Q 正式命名为 Android 10

    根据官方博文,谷歌已经公布了 Android Q 的名称,它并不是想以前一样,以甜食命名,也不是以任何以字母 Q 开头来命名,而是简单称它为 Android 10. 该公司表示,它正在改变其发布版本的 ...

  6. Admiral(双向BFS + Hash)

    Problem Description Suppose that you are an admiral of a famous naval troop. Our naval forces have g ...

  7. Win10家庭版安装Docker

    1.下载Docker Toolbox 下载地址:http://mirrors.aliyun.com/docker-toolbox/windows/docker-toolbox/ 2.安装Docker ...

  8. 品Spring:能工巧匠们对注解的“加持”

    问题的描述与方案的提出 在Spring从XML转向注解时,为了自身的开发方便,对注解含义进行了扩充(具体参考本号上一篇文章). 这个扩充直接导致了一个问题,就是需要从注解往元注解以及元元注解(即沿着从 ...

  9. C++:Memory Management

    浅谈C++内存管理 new和delete 在C++中,我们习惯用new申请堆中的内存,配套地,使用delete释放内存. class LiF; LiF* lif = new LiF(); // 分配内 ...

  10. Python学习笔记整理总结【网络编程】【线程/进程/协程/IO多路模型/select/poll/epoll/selector】

    一.socket(单链接) 1.socket:应用层与TCP/IP协议族通信的中间软件抽象层,它是一组接口.在设计模式中,Socket其实就是一个门面模式,它把复杂的TCP/IP协议族隐藏在Socke ...