90pts WA on #3 help
查看原帖
90pts WA on #3 help
764773
AstaVenti_楼主2024/1/28 14:17
#include<bits/stdc++.h>
using namespace std;
struct edge{
	int u,v,w;
}e[100005];
int fa[100005],n,p,k=0,m,ans=0,ltk;
int find(int x){
	return x==fa[x]?x:fa[x]=find(fa[x]);
}
bool cmp(edge a,edge b){
	return a.w<b.w;
}
int main(){
	cin>>n;
	ltk=n+1;
	for(int i=0;i<n;i++){
		cin>>p;
		e[i].u=0,e[i].v=i+1,e[i].w=p,k++;
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			cin>>m;
			if(i>j){
				e[k].u=i,e[k].v=j,e[k++].w=m;
			}
		}
	}
	sort(e,e+k-1,cmp);
	for(int i=0;i<n+1;i++)fa[i]=i;
	for(int i=0;i<k;i++){
		int u=find(e[i].u),v=find(e[i].v);
		if(u!=v){
			ans+=e[i].w,fa[u]=v,ltk--;
		}
		if(ltk==1)break;
	}
	cout<<ans;
	
}

2024/1/28 14:17
加载中...