Aizu:2224-Save your cats

Save your cats

Time limit 8000 ms
Memory limit 131072 kB

Problem Description

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

Example

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


解题心得:

  1. 很有意思的一个题,说是一个人养了很多的猫,巫师看不惯,用很多桩子和墙将这些猫全部围困了起来(每个墙的起点和终点都在桩子上,每堵墙不交叉,每个围起来的格子里面都有猫),每个桩子有一个坐标,你需要用圣水去将墙消融,将猫放出来,每单位长度墙要用一单位圣水,问最少需要多少圣水。
  2. 首先要明白要将所有的猫都放出来就不能有格子,怎么看有没有格子(也就是环),使用并查集,然后要转换一个思想,那就是不能想怎么去拆除墙,因为拆除墙是没有办法倒着来使用并查集的。可以先得到所有的墙的长度的总和,然后建立一个最”大“生成树,用所有墙长度的总和减去建立最大生成树使用的长度就是需要拆除的长度。

#include <math.h>
#include <stdio.h>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn = 1e6+100;

struct Pile{
    int x,y;
}p[maxn];

struct Fence{
    double len;
    int s,e;

    bool operator < (const Fence &a) const {
        return a.len < len;
    }
}f[maxn];

int n,m,father[maxn];
double tot;

double get_len(int x,int y){
    double x1 = p[x].x;
    double y1 = p[x].y;
    double x2 = p[y].x;
    double y2 = p[y].y;

    double len = (x1-x2)*(x1-x2) + (y1-y2)*(y1-y2);
    len = sqrt(len);

    return len;
}

void init() {
    tot = 0;
    for(int i=1;i<=n;i++)
        father[i] = i;
    for(int i=1;i<=n;i++)
        scanf("%d%d",&p[i].x,&p[i].y);
    for(int i=0;i<m;i++) {
        scanf("%d%d", &f[i].s, &f[i].e);
        f[i].len = get_len(f[i].s,f[i].e);
        tot += f[i].len;
    }
    sort(f,f+m);
}

int find(int x) {
    if(x == father[x])
        return x;
    return father[x] = find(father[x]);
}

void merge(int x,int y){
    int fx = find(x);
    int fy = find(y);
    father[fx] = fy;
}

void build_tree() {
    double add = 0;
    for(int i=0;i<m;i++) {
        if(find(f[i].s) != find(f[i].e)) {
            add += f[i].len;
            merge(f[i].e,f[i].s);
        }
    }
    printf("%.3f\n",tot-add);
}

int main() {
    while(scanf("%d%d",&n,&m) != EOF) {
        init();
        build_tree();
    }
    return 0;
}
本项目是一个基于SSM(Spring+SpringMVC+MyBatis)后端框架与Vue.js前端框架开发的疫情居家办公系统。该系统旨在为居家办公的员工提供一个高效、便捷的工作环境,同时帮助企业更好地管理远程工作流程。项目包含了完整的数据库设计、前后端代码实现以及详细的文档说明,非常适合计算机相关专业的毕设学生和需要进行项目实战练习的Java学习者。 系统的核心功能包括用户管理、任务分配、进度跟踪、文件共享和在线沟通等。用户管理模块允许管理员创建和管理用户账户,分配不同的权限。任务分配模块使项目经理能够轻松地分配任务给团队成员,并设置截止日期。进度跟踪模块允许员工实时更新他们的工作状态,确保项目按计划进行。文件共享模块提供了一个安全的平台,让团队成员可以共享和协作处理文档。在线沟通模块则支持即时消息和视频会议,以增强团队之间的沟通效率。 技术栈方面,后端采用了Spring框架来管理业务逻辑,SpringMVC用于构建Web应用程序,MyBatis作为ORM框架简化数据库操作。前端则使用Vue.js来实现动态用户界面,搭配Vue Router进行页面导航,以及Vuex进行状态管理。数据库选用MySQL,确保数据的安全性和可靠性。 该项目不仅提供了一个完整的技术实现示例,还为开发者留下了扩展和改进的空间,可以根据实际需求添加新功能或优化现有功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值