POJ1130 Alien Security

                                                                                        Alien Security
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 2359   Accepted: 894

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.

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.

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.

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.

Source

 
思路:输入的第一行给出点的总数N和所要到达的目标点。各个点的编号从0~N-1。点0是开始点。题目所求为:从点0开始到目标点,找出必须要经过的且离目标点最近的点,输出该点的编号。
        首先从点0开始使用BFS搜索,直到目标点,结束。搜索的过程中记录下从点0到目标点的路径。路径记录的方式为记录点X的前驱点。如:PREV[X]=Y;Y为X的前驱结点。那些必须经过的点一定在这个求出的路径上面。然后从目标点到开始点,一个一个查找,看哪个点符合要求。方法为:将点T设置为已经访问,即VIS[T]=1,然后从开始点0搜索,看是否能够到达目标点。如果能够则这个点就是所求。否则看这个点的前驱结点。一直循环下去,知道开始点。
 
 
  1 #include <cstdlib>
  2 #include <iostream>
  3 #include <cstdio>
  4 #include <cstring>
  5 #include <string>
  6 #include <cctype>
  7 #include <cmath>
  8 #include <queue>
  9 #include <vector>
 10 
 11 
 12 #define MAXINT 99999999
 13 
 14 
 15 using namespace std;
 16 
 17 
 18 //int data[1000][1000];
 19 
 20 int prev[300];
 21 int vis[300];
 22 //int path[1000];
 23 
 24 int countn=0;
 25 vector<int>vt[300];
 26 
 27 int bfs(int n,int et)
 28 {
 29     int i;
 30     
 31     queue<int>q;
 32     for(i=0;i<n;i++)
 33     {vis[i]=0;prev[i]=-1;}
 34     
 35     vis[0]=1;
 36     q.push(0);
 37     /*while(q.size())
 38     {
 39                    int tmp=q.front();
 40                    q.pop();
 41                    
 42                    for(i=0;i<n;i++)
 43                    {if((vis[i]==0)&&(data[tmp][i]==1))
 44                    {q.push(i);vis[i]=1;prev[i]=tmp;if(i==et)break;}
 45                    }
 46                    
 47     }*/
 48     
 49     while(q.size())
 50     {
 51                    int tmp=q.front();
 52                    q.pop();
 53                    
 54                    vector<int>::iterator it;
 55                    for(it=vt[tmp].begin();it!=vt[tmp].end();it++)
 56                    {if(vis[*it]==0)
 57                     {vis[*it]=1;
 58                      q.push(*it);
 59                      prev[*it]=tmp;
 60                      if(*it==et)
 61                      break;
 62                     }
 63                    }
 64     }
 65     
 66 
 67 
 68     
 69     return 0;
 70 }
 71 
 72 
 73 
 74 
 75 
 76 
 77 
 78 
 79 int bfs2(int beginv,int n,int et)
 80 {
 81     int i;
 82     
 83     
 84     queue<int>q;
 85     
 86     q.push(beginv);
 87     vis[beginv]=1;
 88     
 89     
 90     while(q.size())
 91     {
 92                    int tmp=q.front();
 93                    q.pop();
 94                    
 95                    
 96                    /*for(i=0;i<n;i++)
 97                    {
 98                                    if((vis[i]==0)&&(data[tmp][i]==1))
 99                                    {vis[i]=1;
100                                     q.push(i);
101                                     if(i==et)
102                                     return 1;
103                                    }
104                    }*/
105                    
106                    vector<int>::iterator it;
107                    for(it=vt[tmp].begin();it!=vt[tmp].end();it++)
108                    {if(vis[*it]==0)
109                     {vis[*it]=1;
110                      q.push(*it);
111                      if(*it==et)
112                      return 1;
113                     }
114                    }
115     }
116     return 0;
117 }
118 
119 
120 
121 int dfs(int beginv,int n,int et)
122 {
123     if(beginv==et)
124     return 1;
125     
126     vis[beginv]=1;
127     
128     vector<int>::iterator it;
129     for(it=vt[beginv].begin();it!=vt[beginv].end();it++)
130     {if(vis[*it]==0)
131      {if(dfs(*it,n,et))
132       return 1;
133      }
134     }
135     
136     return 0;
137 }
138 
139 
140 
141       
142 
143                    
144 
145 
146 int main(int argc, char *argv[])
147 {
148     int n,et;
149     scanf("%d%d",&n,&et);
150     int a,b;
151     int i,j,k;
152     /*for(i=0;i<n;i++)
153     for(j=0;j<n;j++)
154     data[i][j]=0;
155     */
156     
157     
158     
159     
160     while(scanf("%d%d",&a,&b)!=EOF)
161     {
162             //data[a][b]=1;
163             vt[a].insert(vt[a].end(),b);
164     }
165     
166     bfs(n,et);
167     
168     int w=prev[et];
169    
170   
171   while(w>-1)
172   {
173              for(i=0;i<n;i++)
174              vis[i]=0;
175              
176             vis[w]=1;
177             
178             if(w==0)
179             {printf("Put guards in room %d.\n",w);break;}
180              
181             int tag=bfs2(0,n,et);
182             
183             if(tag==0)
184             {printf("Put guards in room %d.\n",w);break;}
185             
186             w=prev[w];
187             
188              
189   }
190   
191   
192     
193             
194     
195     system("PAUSE");
196     return EXIT_SUCCESS;
197 }

 

posted @ 2012-08-01 21:54  cseriscser  阅读(425)  评论(0编辑  收藏  举报