• Save your cats Aizu


    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 ≤ 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


    题解:
      这个应该算是水题了吧,但我还是在别人的启发下想出来的。
      把模型抽象出来,就是一个图,让你拆一些边,使得这个图不存在环,求最小拆去边的权值和。
      从无向图拆到没有环,不就是拆成一棵树,那么肯定是最大生成树最优吧,那么我们用总权值-最大生成树的权值就可以了。

    代码:
    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <cmath>
    #include <iostream>
    #define MAXN 50100
    using namespace std;
    int x[MAXN],y[MAXN];
    struct edge{
        int from,to;double quan;
    }e[MAXN*2];
    int n,m;int fa[MAXN];
    
    int find(int x){
        if(fa[x]!=x) fa[x]=find(fa[x]);
        return fa[x];
    }
    
    double getdis(int f,int s){
        return sqrt((x[f]-x[s])*(x[f]-x[s])+(y[f]-y[s])*(y[f]-y[s]));
    }
    
    bool cmp(edge x,edge y){
        return x.quan>y.quan;
    }
    
    int main()
    {
        scanf("%d%d",&n,&m);
        for(int i=1;i<=n;i++) cin>>x[i]>>y[i];//scanf("%f%f",&x[i],&y[i]);
        double ans=0;
        for(int i=1;i<=n;i++) fa[i]=i;
        for(int i=1;i<=m;i++){
            int xx,yy;scanf("%d%d",&xx,&yy);
            double dis=getdis(xx,yy);
            e[i].from=xx,e[i].to=yy,e[i].quan=dis;
            ans+=dis;
        }
        sort(e+1,e+m+1,cmp);
        for(int i=1;i<=m;i++){
            int xx=find(e[i].from),yy=find(e[i].to);
            if(fa[xx]!=fa[yy]){
                fa[xx]=yy;
                ans-=e[i].quan;
            }
        }
        printf("%0.3f",ans);
        return 0;
    }


  • 相关阅读:
    python异常处理
    装饰器汇总
    PHP PDO预定义常量
    [转载]Firebird与MySQL:一个使用者的体会
    卸载AppDomain动态调用DLL异步线程执行失败
    c#数据库访问读取数据速度测试
    iis最大工作进程数
    WINCE 获取智能设备唯一编号
    通过 JDBC 驱动程序使用大容量复制
    IIS出现问题时修改配置文件的几项说明
  • 原文地址:https://www.cnblogs.com/renjianshige/p/7553286.html
Copyright © 2020-2023  润新知