swust.oj.1075

唉,看错了,写成了克鲁斯卡尔算法,尴尬
#include<stdio.h>
#include<algorithm>
#include<math.h>
#include<string.h>
#include<iostream>
using namespace std;
int b[20];

struct node
{
	char x, y;
	int date;
}a[100];

void init()
{
	for (int i = 0; i < 20; i++)
	{
		b[i] = i;
	}
}

bool cmp(node x, node y)
{
	if (x.date < y.date)
		return true;
	return false;
}

int find(int x)
{
	if (b[x] == x)return x;
	return b[x] = find(b[x]);
}

int main()
{
	int n, m;
	while (cin >> n>>m)
	{
		char ch[20];
		cin >> ch;
		init();
		for (int i = 0; i < m; i++)
		{
			cin >> a[i].x >> a[i].y >> a[i].date;
		}
		sort(a, a + m, cmp);
		int k = 0;
		for (int i = 0; i < m; i++)
		{
			if (k == n - 1)
				break;
			int x, y;
			for (int j = 0; j < n; j++)
			{
				if (a[i].x == ch[j])
					x = j + 1;
				if (a[i].y == ch[j])
					y = j + 1;
			}
			if (find(x) != find(y))
			{
				cout << "(" << a[i].x << "," << a[i].y << ")";
				k++;
				b[find(x)] = b[find(y)];
			}
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值