Aizu - 2224-Save your cats

这篇博客讲述了Nicholas Y. Alford如何通过使用最少的神圣水来摧毁邪恶巫婆在花园中设置的魔法篱笆,救出被困的猫咪。问题转化为寻找最小生成树,确保所有猫咪所在的区域都能被连接。输入包括点的数量、篱笆的数量以及它们的位置信息,输出是最小的神圣水需求量。解决方案是利用并查集和最小生成树的概念,避免形成环路并减少神圣水的使用。
摘要由CSDN通过智能技术生成

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
xy1
.
.
.
xN yN
pq1
.
.
.
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

题解:

NY在自己的花园里养了很多猫。有一天,一个巫婆在N个点设置了魔法,然后有M条关系,每一条在两个点之间有栅栏。NY需要损坏这些栅栏但是需要栅栏长度这么多神奇的水,因为这种水很昂贵所以希望水用的越少越好。输出最少花费。

 

输入N,M表示N个点,接下来N行每行一个点的坐标,接下来M行每行两个数表示x,y之间有栅栏相连。

没有栅栏会交叉,每个圈都至少有一只猫。

 

分析:

题目意思就是如果图产生了圈就要把一些边去掉,破坏这个圈,问需要破坏的边的最小长度。

那么每次并查集的时候只要判断在同一个连通分量那么就需要破坏掉这条边,累加即可。因为不会有重边,所以

按边的权值从大到小,构成类似最小生成树,没加的边就是围成圈的最小边,即为题意重要用圣水抹去的边。

离散数学里是这么说的 对树上任意两点加一条边就会构成圈 所以使图只剩一颗树(当然图本身也可能不连通)就是我们的目标

 

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
int N,M;
int pre[10010];
struct node1{
	double x,y;
}point[10010]; 
struct node2{
	int u,v;
	double cost;
}rode[100010];
bool cmp(node2 x,node2 y){
	return x.cost>y.cost;
}
double dis(int x,int y){
   double sum;
   sum=sqrt((point[x].x-point[y].x)*(point[x].x-point[y].x)+(point[y].y-point[x].y)*(point[y].y-point[x].y));
   return sum;
}
void init(){
	for(int i=0;i<=10010;i++){
		pre[i]=i;
	}
}
int  find(int x){
	if(pre[x]==x)
	  return x;
	return pre[x]=find(pre[x]);
}
void mar(int x,int y){
	int xx=find(x);
	int yy=find(y);
	if(xx!=yy);
	pre[xx]=yy;
}
double kruskra(){
	double ans=0;
	int num=0;
	sort(rode,rode+M,cmp);
	for(int i=0;i<M;i++){
		if(find(rode[i].v)!=find(rode[i].u)){
			mar(rode[i].u,rode[i].v);
			ans=ans+rode[i].cost;
			num++;
		}
	}
	return ans;
}
int main(){
	while(cin>>N>>M){
	double sum1=0,sum2=0;
	   for(int i=1;i<=N;i++){
		  cin>>point[i].x>>point[i].y;
	   }
	    for(int i=0;i<M;i++){
		  cin>>rode[i].u>>rode[i].v;
		  rode[i].cost=dis(rode[i].u,rode[i].v);
		  sum1=rode[i].cost+sum1;
	   }
	   init();
	   sum2=kruskra();
	   printf("%.3lf\n",sum1-sum2);
   }
}
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <cstdio>
#include <cmath>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;
#define N 12000
int pre[N];
int n,m;
struct Edge
{
    int u,v;
    double len;
}edge[N*N/2];
struct Node
{
    double x,y;
}node[N];
int cmp(Edge a,Edge b)
{
    return a.len>b.len;
}
double dis(Node a,Node b)
{
    return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
 
void init(int n)
{
    for(int i=1;i<=n;i++)
    {
        pre[i]=i;
    }
}
int find(int x)
{
    if(pre[x]==x) return x;
    else return pre[x]=find(pre[x]);
}
void mix(int x,int y)
{
    int fx=find(x);
    int fy=find(y);
    if(fx!=fy)
    {
        pre[fx]=fy;
    }
}
double kruskal()
{
    init(n);
    sort(edge,edge+m,cmp);
    double s=0.0;//记录不用抹去的边,类似kruskal求最小生成树
    for(int i=0;i<m;i++)
    {
        Edge e=edge[i];
        if(find(e.u)!=find(e.v))
        {
            mix(e.u,e.v);
            s+=e.len;
        }
    }return s;
}
int main()
{
   // freopen("h,in","r",stdin);
    //freopen("h.out","w",stdout);
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        int a,b;
        for(int i=1;i<=n;i++)
        {
            scanf("%lf%lf",&node[i].x,&node[i].y);
        }
        double sum=0.0;//巫婆围得的总边和
        for(int i=0;i<m;i++)
        {
            scanf("%d%d",&a,&b);
            edge[i].u=a;
            edge[i].v=b;
            edge[i].len=dis(node[a],node[b]);
            sum+=edge[i].len;
        }
        double s=kruskal();
        double ans=0.0;
        ans=sum-s;
        printf("%.3lf\n",ans);
    }return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值