3RE+1WA求助
  • 板块P1194 买礼物
  • 楼主liyixuan5
  • 当前回复3
  • 已保存回复3
  • 发布时间2021/6/30 19:11
  • 上次更新2023/11/4 21:08:31
查看原帖
3RE+1WA求助
443933
liyixuan5楼主2021/6/30 19:11
#include<bits/stdc++.h>
using namespace std;
#define I(e) for(int  i=0;i<e;i++)
#define J(e) for(int  j=0;j<e;j++)
#define K(w,e) for(int  k=w;k<e;k++)
int fa[1005];
struct P{
	int fm,to,w;
}p[1005];
bool cmp(P a,P b){
	return a.w<b.w;
}
int got(int x){
	if(fa[x]==x){
		return x;
	}
	else{
		return got(fa[x]);
	}
}
void morge(int a,int b){
	fa[got(a)]=got(b);
}
int main(){
	int a,b;
	int y;
	scanf("%d%d",&a,&b);
	int ct=0;
	I(b){
		J(b){
			fa[ct]=ct;
			int h;
			scanf("%d",&h);
			p[ct].w=h;
			if(h==0){
				p[ct].w=a;
			}
			p[ct].fm=i;
			p[ct].to=j;
			ct++;
		}
	}
	sort(p,p+(b*b),cmp);
	int ans=a;
	I(b*b){
		int x=got(p[i].fm),y=got(p[i].to);
		if(x!=y){
			morge(x,y);
			ans+=p[i].w;
		}
		i++;
	}
	cout<<ans;
	return 0;
}
2021/6/30 19:11
加载中...