poj Kruskal 【poj3723 Conscription : 最大生成树;poj 1251 Jungle Roads: 最小生成树poj1861Network】

poj3723 Conscription : 最大生成树

#include <cstdio>
#include <algorithm>
#define N 20005
#define E 50005
using namespace std;
struct edge
{
    int u,v,w;
}e[E];
int n,sum;
int father[N],rank[N];
bool cmp(edge a,edge b)
{
    return a.w>b.w;
}
void makeset(void)
{
    register int i=0;
    for(i=0;i<n;i++)
    {
        father[i]=i;
        rank[i]=0;
    }
}
int find(int x)
{
    if(x!=father[x])    father[x]=find(father[x]);
    return father[x];
}
void Union(int x,int y,int w)
{
    x=find(x),y=find(y);
    if(x==y)    return;
    if(rank[x]>rank[y])  father[y]=x;
    else
    {
        if(rank[x]==rank[y])    rank[y]++;
        father[x]=y;
    }
    sum+=w;
}
int main(void)
{
    int m,r,t,a,b,c;
    register int i;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&m,&n,&r);
        n+=m;
        makeset();
        for(i=0;i<r;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            b+=m;
            e[i].u=a,e[i].v=b,e[i].w=c;
        }
        sort(e,e+r,cmp);
        for(sum=i=0;i<r;i++)
            Union(e[i].u,e[i].v,e[i].w);
        printf("%d\n",10000*n-sum);
    }
    return 0;
}


poj 1251 Jungle Roads: 最小生成树


#include <stdio.h>
#include <iostream>
#include <algorithm>
 
using namespace std;
 
#define MAX 26
 
/* 定义边(x,y),权为w */
typedef struct
{
	int x, y;
	int w;
}edge;
 
edge e[MAX * MAX];
/* rank[x]表示x的秩 */
int rank[MAX];
/* father[x]表示x的父节点 */
int father[MAX];
int sum;
 
/* 比较函数,按权值非降序排序 */
bool cmp(const edge a, const edge b)
{
     return a.w < b.w;
}
 
/* 初始化集合 */
void Make_Set(int x)
{
	father[x] = x;
	rank[x] = 0;
}
 
/* 查找x元素所在的集合,回溯时压缩路径 */
int Find_Set(int x)
{
	if (x != father[x])
	{
		father[x] = Find_Set(father[x]);
	}
	return father[x];
}
 
/* 合并x,y所在的集合 */
void Union(int x, int y, int w)
{
 
	if (x == y) return;
	if (rank[x] > rank[y])
	{
		father[y] = x;
	}
	else
	{
		if (rank[x] == rank[y])
		{
			rank[y]++;
		}
		father[x] = y;
	}
	sum += w;
}
 
int main()
{
	int i, j, k, m, n, t;
	char ch;
	while(cin >> m && m != 0)
	{
		k = 0;
		for (i = 0; i < m; i++) Make_Set(i);
		for (i = 0; i < m - 1; i++)
		{
			cin >> ch >> n;
			for (j = 0; j < n; j++)
			{
				cin >> ch >> e[k].w;
				e[k].x = i;
				e[k].y = ch - 'A';
				k++;
			}
		}
 
		sort(e, e + k, cmp);
 
		sum = 0;
 
		for (i = 0; i < k; i++)
		{
			Union(Find_Set(e[i].x), Find_Set(e[i].y), e[i].w);
		}
 
		cout << sum << endl;
	}
	 system("PAUSE");
	return 0;
}
   

poj1861Network

#include <stdio.h>
#include <stdlib.h>
 
#define MAX 15001
 
/* 定义边(x,y),权为w */
typedef struct
{
	int x, y;
	int w;
}edge;
 
edge e[MAX];
edge v[MAX];
/* rank[x]表示x的秩 */
int rank[MAX];
/* father[x]表示x的父节点 */
int father[MAX];
 
/* 比较函数,按权值非降序排序 */
int cmp(const void *a, const void *b)
{
	return (*(edge *)a).w - (*(edge *)b).w;
}
 
/* 初始化集合 */
void Make_Set(int x)
{
	father[x] = x;
	rank[x] = 0;
}
 
/* 查找x元素所在的集合,回溯时压缩路径 */
int Find_Set(int x)
{
	if (x != father[x])
	{
		father[x] = Find_Set(father[x]);
	}
	return father[x];
}
 
/* 合并x,y所在的集合 */
void Union(int x, int y)
{
 
	if (x == y) return;
	if (rank[x] > rank[y])
	{
		father[y] = x;
	}
	else
	{
		if (rank[x] == rank[y])
		{
			rank[y]++;
		}
		father[x] = y;
	}
}
 
int main()
{
	int i, j, m, n, k, max;
	int x, y;
 
	scanf("%d%d", &m, &n);
 
	/* 读入边数据 */
	for (i = 0; i < n; i++)
	{
		scanf("%d%d%d", &e[i].x, &e[i].y, &e[i].w);
	}
 
	/* 初始化集合 */
	for (i = 0; i < m; i++)
	{
		Make_Set(i);
	}
 
	/* 对边排序 */
	qsort(e, n, sizeof(edge), cmp);
 
	k = 0;
	max = 0;	
 
	for (i = 0; i < n; i++)
	{
		x = Find_Set(e[i].x);
		y = Find_Set(e[i].y);
		if (x != y)
		{
			k++;
			Union(x, y);
			/* 保存边信息 */
			v[k] = e[i];
			/* 记录最大权值 */
			if (max < e[i].w) max = e[i].w;
		}
	}
 
	/* 输出结果 */
	printf("%d\n", max);
	printf("%d\n", k);	
	for (i = 1; i <= k; i++)
	{
		printf("%d %d\n", v[i].x, v[i].y);
	}
   system("pause");
	return 0;
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值