您的位置:首页 > 编程语言 > Go语言

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

2012-10-13 16:01 489 查看
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
,rank
;
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  algorithm struct system ini ie c