几何 + 思路构造 + 高斯消元 Destroy Walls HDU - 6187

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/a1781842634/article/details/79967093
#include <stdio.h>
#include <vector>
#include <iostream>
#include <string.h>
#include <algorithm>
using namespace std;
#define LL long long

const int MAXV=100000+3;
const int MAXE=200000+3;

struct Edge
{
    int from, to, cost;
    Edge(int f=0, int t=0, int c=0):from(f), to(t), cost(c){}
    bool operator < (const Edge &other)const
    {
        return cost>other.cost;
    }
}edge[MAXE];

int V, E;
int par[MAXV], high[MAXV];

int findfather(int x)
{
    return par[x]=par[x]==x?x:findfather(par[x]);
}

bool unite(int a, int b)
{
    int fa=findfather(a), fb=findfather(b);
    if(fa==fb)
        return false;
    if(high[fa]>high[fb])
        par[fa]=fb;
    else
    {
        par[fb]=fa;
        if(high[fa]==high[fb])
            ++high[fb];
    }
    return true;
}

void init()//初始化
{
    for(int i=0;i<=V;++i)
    {
        par[i]=i;
        high[i]=0;
    }
}

int main()
{
    while(~scanf("%d%d", &V, &E))
    {
        init();
        for(int i=0;i<V;++i)
        {
            int x, y;
            scanf("%d%d", &x, &y);
        }
        LL ans=0;
        for(int i=0;i<E;++i)
        {
            scanf("%d%d%d", &edge[i].from, &edge[i].to, &edge[i].cost);
            ans+=edge[i].cost;
        }
        sort(edge, edge+E);
        int cnt=0;//保留的边数
        for(int i=0;i<E;++i)
            if(unite(edge[i].from, edge[i].to))
            {
                ans-=edge[i].cost;
                ++cnt;
            }
        printf("%d %lld\n", E-cnt, ans);
    }

    return 0;
}

阅读更多

没有更多推荐了,返回首页