zoukankan      html  css  js  c++  java
  • HDU 6187 Destroy Walls (对偶图最小生成树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6187

    题意:有一个V个结点M条边的带边权无向平面图,有一个人在一个区域,要拆一些墙使得他可以到达任意一个区域,问最小花费。

    解法:

    #include <bits/stdc++.h>
    using namespace std;
    const int maxn = 100010;
    const int maxm = 200010;
    struct edge{
        int u,v,w;
        edge(){}
        bool operator<(const edge &rhs) const{
            return w > rhs.w;
        }
    }edge[maxm];
    int V,E;
    namespace DSU{
        int fa[maxn];
        void init(){
            for(int i=1; i<maxn; i++) fa[i] = i;
        }
        int find_set(int x){
            if(x==fa[x]) return x;
            else return fa[x] = find_set(fa[x]);
        }
        bool union_set(int x, int y){
            x = find_set(x);
            y = find_set(y);
            if(x!=y){
                fa[x] = y;
                return 1;
            }
            return 0;
        }
    }
    using namespace DSU;
    int main()
    {
        while(~scanf("%d %d", &V,&E))
        {
            init();
            for(int i=0; i<V; i++){
                int x,y;
                scanf("%d %d", &x,&y);
            }
            long long ans = 0;
            for(int i=0; i<E; i++){
                scanf("%d %d %d", &edge[i].u, &edge[i].v, &edge[i].w);
                ans += edge[i].w;
            }
            sort(edge, edge+E);
            int cnt = 0;
            for(int i=0; i<E; i++){
                if(union_set(edge[i].u, edge[i].v)){
                    ans -= edge[i].w;
                    ++cnt;
                }
            }
            printf("%d %lld
    ", E-cnt, ans);
        }
        return 0;
    }
    
  • 相关阅读:
    redis 笔记
    经验
    增加模块-概念图
    node API buffer
    VS2010中使用CL快速 生成DLL的方法
    WIN7下VS2010中使用cl编译的步骤
    Win7下VS2010编译的程序在XP报错:找不到msvcp100d.dll或者msvcp100.dll
    C#速学
    Windows下架设SVN服务
    Redis 压力测试
  • 原文地址:https://www.cnblogs.com/spfa/p/7496195.html
Copyright © 2011-2022 走看看