HDU 6187 Destroy Walls(最大生成树-Kruskal)

Description

国王的领地被一些城市和城市之间的城墙分成若干区域,国王的城堡在 (0.62,0.63) ( 0.6 ∗ 2 , 0.6 ∗ 3 ) 处,第 i i 个城市在(xi,yi)处,第 ui u i 个城市和第 vi v i 个城市之间有一道城墙,破坏该城墙需要 wi w i 的花费,现在国王要破坏尽可能少的城墙使得他可以到达他领土的任意地方,且要求破坏城墙的总代价尽可能小

Input

多组用例,每组用例首先输入两个整数 n,m n , m 表示城市数和城墙数,之后 n n 行每行输入两个整数xi,yi表示第 i i 个城市的位置,最后m行每行三个整数 ui,vi,wi u i , v i , w i 表示 ui u i 城市和 vi v i 城市之间有一道城墙,破坏代价是 wi w i

(2n105,1m2105,|xi|,|yi|105,0wi104) ( 2 ≤ n ≤ 10 5 , 1 ≤ m ≤ 2 ⋅ 10 5 , | x i | , | y i | ≤ 10 5 , 0 ≤ w i ≤ 10 4 )

Output

对于每组用例,输出最小的破坏城墙数量以及最小代价

Sample Input

4 4
-1 -1
-1 1
1 1
1 -1
1 2 1
2 3 2
3 4 1
4 1 2

Sample Output

1 1

Solution

要想国王可以到达任意地方,这些城市构成的图不能有环,为使破坏城墙数量最少且破坏总代价最小,即要使得剩余城墙数量尽可能多,且剩余城墙破坏总代价尽可能大,也即求该图的最大生成树

Code

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=100001;
struct node
{
    int u,v,w;
    bool operator<(const node&b)const
    {
        return w>b.w;
    }
}e[2*maxn];
int f[maxn];
int find(int x)
{
    if(f[x]==x)return x;
    return f[x]=find(f[x]);
}
int main()
{
    int n,m;
    while(~scanf("%d%d",&n,&m))
    {
        int x,y,sum=0;
        for(int i=1;i<=n;i++)scanf("%d%d",&x,&y),f[i]=i;
        for(int i=0;i<m;i++)scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w),sum+=e[i].w;
        sort(e,e+m);
        int num=0,ans=0;
        for(int i=0;i<m;i++) 
        {
            int u=e[i].u,v=e[i].v,w=e[i].w;
            u=find(u),v=find(v);
            if(u==v)continue;
            num++,ans+=w,f[u]=v;
        }
        printf("%d %d\n",m-num,sum-ans);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值