HDU 6187 Destroy Walls(并查集+最大生成树)

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

 

Problem Description

Long times ago, there are beautiful historic walls in the city. These walls divide the city into many parts of area.

Since it was not convenient, the new king wants to destroy some of these walls, so he can arrive anywhere from his castle. We assume that his castle locates at (0.6∗2√,0.6∗3√) .

There are n towers in the city, which numbered from 1 to n. The ith's location is (xi,yi) . Also, there are m walls connecting the towers. Specifically, the ith wall connects the tower ui and the tower vi (including the endpoint). The cost of destroying the ith wall is wi .

Now the king asks you to help him to divide the city. Firstly, the king wants to destroy as less walls as possible, and in addition, he wants to make the cost least.

The walls only intersect at the endpoint. It is guaranteed that no walls connects the same tower and no 2 walls connects the same pair of towers. Thait is to say, the given graph formed by the walls and towers doesn't contain any multiple edges or self-loops.

Initially, you should tell the king how many walls he should destroy at least to achieve his goal, and the minimal cost under this condition.

 

 

Input

There are several test cases.

For each test case:

The first line contains 2 integer n, m.

Then next n lines describe the coordinates of the points.

Each line contains 2 integers xi,yi .

Then m lines follow, the ith line contains 3 integers ui,vi,wi

|xi|,|yi|≤105

3≤n≤100000,1≤m≤200000

1≤ui,vi≤n,ui≠vi,0≤wi≤10000

 

 

Output

For each test case outout one line with 2 integers sperate by a space, indicate how many walls the king should destroy at least to achieve his goal, and the minimal cost under this condition.

 

 

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

 

 

题意:有几个城楼和连接城楼的城墙,先求要使国王城堡能够随意的到达呵任何城楼。

那么城楼相互之间一定没有环。

我们知道,如果一个平面上一个点可以到达平面的任何地方,这个平面是一定没有封闭的区域的。

什么叫没有封闭的区域?就是没有环。

既然没有环,又需要减少最少的边,那么就是要变成一堆树啦(森林)。

而我们在减少最少的边的情况下,又需要减掉的边的权值和最小。

那么就是留下的树上的边的权值和最大,不是最大生成树还是什么?

此题的坐标完全无用。。。

步骤思路:给定了墙的代价及墙两端的城楼,就相当于给定了每个点及相连的边,在这个图内找最大的生成树。。。

每次找最长的边,然后为防止找到的边围成环,需判断这条边的两个端点是否属于同一棵数,需要进行并查集操作

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
struct edge{
    int from, to, cost;
}G[200005];
int n,m;
bool cmp(edge a, edge b){ return a.cost>b.cost; }
int root[100005];
int rootof(int x){
    return root[x]=root[x]==x?x:rootof(root[x]);
}
bool merge(int a, int b){
    int ra = rootof(a), rb = rootof(b);
    if(ra==rb)return false; // already merged.
    else root[ra]=rb;
    return true;
}
int main() {
    while(scanf("%d%d",&n,&m)!=EOF){
            int x,y;
        for(int i=1;i<=n;i++){
            scanf("%d%d",&x,&y);
            root[i]=i;//起初定义每个点的根是它本身
        }
        long long res = 0;
        int cnt = 0;
        for(int i=0;i<m;i++){
            int a,b,c;
            scanf("%d%d%d",&a,&b,&c);
            G[i].from=a,G[i].to=b,G[i].cost=c;
            res+=c;
        }
        sort(G,G+m,cmp);
        for(int i=0;i<m;i++){
            if(merge(G[i].from,G[i].to)){
                res-=G[i].cost;
                cnt++;
            }
        }
        printf("%d %lld\n",m-cnt,res);
    }
    return 0;
}

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值