Alien Security
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 2646 | Accepted: 974 |
Description
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 ?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 ?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.
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 ?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 ?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.
Input
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 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.
Output
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.
Put guards in room N.
where N is the room you've decided to place the guards.
Sample Input
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,给定重点ed.求一个点v,使得v到终点ed距离最短,并且去掉点v都,从起点0到终点ed就不可到达。 思路:先用bfs(或者单源最短路经算法dijstra或者spfa(本质对于这种边权为1的图,spfa与bfs是没有什么区别))求出所有到终点ed最短的路径。 这样就可以转化为单源最短路,以终点ed为源,并且所有的边方向全部反向。 其实在用dfs枚举删除每个点后(终点ed不需要)看是否还能从起点0到终点ed,在这些点中,取到终点ed最近的那个点。 注意输入:用到了sscanf(str,"%d",&u); 代码如下:/* @author : liuwen */ #include <iostream> #include <cstdio> #include <cstring> #include <climits> //INT_MAX INT_MIN LONG_LONG_MAX LONG_LONG_MIN #include <cstdlib> #include <queue> #include <stack> #include <map> #include <vector> #include <cmath> #include <algorithm> using namespace std; const int inf=10000000; int G[205][205],dist[205],vis[205]; int ed,n,isok; void bfs() { queue<int>que; memset(vis,0,sizeof(vis)); que.push(ed); dist[ed]=0; vis[ed]=1; while(!que.empty()){ int tmp=que.front(); que.pop(); vis[tmp]=0; for(int i=0;i<n;i++){ if(G[i][tmp]!=inf&&dist[i]>dist[tmp]+1){ dist[i]=dist[tmp]+1; if(!vis[i]){ vis[i]=1; que.push(i); } } } } } void dfs(int u) { vis[u]=1; if(isok) return; if(u==ed) {isok=1;return;} for(int i=0;i<n;i++){ if(G[u][i]!=inf&&!vis[i]){ dfs(i); vis[i]=0; } } } int main() { //freopen("in.txt","r",stdin); char str1[100],str2[100]; int u,v; scanf("%d%d",&n,&ed); memset(G,0,sizeof(G)); memset(vis,0,sizeof(vis)); for(int i=0;i<n;i++) dist[i]=inf; for(int i=0;i<n;i++){ for(int j=0;j<n;j++){ G[i][j]=inf; } } while(scanf("%s%s",str1,str2)==2){ sscanf(str1,"%d",&u); sscanf(str2,"%d",&v); G[u][v]=1; } bfs(); int pos=0; for(int i=0;i<n;i++){ if(i==ed) continue; memset(vis,0,sizeof(vis)); isok=0; vis[i]=1; //去掉i点 dfs(0); if(!isok){ //若去掉i点后,不可到达终点 if(dist[pos]>dist[i]){ pos=i; } } } printf("Put guards in room %d.\n",pos); return 0; }