打卡—最小生成树

前两天发烧就没有打卡...这两天做了一些最小生成树的板子题补上

a:

#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
typedef long long ll;
using namespace std;
int f[10005];
struct node 
{
    int a, b;
    int w;
}p[10005];
bool cmp(node x , node y)
{
    return x.w < y.w;
}
int find(int x){
    if(f[x]!=x) f[x]=find(f[x]);
    return f[x];
}
int main(){
    int y;
	while(cin>>y){
		if(y==0) break;
		int n;
		int sum=0;
		int cnt=0;
		for(int i=0;i<y-1;i++){
			char tmp;
			int qiao;
			cin>>tmp>>qiao;
			int gan=tmp-'A'+1;
			for(int j=0;j<qiao;j++){
				char tmp2;
				cin>>tmp2;
				int qiao2=tmp2-'A'+1,chang;
				cin>>chang;
				p[cnt].a=gan;
				p[cnt].b=qiao2;
				p[cnt].w=chang;
				cnt++;
			}
		}
		sort(p,p+cnt,cmp);
		for(int i=1;i<=y;i++)f[i]=i;
		for(int i=0;i<cnt;i++){
            int a = p[i].a , b =p[i].b , w = p[i].w;
            a = find(a);
            b = find(b);
            if(a==b) continue;
            else{
                f[a]=b;
                sum+=w;
            }
        }
		cout<<sum<<endl;;
	}
}

b: 

#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
typedef long long ll;
using namespace std;
int f[10005];
struct node 
{
    int a, b;
    int w;
}p[10005];
bool cmp(node x , node y)
{
    return x.w < y.w;
}
int find(int x){
    if(f[x]!=x) f[x]=find(f[x]);
    return f[x];
}
int main(){
    int y;
	while(cin>>y){
		if(y==0) break;
		int n;
		int sum=0;
		cin>>n;
		int cnt=0;
		while(n--){
			cin>>p[cnt].a>>p[cnt].b>>p[cnt].w;
			cnt++;
		}
		sort(p,p+cnt,cmp);
		for(int i=1;i<=y;i++)f[i]=i;
		for(int i=0;i<cnt;i++){
            int a = p[i].a , b =p[i].b , w = p[i].w;
            a = find(a);
            b = find(b);
            if(a==b) continue;
            else{
                f[a]=b;
                sum+=w;
            }
        }
		cout<<sum<<endl;;
	}
}

c: 

#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
typedef long long ll;
using namespace std;
int f[10005];
struct node 
{
    int a, b;
    int w;
}p[10005];
bool cmp(node x , node y)
{
    return x.w < y.w;
}
int find(int x){
    if(f[x]!=x) f[x]=find(f[x]);
    return f[x];
}
int main(){
		int n;
		int sum=0;
		cin>>n;
		int cnt=0;
		for(int i=1;i<=n;i++){
			for(int j=1;j<=n;j++){
				if(i==j){
					int tmp;
					cin>>tmp;
					continue;
				}
				p[cnt].a=i;
				p[cnt].b=j;
				cin>>p[cnt].w;
				cnt++;
			}
		}
		for(int i=1;i<=n;i++)f[i]=i;
		int q;
		cin>>q;
		while(q--){
			int x,y;
			cin>>x>>y;
			if(x<y){
				int a=find(x),b=find(y);
				f[a]=b;
			}
		}
		sort(p,p+cnt,cmp);
		
		for(int i=0;i<cnt;i++){
            int a = p[i].a , b =p[i].b , w = p[i].w;
            a = find(a);
            b = find(b);
            if(a==b) continue;
            else{
                f[a]=b;
                sum+=w;
            }
        }
	cout<<sum<<endl;;
}

d:

#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
typedef long long ll;
using namespace std;
int f[10005];
struct node 
{
    int a, b;
    int w;
}p[10005];
bool cmp(node x , node y)
{
    return x.w < y.w;
}
int find(int x){
    if(f[x]!=x) f[x]=find(f[x]);
    return f[x];
}
int main(){
    int y;
	cin>>y;
		int n;
		int sum=0;
		cin>>n;
		int cnt=0;
		while(n--){
			cin>>p[cnt].a>>p[cnt].b>>p[cnt].w;
			cnt++;
		}
		sort(p,p+cnt,cmp);
		for(int i=1;i<=y;i++)f[i]=i;
		for(int i=0;i<cnt;i++){
            int a = p[i].a , b =p[i].b , w = p[i].w;
            a = find(a);
            b = find(b);
            if(a==b) continue;
            else{
                f[a]=b;
                if(w>sum) sum=w;
            }
        }
		cout<<sum<<endl;;
}

f:

#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<queue>
#include<map>
typedef long long ll;
using namespace std;
int f[250005];
float x1[505];
float y123[505];
struct node 
{
    int a, b;
    float w;
}p[250005];
bool cmp(node x , node y)
{
    return x.w < y.w;
}
int find(int x){
    if(f[x]!=x) f[x]=find(f[x]);
    return f[x];
}
int main(){
    int y;
	cin>>y;
	while(y--){
		int n,m;
		
		int cnt=0;
		cin>>n>>m;
		for(int i=1;i<=m;i++){
			cin>>x1[i];
			cin>>y123[i];
		}
		for(int i=1;i<=m;i++){
			for(int j=1;j<=m;j++){
				if(i==j) continue;
				p[cnt].a=i;
				p[cnt].b=j;
				p[cnt].w=sqrt((x1[i]-x1[j])*(x1[i]-x1[j])+(y123[i]-y123[j])*(y123[i]-y123[j]));
				cnt++;
			}
		}
		sort(p,p+cnt,cmp);
		float sum=0.00;
		int sum2=0;
		for(int i=1;i<=m;i++)f[i]=i;
		for(int i=0;i<cnt;i++){
			if(sum2==m-n) break;
            int a = p[i].a , b =p[i].b ;
			float w = p[i].w;
            a = find(a);
            b = find(b);
            if(a==b) continue;
            else{
                f[a]=b;
                sum=w;
				sum2++;
            }
        }
		printf("%.2f\n",sum);
	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值