ZOJ1085-Alien Security

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <queue>
using namespace std;

#define NUM 100
int n;
int et;
bool used[NUM];
bool data[NUM][NUM];
int dis[NUM];

void bfs_path() 
{
	queue<int> q; 
	q.push(et);
	dis[et] = 0;
	int x;
	while (!q.empty())
	{
		x = q.front();
		q.pop();
		for (int i = 0; i < n; ++i)
			if (data[i][x] && dis[x] + 1 < dis[i])
			{
				q.push(i);
				dis[i] = dis[x] + 1;
			}
	}
}

int dfs_search(int id)
{
	if (id == et) return 1;
	used[id] = 1;
	for (int i = 0; i <n; ++i)
		if (!used[i] && data[id][i])
			if (dfs_search(i)) return 1;
	return 0;
}

int main()
{
	int cases;
	int room;
	scanf("%d", &cases);
	for (int t = 0; t < cases; ++t)
	{
		scanf("%d%d\n", &n, &et);
		memset(data, 0, sizeof(data));
		for (int i = 0; i < NUM; ++i) 
			dis[i] = 100000;
		char line[10];
		int a, b;
		while (gets(line))
		{
			if (strcmp(line, "")==0) break;
			sscanf(line, "%d%d", &a, &b);
			data[a][b] = true;
		}
		bfs_path();
		int d = dis[0];
		room = 0;
		for (int i = 1; i < n; ++i)
		{
			if (i == et) continue;
			memset(used, 0, sizeof(used));
			used[i] = 1;
			if (!dfs_search(0) && dis[i] < d)
			{
				room = i;
				d = dis[i];
			}
		}
		if (t) cout << endl;
		cout << "Put guards in room " << room << "." << endl;
	}
	return 0;
}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值