AOJ - 2224 Save your cat(最小生成树)
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=45524
NY在自己的花园里养了很多猫。有一天,一个巫婆在N个点设置了魔法,然后有M条关系,每一条在两个点之间有栅栏。
NY需要损坏这些栅栏但是需要栅栏长度这么多神奇的水,因为这种水很昂贵所以希望水用的越少越好。输出最少花费。
输入N,M表示N个点,接下来N行每行一个点的坐标,接下来M行每行两个数表示x,y之间有栅栏相连。
没有栅栏会交叉,每个圈都至少有一只猫。
题目意思就是如果图产生了圈就要把一些边去掉,破坏这个圈,问需要破坏的边的最小长度。
那么每次并查集的时候只要判断在同一个连通分量那么就需要破坏掉这条边,累加即可。因为不会有重边,所以
按边的权值从大到小或者从小到大都可以。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std; struct node
{
int x,y,id;
};
struct edge
{
int u,v;
double cost;
edge() {}
edge(int x,int y,double z)
{
u=x;
v=y;
cost=z;
}
bool operator <(const edge& a) const
{
return cost>a.cost;
}
}; edge es[];
int par[];
node p[];
int n,m;
void init()
{
for(int i=;i<=n;i++) par[i]=i;
} int find(int x)
{
return x==par[x]?x:par[x]=find(par[x]);
} void unite(int x,int y)
{
x=find(x);
y=find(y);
if(x!=y) par[x]=y;
} double dis(int a,int b)
{
return sqrt(1.0*(p[a].x-p[b].x)*(p[a].x-p[b].x)+1.0*(p[a].y-p[b].y)*(p[a].y-p[b].y));
} double kruskal()
{
sort(es,es+m);
//for(int i=0;i<m;i++) printf("%d %d %lf\n",es[i].u,es[i].v,es[i].cost);
double s=;
for(int i=;i<m;i++)
{
edge e=es[i];
if(find(e.u)!=find(e.v))
{
unite(e.u,e.v);
}
else
{
s+=e.cost;
}
}
return s;
}
int main()
{
//freopen("a.txt","r",stdin);
int a,b;
double c,sum;
while(~scanf("%d%d",&n,&m))
{
init();
sum=;
for(int i=;i<=n;i++)
{
scanf("%d%d",&p[i].x,&p[i].y);
}
for(int i=;i<m;i++)
{
scanf("%d%d",&a,&b);
c=dis(a,b);
es[i]=edge(a,b,c);
}
printf("%.3lf\n",kruskal());
}
return ;
}
AOJ - 2224 Save your cat(最小生成树)的更多相关文章
- AOJ 2224 Save your cats( 最小生成树 )
链接:传送门 题意:有个女巫把猫全部抓走放在一个由 n 个木桩(xi,yi),m 个篱笆(起点终点木桩的编号)围成的法术领域内,我们必须用圣水才能将篱笆打开,然而圣水非常贵,所以我们尽量想降低花费来解 ...
- AOJ 2224 Save your cats (Kruskal)
题意:给出一个图,去除每条边的花费为边的长度,求用最少的花费去除部分边使得图中无圈. 思路:先将所有的边长加起来,然后减去最大生成树,即得出最小需要破坏的篱笆长度. #include <cstd ...
- ProgrammingContestChallengeBook
POJ 1852 Ants POJ 2386 Lake Counting POJ 1979 Red and Black AOJ 0118 Property Distribution AOJ 0333 ...
- 从一次异常中浅谈Hibernate的flush机制
摘自http://www.niwozhi.net/demo_c70_i1482.html http://blog.itpub.net/1586/viewspace-829613/ 这是在一次事务提交时 ...
- systemtap-与 oracle 转
https://baoz.net/using-systemtap/ http://nanxiao.me/category/%E6%8A%80%E6%9C%AF/systemtap-%E7%AC%94% ...
- inst_for_mysql5.7.sh
#!/bin/bash # Author: wangshenjin<wangshenjin233@foxmail.com> # Description: install percona-s ...
- RHCSA 第八天
1.查询ip的几种方式: ip, ifconfig, nmcli,nmtui 2.nmcli命令使用: a.在ens160网卡上新建连接static_con,并配置静态ip b.在ens160网卡上新 ...
- 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 ...
- 【转】Android Canvas的save(),saveLayer()和restore()浅谈
Android Canvas的save(),saveLayer()和restore()浅谈 时间:2014-12-04 19:35:22 阅读:1445 评论:0 收藏: ...
随机推荐
- HDU 5693 D Game 区间dp
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5693 题解: 一种朴实的想法是枚举选择可以删除的两个或三个数(其他的大于三的数都能凑成2和3的和), ...
- hdu 4240 Route Redundancy 最大流
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4240 A city is made up exclusively of one-way steets. ...
- 剑指offer--21题
#include "stdafx.h" #include<iostream>using namespace std; void LeftRotateString(cha ...
- 关于IE下AJAX的问题探讨
今天JS练手的时候,想封装一个发送AJAX请求的对象,当然,是想要兼容全浏览器的.代码如下: var Ajax = { xhr: null, callback: null, XMLHttp: func ...
- 常用正则表达式(匹配URL/email/number)
var URL_REGEXP = /^(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/ ...
- 理解ASP.NET MVC Framework Action Filters
原文:http://www.cnblogs.com/darkdawn/archive/2009/03/13/1410477.html 本指南主要解释action filters,action filt ...
- uva 10564
Problem FPaths through the HourglassInput: Standard Input Output: Standard Output Time Limit: 2 Seco ...
- Content Providers详解
今天仔细阅读了一遍Content Providers的官方API文档,总结了一下Android中Content Providers的用法. 各种类型的Content Provider对一个结构化的数据 ...
- SQL技术内幕-12 SQL优化方法论前言
我推荐的一种使用自顶向下的优化论.这种方法,首先分析实例级的等待时间,在通过一系列步骤将其不断细化,知道找出系统中导致大量等待的进程/组件.一旦找出这些令人讨厌的进程,就可以集中优化他们了,一下是这种 ...
- UVA 10892 LCM Cardinality 数学
A pair of numbers has a unique LCM but a single number can be the LCM of more than one possiblepairs ...