ZOJ - 3204 Connect them (最小生成树)

题意:输出字母序最小的最小生成树

思路:对边按照cost u v排序之后,求最小生成树,然后在对u v排序输出

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <cmath>
#include <cctype>

using namespace std;
const int INF = 0x3f3f3f3f;
const int maxn = 10005;
struct node{
	int u,v;
	int cost;
}no[maxn];
struct node1{
	int u,v;
}ans[maxn];
bool cmp2(struct node1 a,struct node1 b){
	if(a.u == b.u){
		return a.v < b.v;
	}
	return a.u < b.u;
}

int pre[105];
int find(int x){
	if(x == pre[x]){
		return x;
	}
	else{
		return pre[x] = find(pre[x]);
	}
}
bool join(int x,int y){
	int fx = find(x);
	int fy = find(y);
	if(fx != fy){
		pre[fy] = fx;
		return 1;
	}
	return 0;
}
bool cmp1(struct node a,struct node b){
	if(a.cost == b.cost){
		if(a.u == b.u){
			return a.v < b.v;
		}
		return a.u < b.u;
	}
	return a.cost < b.cost;
}
int main(void){
	int t;
	scanf("%d",&t);
	
	while(t--){
		int n;
		scanf("%d",&n);
		int cnt = 0;
		for(int i = 1; i <= n; i++){
			for(int j = 1; j <= n; j++){
				int co;
				scanf("%d",&co);
				
				if(i < j && co != 0){
					no[cnt].u = i;
					no[cnt].v = j;
					no[cnt].cost = co;
					cnt++;
				}
			}
		}
		
		sort(no,no + cnt,cmp1);
		for(int i = 1; i <= n; i++){
			pre[i] = i;
		}
		int cntt = 0;
		int pos;
		for(int i = 0; i < cnt; i++){
			if(join(no[i].u,no[i].v)){
				struct node1 temp;
				temp.u = no[i].u;
				temp.v = no[i].v;
				ans[cntt++] = temp;
			}
			if(cntt == n - 1){
				pos = i;
				break;
			}
		}
		if(cntt == n - 1){
			sort(ans,ans + n - 1,cmp2);
			for(int i = 0; i < n - 1; i++){
				if(i == n - 2){
					printf("%d %d\n",ans[i].u,ans[i].v);
				}
				else{
					printf("%d %d ",ans[i].u,ans[i].v);
				}
			}
		}
		else{
			printf("-1\n");
		}
	}
	return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值