求问+敲警钟—RE
查看原帖
求问+敲警钟—RE
905100
chenyizhen楼主2024/12/6 20:38

不知为什么第二的点re 把305的数据额范围开大一点就过了 求问why

#include<bits/stdc++.h>
using namespace std;
const int N=300*300+5,inf=1e5+5;
int w[305],n,p[305][305];
int cnt=0;
long long ans=0;
int water[3050],f[3050],well[3005];//这改了一下
struct edge{
	int start,end,w;
}e[N];
bool cmp(edge x,edge y){
	return x.w<y.w;
}
int getf(int x){
	if(f[x]==x) return x;
	else return f[x]=getf(f[x]); 
}
void graft(int x,int y){
	int t1=getf(x),t2=getf(y);
	if(t1!=t2){
		f[t1]=t2;
	}
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	cin>>n;
	for(int i=1;i<=n;i++){
		cin>>w[i];
	}
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			cin>>p[i][j];
			if(i==j) continue;
			e[++cnt].start=i;e[cnt].end=j;e[cnt].w=p[i][j];
		}
	}
	f[n+1]=n+1;
	for(int i=1;i<=n;i++){
		f[i]=i;
		e[++cnt].start=n+1;e[cnt].end=i;e[cnt].w=w[i];
		e[++cnt].start=i;e[cnt].end=n+1;e[cnt].w=w[i];
	}
	sort(e+1,e+1+cnt,cmp);
	for(int i=1;i<=cnt;i++){
		
		if(getf(e[i].start)!=getf(e[i].end)){
			graft(e[i].start,e[i].end);
			ans+=e[i].w;
		}			
	}
	cout<<ans;
	return 0;
}
2024/12/6 20:38
加载中...