HDU 6187 Destroy Walls(最小生成树)

Destroy Walls

Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others)
Total Submission(s): 831    Accepted Submission(s): 358


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.62,0.63).

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

3n100000,1m200000

1ui,vin,uivi,0wi10000
 

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
 

Source
 

题意:有一张n个顶点,m条边(墙)的无向图,把平面划分为若干个区域,一个人站在一个固定的坐标,问最少拆多少墙而且花费最少,使得这个人可以到达任一区域。

思路:显然最后的图肯定会没有环,树是没有环的,而且使花费最小,所以求出最大生成树,剩下的边的数目和花费和就是所求答案。

#include <cstdio>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <set>
#include <vector>
#include <queue>
#include <stack>

using namespace std;
typedef long long ll;

const int MAXN = 200005;
const int INF = 0x3f3f3f3f;

int pre[MAXN];

int Find(int x){
    return pre[x] == x ? x : pre[x] = Find(pre[x]);
}
struct Edge
{
    int u,v;
    int cost;
}edge[MAXN];
bool cmp(struct Edge a,struct Edge b)
{
    return a.cost > b.cost;
}
bool Union(int x,int y)
{
    int fx = Find(x);
    int fy = Find(y);

    if(fx != fy) {
        pre[fx] = fy;
        return 1;
    }
    return 0;
}
int main(void)
{
    int n,m,i;
    int x,y,z;
    while(~scanf("%d%d",&n,&m)){
        for(i=1;i<=n;i++){
            scanf("%d%d",&x,&y);
        }
        for(i=1;i<=n;i++){
            pre[i] = i;
        }
        int sum = 0;
        for(i=1;i<=m;i++){
            scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].cost);
            sum += edge[i].cost;
        }
        int cnt = 0,Cost = 0;
        sort(edge + 1,edge + m + 1,cmp);
        for(int i = 1; i <= m; i++) {
            if(Union(edge[i].u,edge[i].v)) {
                Cost += edge[i].cost;
                cnt++;
            }
        }
        printf("%d %d\n",m - cnt,sum - Cost);
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值