萌新求助
查看原帖
萌新求助
174897
zjrdmd楼主2020/7/16 21:48

正解目前没想出来,觉得64pts挺好写的于是想先把64pts做法写出来,结果64pts也挂掉了/kk

大致思路:将每个点附上一个编号,然后正常跑Kruskal。

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <vector>
#include <set>
#include <map>
#include <stdlib.h>
#include <stack>
#include <queue>
#define ri register int
#define N 300005
#define M 90005

inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
    return x*f;
}

struct node{
	int q,z,b;
}edge[M];

int f[M],n,m,cnt=0,suma=0,sumb=0;
long long ans=0;
int d;

bool cmp(node x,node y){
	return x.b<y.b;
}

int get(int p){
	if(f[p]==p)return p;
	return f[p]=get(f[p]);
}
int main(){
    n=read(),m=read();d=n*m-1;
    for(ri i=1;i<=n;i++){
    	int w=read(),q=(i-1)*m+1,z=q+1;
    	for(ri j=1;j<m;j++){
    		edge[++cnt].b=w,edge[cnt].q=q,edge[cnt].z=z;
    		q+=1,z+=1;
		}
	}
	for(ri i=1;i<=m;i++){
		int w=read(),q=i,z=i+m;
		for(ri j=1;j<n;j++){
			edge[++cnt].b=w,edge[cnt].q=q,edge[cnt].z=z;
			q+=m,z+=m;
		}
	}
	for(ri i=1;i<=d+1;i++)f[i]=i;
	std::sort(edge+1,edge+cnt+1,cmp);
    while(true){
    	int x=edge[++sumb].q,y=edge[sumb].z;
    	if(get(x)!=get(y)){
    		suma++;
    		ans+=(long long)edge[sumb].b;
    		f[get(x)]=get(y);
		}
		if(sumb==cnt||suma==d)break;
	}
	printf("%lld",ans);
	return 0;
}

目前得分:48pts/kk

2020/7/16 21:48
加载中...