hdu 6187 Destroy Walls

题目链接

这题类似2018徐州邀请赛的k题 题目链接

简单点想,就是给定点和边,在无向图中除去代价比较小的边构造最大生成树,将边集排序,边上两顶点合并成树。题目要求的花费即为代价小的边集,拆的边数即为总边-最大生成树边数

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N= 1e6+5;
int f[N];

struct NODE{
	int st,ed;
	int w;
	
	bool operator <(const NODE &a)const{
		return w>a.w;  //从大到小排序 
	}
}edge[N];

int find(int x){
	return f[x]=f[x]==x?x:find(f[x]);
} 

bool Union(int a,int b){
	int fa=find(a);
	int fb=find(b);
	
	if(fa==fb) return false;
	f[fa]=fb;
	return true;
}

int main(){
	int n,e;
	while(~scanf("%d %d",&n,&e)){
		int x,y;
		for(int i=1;i<=n;i++){
			scanf("%d %d",&x,&y);
			f[i]=i;  //初始化 
		}
		
		ll cost=0;
		for(int i=0;i<e;i++){
			scanf("%d %d %d",&edge[i].st,&edge[i].ed,&edge[i].w);
			cost+=edge[i].w;
		}
		sort(edge,edge+e);

		int cnt=0;
		for(int i=0;i<e;i++){
			if(Union(edge[i].st,edge[i].ed)){
				cost-=edge[i].w;
				cnt++;
			}
		}
		printf("%d %d\n",e-cnt,cost);
	}	
	return 0;
}

猜你喜欢

转载自blog.csdn.net/lidengdengter/article/details/81055244