zoj 1127 Roman Forts

#include<iostream>
#include<cstring>
#include<string>
#include<queue>
#include<map>
#include<algorithm>
using namespace std;
int forts[27], vis[27];
vector<int> mp[27];
int n, k;
int bfs(int ori) {
	vis[ori] = 1;
	queue<int> q;
	q.push(ori);
	int sz, cur, coun, len, i;
	coun = 0;
	while (!q.empty()) {
		sz = q.size();
		coun++;
		while (sz--) {
			cur = q.front();
			q.pop();
			vector<int> to = mp[cur];
			len = to.size();
			for (i = 0; i < len; i++) {
				if (vis[to[i]])continue;
				if (forts[to[i]]) {
					return coun;
				} else {
					vis[to[i]] = 1;
					q.push(to[i]);
				}
			}
		}
	}
}

int main() {
	char ch1, ch2, first;
	vector<int> ans;
	memset(forts, 0, sizeof(forts));
	cin >> n >> first >> k;
	forts[int(first - 'A' + 1)] = 1;
	ans.push_back(int(first - 'A' + 1));
	while (cin >> ch1 >> ch2) {
		mp[int(ch1 - 'A' + 1)].push_back(int(ch2 - 'A' + 1));
		mp[int(ch2 - 'A' + 1)].push_back(int(ch1 - 'A' + 1));
	}

	int i, j;
	int num, maxd, vuner[27];
	vector<int> v;
	for (i = 1; i < k; i++) {
		maxd = -1;
		memset(vuner, 0, sizeof(vuner));
		v.clear();
		for (j = 1; j <= n; j++) {
			if (!forts[j]) {
				memset(vis, 0, sizeof(vis));
				num = bfs(j);
				vuner[j] = num;
				maxd = max(num, maxd);
			}
		}
		for (j = 1; j <= n; j++) {
			if (!forts[j] && maxd == vuner[j]) {
				v.push_back(j);
			}
		}
		sort(v.begin(), v.end());
		forts[v[0]] = 1;
		ans.push_back(v[0]);
	}

	int len = ans.size();
	printf("Program 8 by team X\n");
	printf("%c", char(ans[0] + 'A' - 1));
	for (i = 1; i < len; i++) {
		printf(" %c", char(ans[i] + 'A' - 1));
	}
	printf("\nEnd of program 8 by team X");
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值