Destroy Walls HDU - 6187 (最大生成树)

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个塔,m个城墙(城墙只在有塔的位置相交),国王要从皇宫能够到达任意地方,必须去掉一部分墙,求至少去掉的墙的数量,在此条件下,最小花费

思路:要想能够到达任意地方,必须保证图中无环,第一反应是拓扑排序,在这里搞了好处时间没搞出来(太菜~~)

其实,图中无环,就是要保证图中是树或者森林,多余的边必须全部去掉,要求去掉边的价值和要最小,所以留下的边要尽可能的大,求最大生成树。

图中给的点的坐标毫无卵用,完全在蒙你,我被蒙住了~~

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 2e5+5;
struct node{
	int u;
	int v;
	int w;
	bool operator < (const node other) const{
		return w>other.w;
	}
}edge[N];
int per[N];
void init(){
    for(int i=0;i<N;i++)
       per[i]=i;
}
int Find(int u){
	return u==per[u] ? u : per[u]=Find(per[u]);
}
int mix(int a,int b){
	int fa=Find(a),fb=Find(b);
	if(fa!=fb){
		per[fb]=fa;
		return 1;
	}
	return 0;
}
int main(){
	int n,m;
    while(~scanf("%d%d",&n,&m)){
    	init();
	    for(int i=0;i<n;i++){
			int x,y;
			scanf("%d%d",&x,&y);
		}
		int sum=0;
		for(int i=0;i<m;i++){
			scanf("%d%d%d",&edge[i].u,&edge[i].v,&edge[i].w);
			sum+=edge[i].w;
		}
		sort(edge,edge+m);
		int cnt=0,ans=0;
		for(int i=0;i<m;i++){
			int a=edge[i].u,b=edge[i].v;
			if(mix(a,b)){
				cnt++;
				ans+=edge[i].w;
			}
			if(cnt==n-1) break;
		}
		printf("%d %d\n",m-cnt,sum-ans);
	}
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/islittlehappy/article/details/81046953
今日推荐