poj 1251 最小生成树

6 篇文章 0 订阅

题意:用桥把n个岛屿连接起来


直接最小生成树就行

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <vector>
#define rep(i, j, k) for(int i = j; i <= k; i++)
#define maxn 200009

using namespace std;

int n, m, tt, father[1009];

struct cadongllas
{
	int x, y, value;
}t[100];

bool cmp (cadongllas x, cadongllas y)
{
	return x.value < y.value;
}

int get_father (int x)
{
	if (father[x] == x)
		return x;
	return get_father (father[x]);
}

int main ()
{
	while (scanf ("%d", &n) == 1 && n)
	{
		int ans = 0, cnt = 0;
		rep (i, 1, n - 1)
		{
			char s[10];
			scanf ("%s", s);
			scanf ("%d", &m);
			rep (j, 1, m)
				scanf ("%s%d", s, &tt), t[++cnt] = (cadongllas){i, s[0] - 'A' + 1, tt};// printf ("add %d %d --%d\n", i, s[0] - 'A' + 1, tt);
		}
		sort (t + 1, t + 1 + cnt, cmp);
		rep (i, 1, n)
			father[i] = i;
		rep (i, 1, cnt)
		{
			int u = get_father (t[i].x), v = get_father (t[i].y);
			if (u != v)
				father[u] = v, ans += t[i].value;
		}
		cout << ans << endl;
	}
	return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值