题意:给出一个图,去除每条边的花费为边的长度,求用最少的花费去除部分边使得图中无圈。

思路:先将所有的边长加起来,然后减去最大生成树,即得出最小需要破坏的篱笆长度。

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std; int N, M; // 桩数量,篱笆数量
int par[10005];
void init() {
for (int i = 1; i <= N; ++i) par[i] = i;
}
int find(int x) {
return x == par[x] ? x : par[x] = find(par[x]);
}
bool same(int x, int y) {
return find(x) == find(y);
}
void unite(int x, int y) {
x = find(x);
y = find(y);
if (x != y) par[x] = y;
} struct point{
int x, y;
} ps[10005]; double dist(point &p1, point &p2) {
return sqrt((p1.x - p2.x)*(p1.x - p2.x) + (p1.y - p2.y)*(p1.y - p2.y));
} struct edge {
int from, to;
double cost;
edge(int from, int to, double cost) : from(from), to(to), cost(cost) {}
bool operator<(const edge &b) const { // 从大到小排序,求出最大生成树
return cost > b.cost;
}
}; vector<edge> es; // 边集
double ans = 0.0; // 答案 void kruskal() {
init();
sort(es.begin(), es.end());
for (auto it : es) { // C++11
if (!same(it.from, it.to)) {
unite(it.from, it.to);
ans -= it.cost; // 减去最大生成树的边即可
}
}
} void solve() {
kruskal();
printf("%.3lf\n", ans);
} int main()
{
cin >> N >> M;
for (int i = 1; i <= N; ++i)
cin >> ps[i].x >> ps[i].y;
int u, v;
double d;
for (int i = 0; i < M; ++i) {
cin >> u >> v;
d = dist(ps[u], ps[v]);
es.push_back(edge(u, v, d));
ans += d; // 求出所有路径和
}
solve();
return 0;
}

AOJ 2224 Save your cats (Kruskal)的更多相关文章

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

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

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

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

  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. Aizu2224 Save your cats(最大生成树)

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

  5. ProgrammingContestChallengeBook

    POJ 1852 Ants POJ 2386 Lake Counting POJ 1979 Red and Black AOJ 0118 Property Distribution AOJ 0333 ...

  6. Aizu:2224-Save your cats

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

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

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

  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. 异常处理com.sun.image.codec.jpeg.JPEGImageEncoder

    以下方案不一定能解决问题.解决方案:配置JDK的JRE_HOME 环境变量既可. Linux 下配置  : vi /etc/profile 在profile文件下面追加写入下面信息: export J ...

  2. mipsel汇编指令学习

    MIPS汇编语言基础 MIPS的系统结构及设计理念比较先进,其指令系统经过通用处理器指令体系MIPS I.MIPS II.MIPS III.MIPS IV.MIPS V,以及嵌入式指令体系MIPS16 ...

  3. Linux - rar 压缩

    Linux - rar yum -y install libstdc++.so. wget http://rarsoft.com/rar/rarlinux-4.0.1.tar.gz cd rar ma ...

  4. QWidget窗口类

    import sys from PyQt5.QtWidgets import QWidget, QApplication,QPushButton from PyQt5.QtGui import QIc ...

  5. 第17月第28天 python yield

    1. class Fab(object): def __init__(self, max): self.max = max self.n, self.a, self.b = 0, 0, 1 def _ ...

  6. cetus系列~ 继续分析

    一 简介:我们来继续探讨cetus的细节问题 二 命令 1 select help 查看帮助  2 select * from backends 查看后端列表  3 select conn_detai ...

  7. mysql 架构 ~ PXC5.7.20安装尝试

    简介:今天来尝试下 pxc 5.7.20安装1 环境安装   yum install -y git scons gcc gcc-c++ openssl check cmake bison boost- ...

  8. mysql 查询优化~sql优化通用

    一 简介:今天我们来探讨下SQL语句的优化基础 二 基础规则: 一 通用: 1 避免索引字段使用函数     2 避免发生隐式转换     3 order by字段需要走索引,否则会发生filesor ...

  9. java Comparable 和 Comparator接口区别

    Comparable 简介 Comparable 是排序接口. 若一个类实现了Comparable接口,就意味着“该类支持排序”.  即然实现Comparable接口的类支持排序,假设现在存在“实现C ...

  10. Shiro的三种授权(十二)

    前提就是在Realm的授权方法中查询出权限并返回List<String>形式 @Override protected AuthorizationInfo doGetAuthorizatio ...