hdu6187 最大生成树

Destroy Walls
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
题意:国王的领土被分割成n个地方,国王想到达他领土的每一个地方,但是现在一些有一些城墙的隔绝两个地方,拆掉城墙需要耗费 一些财力物力,国王想拆最小的城墙数,花费最小的人力物力。给出的数据 ,开始给出的n个坐标值,(这个是唬人的,一点用都没有),而后给出 u v w表示一个城墙连接的两个地方和拆掉需要耗费的代价了。
在说思路之前先介绍一下最大生成树:在一个图的所有生成树中边权值和最大的生成树即为最大生成树。
那怎么生成呢?怎么生成:

1、将图中所有边的边权变为相反数,再跑一遍最小生成树算法。相反数最小,原数就最大。

2、修改一下最小生成树算法:对于kruskal,将“从小到大排序”改为“从大到小排序”;对于prim,将“每次选到所有蓝点代价最小的白点”改为“每次选到所有蓝点代价最大的点”。

思路:给你n个点,m条边,构成一张图,显然这个图可能会有环路,现在已知每条边都有一个权值,现在让你删掉一些边,使得图中不存在环。问你最少要删掉几条边,在删掉这么多边的情况下问你所删掉的边的最小权值和是多少。此时我们用kruskal算法将边权值从大到小排序,直到生成n-1条边,即为最大生成树,而剩下的边数就是要拆掉的墙数,以及边权值总和就是耗费的最小代价。
附上AC代码:

#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#define mod 100000007
#define maxn 10010
using namespace std;
typedef long long ll;
struct node
{
    ll x,y,k;
} Q[200010];
ll f[100010];
ll findx(ll x)
{
    return f[x]==x? x:f[x]=findx(f[x]);
}
bool cmp(node a,node b)
{
    return a.k>b.k;
}
int main()
{
    ll n,m;
    while(cin>>n>>m)
    {
        ll a,b,mincost=0,deswall=0;
        for(ll i=1; i<=n; i++)
        {
            cin>>a>>b;
            f[i]=i;
        }
        for(ll i=0; i<m; i++)
        {
            cin>>Q[i].x>>Q[i].y>>Q[i].k;
            mincost+=Q[i].k;
        }
        sort(Q,Q+m,cmp);
        for(ll i=0; i<m; i++)
        {
            ll tx=findx(Q[i].x);
            ll ty=findx(Q[i].y);
            if(tx!=ty)
            {
                f[tx]=ty;
                deswall++;//这是要保留的墙数,是最大生成树的边
                mincost-=Q[i].k;
                if(deswall==n-1)break;
            }
        }
        cout<<m-deswall<<' '<<mincost<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值