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

Destroy Walls

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

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

思路:要想国王能达到任何一个地方,那么一定不能有环,就是求一棵树,同时拆掉的边的值最小,就是求一个最大生成树。前面给你n个坐标,就是唬你的。

#include<algorithm>  
#include<cstdio>  
#include<cstring>  
#include<iostream>  
using namespace std;  
const int MAX=2e5;  
struct edge  
{  
    int u,v,w;  
}e[MAX];  
int p[MAX];  
int f(int x){return p[x]==x?x:p[x]=f(p[x]);}  
int cmp(const edge& x,const edge& y){return x.w>y.w;}      //这里写成>=就会无限RE。。。  
int main()  
{  
    int x,y;  
    int n,m;  
    while(scanf("%d%d",&n,&m)!=EOF)  
    {  
        int sum=0,ans=0,edg=0;  
        for(int i=1;i<=n;i++)scanf("%d%d",&x,&y),p[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,cmp);  
        for(int i=0;i<m;i++)  
        {  
            int fu=f(e[i].u),fv=f(e[i].v);  
            if(fu!=fv)  
            {  
                edg++;  
                p[fu]=fv;  
                ans+=e[i].w;  
            }  
        }  
        printf("%d %d\n",m-edg,sum-ans);  
    }  
    return 0;  
}  


猜你喜欢

转载自blog.csdn.net/xiao__jia__jia/article/details/80165741
今日推荐