Alien Security (搜索)

来源:互联网 发布:穿越火线游戏数据异常 编辑:程序博客网 时间:2024/06/10 03:26

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. 

给出一个有向图,找一个节点,要求从0进入访问住着外星人的节点必须经过你的结点,还要求你的结点尽可能靠近外星人。

这是我目前见过的最奇葩输入,对输入格式的要求比较高,建议不要用cin,一直wa找不到错误,把cin改了就过了。首先从有外星人的结点开始广搜,因为这样可以保证目前搜到的点始终是离外星人最近的且可能符合第一个条件的,每搜到一个点,尝试着把该点删除,即从起点0处开始深搜,在判断语句里加一个条件,就是不让路径中出现这个假定被删除的点,如果最后可以到达外星人的结点说明该点不是你要找的点,就标记过后继续广搜下一个点,然后再用深搜判断是否符合条件。

#include <iostream>#include <cstring>#include <cstdio>#include <queue>#include <vector>using namespace std;const int M = 105;vector<int> adjin[M];vector<int> adjout[M];int n, e;bool dfsbo[M], bfsbo[M];bool f;int ans;void dfs(int rm, int now){if(rm==0)return;if(now==e){f = true;return;}vector<int>::iterator it;for(it=adjout[now].begin();it!=adjout[now].end();it++){if(*it!=rm&&!dfsbo[*it]){dfsbo[*it] = true;dfs(rm, *it);dfsbo[*it] = false;}if(f)return;}}void bfs(){memset(bfsbo, false, sizeof(bfsbo));queue<int> q;int tmp;q.push(e);bfsbo[e] = true;while(!q.empty()){tmp = q.front();q.pop();vector<int>::iterator it;for(it=adjin[tmp].begin();it!=adjin[tmp].end();it++){if(!bfsbo[*it]){bfsbo[*it] = true;f = false;memset(dfsbo, false, sizeof(dfsbo));dfsbo[*it] = true;dfs(*it, 0);dfsbo[*it] = false;if(f)q.push(*it);else{ans = *it;return;}}}}}int main(){int t, u, v;char str[50];// cin >> t;scanf("%d", &t);getchar();while(t--){// cin >> n >> e;scanf("%d%d", &n, &e);getchar();for(int i=0;i<n;i++){adjin[i].clear();adjout[i].clear();}while(gets(str)){if(strcmp(str, "")==0)break;sscanf(str, "%d %d", &u, &v);adjout[u].push_back(v);adjin[v].push_back(u);}bfs();printf("Put guards in room %d.\n", ans); if(t!=0)printf("\n");}return 0;}