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. 【Spring】 IOC Base

    一.关于容器 1. ApplicationContext和BeanFactory 2. 配置文件 XML方式 Java-configuration 方式 @Configuration 3. 初始化容器 ...

  2. 每天学会一点点(HashMap实现原理及源码分析)

    HashMap实现原理及源码分析   哈希表(hash table)也叫散列表,是一种非常重要的数据结构,应用场景及其丰富,许多缓存技术(比如memcached)的核心其实就是在内存中维护一张大的哈希 ...

  3. vue项目集成金格WebOffice2015

    下载 官网地址:http://www.goldgrid.com/jinge_download/index.aspx?num=5 解压后的文件 js文件中有两个重要的js文件iWebOffice2015 ...

  4. 小程序 请求Promise简单封装

    最近做小程序在调用后台接口的时候感觉总写很长一串,很冗杂.非常想念vue中promise封装的写法,于是自己初步封装了一下. 1.url 接口地址 2.headers请求头 3. params 请求参 ...

  5. 虚拟化(三) -vsphere套件的安装注意及使用

    https://www.cnblogs.com/zhrngM/p/9547958.html [转]虚拟化(三):vsphere套件的安装注意及使用 vsphere套件里面主要的组件有esxi.vcen ...

  6. Day 8 面试题

    一.选择题(每题2分) #####1.1 设超级用户root当前所在目录为:/usr/local,键入cd命令后, 用户当前所在目录为(B) A:/home B:/root C:/home/root ...

  7. 关于WebApi的跨域问题

    前端调用我后端接口时出现200,跨域问题 解决方案: 在webconfig中加入以下配置就OK了 <configuration> <system.webServer> < ...

  8. asp.net core IdentityServer4 概述

    概览 现代应用程序看上去大都是这样的: 最常见的交互是: 浏览器与Web应用程序通信 Web应用程序与Web API通信(有时是独立的,有时是代表用户的) 基于浏览器的应用程序与Web API通信 本 ...

  9. 转:int整数除以int整数一定得到的是int整数(易忽视)

    版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明.本文链接:https://blog.csdn.net/u014053368/article/de ...

  10. APP功能测试要点

    1.功能性测试 根据产品需求文档编写测试用例而进行测试,包括客户端的单个功能模块以及功能业务逻辑(功能交互)如:涉及输入的地方需要考虑等价类,边界值,异常或非法等 1.1 安装与卸载测试 >软件 ...