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



    **链接:****传送门 **

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

    思路:这张图的生成树相当于这个领域的“框架”,总的周长是一定的,只需要让“框架”最长那么其他篱笆(解救猫咪“开口”的地方)也就最短,只需要把边权处理成负数然后做最小生成树,最后 周长 + 最小生成树的路径长 即为答案


    /*************************************************************************
        > File Name: aoj2224.cpp
        > Author:    WArobot 
        > Blog:      http://www.cnblogs.com/WArobot/ 
        > Created Time: 2017年06月19日 星期一 19时52分17秒
     ************************************************************************/
    
    #include<bits/stdc++.h>
    using namespace std;
    
    const int MAX_N = 10010;
    const int MAX_M = 200000;
    struct point{
    	int x, y;
    }P[MAX_N];
    struct edge{
    	int from , to;
    	double cost;
    }E[MAX_M];
    
    int n , m;
    int par[MAX_N];
    
    void init_union_find_set()	{ for(int i = 0 ; 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 union_set(int x,int y) { x = find(x); y = find(y); if(x!=y) par[y] = x; }
    
    bool cmp(edge a,edge b){
    	return a.cost < b.cost;
    }
    double Kruskal(){
    	init_union_find_set();
    	sort(E,E+m,cmp);
    	double ret = 0;
    	for(int i = 0 ; i < m ; i++){
    		if( !same(E[i].from,E[i].to) ){
    			union_set(E[i].from,E[i].to);
    			ret += E[i].cost;
    		}
    	}
    	return ret;
    }
    
    double Distence(point a,point b){
    	return sqrt( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) );
    }
    
    int main(){
    	int from , to;
    	double cost , sum;
    	while(~scanf("%d%d",&n,&m)){
    		sum = 0;
    		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",&E[i].from , &E[i].to);
    			E[i].cost = Distence( P[E[i].from] , P[E[i].to] ) * -1;
    			sum += ( E[i].cost * -1 );
    		}
    		double ret = Kruskal();
    		printf("%.3lf
    ",sum + ret);
    	}
    	return 0;
    }
  • 相关阅读:
    逆序对的相关问题:bzoj1831,bzoj2431
    bzoj3211,bzoj3038
    hdu 1179最大匹配
    hdu 3038带权并查集
    poj 1733离散化(map)+并查集
    codeforces 369B
    poj 1456
    POJ 1988相对偏移
    poj 1986tarjan模板题
    poj 1330lca模板题离线算法
  • 原文地址:https://www.cnblogs.com/WArobot/p/7050398.html
Copyright © 2020-2023  润新知