7 solutions

  • 0
    @ 2022-4-7 21:18:12

    时隔这么多天,终于把这道题做了(泪目)

    #include<iostream>
    #include<algorithm>
    using namespace std;
    
    int t=1,f[2027];
    
    struct Edge{
    	int u,v,w;
    }edges[4084441];
    
    bool cmp(Edge a,Edge b){
    	return a.w<b.w;
    }
    
    int val_num(int x,int y){
    	int sum=0;
    	while(x||y){
    		if(x%10!=y%10){
    			sum+=x%10;
    			sum+=y%10;
    		}
    		x/=10;
    		y/=10;
    	}
    	return sum;
    }
    
    int find(int x){
    	if(x==f[x])return x;
    	else return find(f[x]);
    }
    
    void val(){
    	for(int i=1;i<=2021;i++){
    		for(int j=1;j<=2021;j++){
    			if(i==j)continue;
    			edges[t].u=i;
    			edges[t].v=j;
    			edges[t].w=val_num(i,j);
    			t++; 
    		}
    	}
    }
    
    int kruskal(){
    	int total=0,sum=0;
    	for(int i=1;i<=t;i++){
    		Edge e=edges[i];
    		int u=find(e.u),v=find(e.v),w=e.w;
    		if(u==v)continue;
    		total+=w;
    		f[u]=v;
    		sum++;
    		if(sum==2020)break;
    	}
    	return total;
    }
    
    int main(){
    	val_num(1511,501);
    	val();
    	for(int i=1;i<=2021;i++)f[i]=i;
    	sort(edges+1,edges+t+1,cmp);
    	cout<<kruskal();
    	return 0;
    } 
    

    Information

    ID
    108
    Time
    1000ms
    Memory
    256MiB
    Difficulty
    6
    Tags
    # Submissions
    122
    Accepted
    41
    Uploaded By