ZOJ-1085 Alien Security

Alien Security

Time Limit: 2 Seconds      Memory Limit: 65536 KB

You are in charge of security at a top-secret government research facility. Recently your government has captured a live extra-terrestrial (ET) life form, and is hosting an open day for fellow researchers. Of course, not all the guests can be trusted, so they are assigned different security clearance levels. Only guests with a level 5 rating will be allowed into the lab where the extra-terrestrial is being held; other than that, everyone is free to roam throughout the rest of the facility. Each room in the facility is connected via one-way airlocks, so that you can pass through the door in only one direction.

To protect your precious ET you will put in place enhanced security measures (in the form of armed guards) on the route leading to the room containing the ET, but not in the room itself C the guards do not have sufficient clearance to enter the room containing the ET.

The guards will check the identity and the security rating of all guests trying to pass through the room in which they are stationed, so you would like to place the guards where they will cause the minimum amount of irritation to the guests who have no intention of visiting the ET. The room where the guards must be placed thus satisfies the following two conditions:

1. In order to get to the room containing the ET, the guests must pass through the room containing the guards;

2. There is no other room with this property that is closer to the room containing the ET  C remember, the guards cannot be placed in the room containing the ET itself.

The diagram below illustrates one possible map of your facility:

Note that placing the guards in room 2 would satisfy the first condition, but room 3 is closer to the ET, so the guards must be placed in room 3.

All guests enter through room 0, the entrance to your facility. Your program accepts a sequence of lines containing integers. The first line consists of two integers: the number of rooms, and the room in which the ET is being held (out of his own free will, of course).

The rest of the input is a sequence of lines consisting of only two integers, specifying where the airlock-doors are located. The first number on these lines specifies the source room, and the second the destination room. Remember: you can pass only from the source room to the destination room.

The output of your program consists only of a single line:

Put guards in room N.

where N is the room you've decided to place the guards.


This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.


SAMPLE INPUT

This input sequence specifies the map shown above.

1

9 4
0 2
2 3
3 4
5 3
5 4
3 6
6 5
6 7
6 8
4 7
0 1
1 7
7 0


SAMPLE OUTPUT

Put guards in room 3. 


先求两点间的最短路径,通过floyd算法可以在O(n^3)的时间复杂度下求得,然后判断每一个最近的必须经过的点,对每一个必须经过的点处理成不可达,然后可以通过floyd-washall先求闭包,然后判断两点间的是否可达。

注意输入和输出的格式。


#include <stdio.h>
#include <string.h>

int reached(int, int);
int map[100][100][2], n;
int temp[100][100];
int main()
{
	int N, i, j, et, minimal, result;
	int f, t, k;
	char str[20];
	scanf("%d", &N);
	getchar();
	while(N--) {
		if(N == 0) {
			scanf("%d%d", &n, &et);
			for(i = 0; i < n; i++) {
				for(j = 0; j < n; j++) {
					map[i][j][1] = 10000;
					map[i][j][0] = 0;
				}
			}
			while(scanf("%d%d", &f, &t) != EOF) {
				map[f][t][0] = 1;
				map[f][t][1] = 1;
			}
		} else {
			scanf("%d%d", &n, &et);
			getchar();
			for(i = 0; i < n; i++) {
				for(j = 0; j < n; j++) {
					map[i][j][1] = 10000;
					map[i][j][0] = 0;
				}
			}
			while(gets(str) && strcmp(str, "") != 0) {
				f = 0;
				i = 0;
				while(i < strlen(str) && str[i] != ' ') {
					f *= 10;
					f += str[i]-'0';
					i++;
				}
				i++;
				t = 0;
				while(i < strlen(str)) {
					t *= 10;
					t += str[i] - '0';
					i++;
				}
				map[f][t][0] = 1;
				map[f][t][1] = 1;
			}
		}
		for(k = 0; k < n; k++) {
			for(i = 0; i < n; i++) {
				for(j = 0; j < n; j++) {
					if(map[i][j][1] > map[i][k][1] + map[k][j][1]) {
						map[i][j][1] = map[i][k][1] + map[k][j][1];
					}
				}
			}
		}
		minimal = 100;
		for(i = 0; i < n; i++) {
			if(et == i) {
				continue;
			}
			if(map[i][et][1] < minimal) {
				for(j = 0; j < n; j++) {
					for(k = 0; k < n; k++) {
						temp[j][k] = map[j][k][0];
					}
				}
				if(!reached(i, et)) {
					minimal = map[i][et][1];
					result = i;
				}
			}
		}
		printf("Put guards in room %d.\n", result);
		if(N) {
			printf("\n");
		}
	}
	return 0;
}

int reached(int s, int e)
{
	int i, j, k;
	for(i = 0; i < n; i++) {
		temp[s][i] = 0;
	}
	for(k = 0; k < n; k++) {
		for(i = 0; i < n; i++) {
			for(j = 0; j < n; j++) {
				if(temp[i][k] && temp[k][j]) {
					temp[i][j] = 1;
				}
			}
		}
	}
	if(temp[0][e]) {
		return 1;
	} else {
		return 0;
	}
}


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值