Problem C:
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
.
.
.
xNyN
pq1
.
.
.
pMqM
NNMMNMNxiyixiyiMpjqjpjqjNpjqj-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
我挺不喜欢写这些文字的,只好复制其他人的了 = = ~
https://blog.csdn.net/qq_33406883/article/details/52074736
题意:
NY在自己的花园里养了很多猫。有一天,一个巫婆在N个点设置了魔法,然后有M条关系,每一条在两个点之间有栅栏。NY需要损坏这些栅栏但是需要栅栏长度这么多神奇的水,因为这种水很昂贵所以希望水用的越少越好。输出最少花费。
输入N,M表示N个点,接下来N行每行一个点的坐标,接下来M行每行两个数表示x,y之间有栅栏相连。
没有栅栏会交叉,每个圈都至少有一只猫。
分析:
题目意思就是如果图产生了圈就要把一些边去掉,破坏这个圈,问需要破坏的边的最小长度。
那么每次并查集的时候只要判断在同一个连通分量那么就需要破坏掉这条边,累加即可。因为不会有重边,所以
按边的权值从大到小,构成类似最小生成树,没加的边就是围成圈的最小边,即为题意重要用圣水抹去的边。
离散数学里是这么说的 对树上任意两点加一条边就会构成圈 所以使图只剩一颗树(当然图本身也可能不连通)就是我们的目标
#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; }