HDU - 6187 Destroy Walls 最大生成树

题目:

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

思路:

题目的意思就是m堵墙把一块平面图划分成了许多封闭的区域,求破墙使之变成一块区域的最小花费。

可以将其抽象成生成树问题,因为每块封闭的区域必定是由环路构成的,所以只需要将每个区域拆成树即可,实际上就是求最大生成树,可能会出现某些塔孤立的情况,所以不能完全套用生成树模板。

代码如下:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
const int maxn=1e5+5;
const int maxm=2*1e5+5;
int n,m;
int a[maxn];
ll sum;
int cnt;
struct edge
{
    int u,v;
    int len;

};
edge e[maxm];
int compare (edge a,edge b)
{
    return a.len>b.len;
}
int Find (int x)
{
    if(a[x]==x) return x;
    return a[x]=Find(a[x]);
}
ll kural ()
{
    ll tsum=0;
    for (int i=0;i<m;i++)
    {
        int u=e[i].u,v=e[i].v;
        int fa=Find(u);
        int fb=Find(v);
        if(fa!=fb)
        {
            a[fa]=fb;
            tsum+=e[i].len;
            cnt++;
        }
    }
    return tsum;
}
int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        cnt=sum=0;
        for (int i=1;i<=n;i++) a[i]=i;
        for (int i=1;i<=n;i++)
        {
            int x,y;
            scanf("%d%d",&x,&y);
        }
        for (int i=0;i<m;i++)
        {
            scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].len);
            sum+=e[i].len;
        }
        sort(e,e+m,compare);
        printf("%d %lld\n",m-cnt,sum-kural());
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41410799/article/details/89173226
今日推荐