7 solutions

  • 0
    @ 2022-1-21 20:13:04

    我是蒟蒻

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    int fa[2031];
    
    struct edge
    {
    	int start,end,val;
    };
    bool cmp(edge a,edge b)
    {
    	return a.val<b.val;
    }
    vector<edge> V;
    int get(int a,int b)
    {
    	int ans=0;
    	vector<int> first,sec;
    	while(a>0)
    	{
    		first.push_back(a%10);
    		a/=10;
    	}
    	while(b>0)
    	{
    		sec.push_back(b%10);
    		b/=10;
    	}
    	int lenth1=first.size(),lenth2=sec.size();
    	int i=0;
    	while(i<lenth1&&i<lenth2)
    	{
    		if(first[i]!=sec[i]) ans+=first[i]+sec[i];
    		i++;
    	}
    	while(i<lenth1) ans+=first[i++];
    	while(i<lenth2) ans+=sec[i++];
    	return ans;
    }
    
    int find(int x) 
    {
    	int t = x; 
    	while(t != fa[t]) t = fa[t];
    	while(x != fa[x]) {
    		int temp = fa[x];
    		fa[x] = t;
    		x = temp;
    	}
    	return x;
    }
    
    int kruskal()
    {
    	int ans=0;
    	for(int i=1;i<=2021;++i)
    	{
    		fa[i]=i;
    	}
    	int lenth=V.size();
    	for(int i=0;i<lenth;++i)
    	{
    		int s=V[i].start;
    		int e=V[i].end;
    		s=find(s);
    		e=find(e);
    		if(s!=e)
    		{
    			ans+=V[i].val;
    			fa[e]=s;
    		}
    	}
    	return ans;
    }
    
    int main()
    {
    	for(int i=1;i<=2021;++i)
    	{
    		for(int j=i+1;j<=2021;++j)
    		{
    			int val=get(i,j);
    			V.push_back({i,j,val});
    		}
    	}
    	sort(V.begin(),V.end(),cmp);
    	cout<<kruskal()<<"\n";
    	return 0;
    }
    

    Information

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