首页 > 专题系列 > Java解POJ > POJ 3084 Panic Room [解题报告] Java
2013
11-12

POJ 3084 Panic Room [解题报告] Java

Panic Room

问题描述 :

You are the lead programmer for the Securitron 9042, the latest and greatest in home security software from Jellern Inc. (Motto: We secure your stuff so YOU can’t even get to it). The software is designed to “secure” a room; it does this by determining the minimum number of locks it has to perform to prevent access to a given room from one or more other rooms. Each door connects two rooms and has a single control panel that will unlock it. This control panel is accessible from only one side of the door. So, for example, if the layout of a house looked like this:



with rooms numbered 0-6 and control panels marked with the letters “CP” (each next to the door it can unlock and in the room that it is accessible from), then one could say that the minimum number of locks to perform to secure room 2 from room 1 is two; one has to lock the door between room 2 and room 1 and the door between room 3 and room 1. Note that it is impossible to secure room 2 from room 3, since one would always be able to use the control panel in room 3 that unlocks the door between room 3 and room 2.

输入:

Input to this problem will begin with a line containing a single integer x indicating the number of datasets. Each data set consists of two components:
  1. Start line – a single line “m n” (1 <=m<= 20; 0 <=n<= 19) where m indicates the number of rooms in the house and n indicates the room to secure (the panic room).
  2. Room list – a series of m lines. Each line lists, for a single room, whether there is an intruder in that room (“I” for intruder, “NI” for no intruder), a count of doors c (0 <= c <= 20) that lead to other rooms and have a control panel in this room, and a list of rooms that those doors lead to. For example, if room 3 had no intruder, and doors to rooms 1 and 2, and each of those doors' control panels were accessible from room 3 (as is the case in the above layout), the line for room 3 would read "NI 2 1 2". The first line in the list represents room 0. The second line represents room 1, and so on until the last line, which represents room m - 1. On each line, the rooms are always listed in ascending order. It is possible for rooms to be connected by multiple doors and for there to be more than one intruder!

输出:

For each dataset, output the fewest number of locks to perform to secure the panic room from all the intruders. If it is impossible to secure the panic room from all the intruders, output “PANIC ROOM BREACH”. Assume that all doors start out unlocked and there will not be an intruder in the panic room.

样例输入:

3
7 2
NI 0
I 3 0 4 5
NI 2 1 6
NI 2 1 2
NI 0
NI 0
NI 0
7 2
I 0
NI 3 0 4 5
NI 2 1 6
I 2 1 2
NI 0
NI 0
NI 0
4 3
I 0
NI 1 2
NI 1 0
NI 4 1 1 2 2

样例输出:

2
PANIC ROOM BREACH
1

解题代码:

/* @author: */
import java.util.*;
class ff
{
  static int min( int a, int b ) {
    return a< b?a:b;
  }
	
  class edge
 {
  int to;
  int c, f; 
  int rev_i;
  edge( int pa, int pb, int pc ) {
	to = pa; c = pb; f = 0;
	rev_i = pc;
   }
  }

  edge e[][] = new edge[30][1000];
  int en[] = new int[30];
  boolean sign[] = new boolean[30];
	
  void insert_edge( int from, int to, int limit )
  {
	e[from][en[from]] = new edge( to, limit, en[to] );
	e[to][en[to]] = new edge( from, 0, en[from] );
	en[from]++;
	en[to]++;
   }
	
   void add_flow( edge ee, int d )
  {
	ee.f += d;
	e[ ee.to ][ ee.rev_i ].f -= d;
   }
	
   int search( int k, int t, int best )
   {
	int i, m = en[k];
	int temp;
	edge ep;
	sign[k] = true;
	if( k == t )
         return best;
	for( i=0; i< m; i++ )
	{
	  ep = e[k][i];
	  if( ep.f < ep.c && !sign[ ep.to ] )
	  {
	   if( ( temp = search( ep.to, t, min( best, ep.c - ep.f ) ) ) > 0 )
	   {
		ep.f += temp;
		e[ ep.to ][ ep.rev_i ].f -= temp;
		return temp;
	    }
	   }
	  }
	 return 0;
     }
	
   int maxflow( int n, int s, int t )
   {
     int total = 0, add = 0;
     do
     {
	total += add;
	for( int i=0; i< 30; i++ )
	  sign[i] = false;
      }
     while( ( add = search( s, t, (1<< 30) ) ) > 0 );
	return total;
    }
 }

class Main {
 public static void main(String args[]) throws Exception
 {
   Scanner cin = new Scanner( System.in );
   int t, n, m, i, j, ans, b = 0;
   int h, g;
   String w;
   t = cin.nextInt();
   while( t-- != 0 ) {
     ff f = new ff();
     n = cin.nextInt();
     m = cin.nextInt();
     for( i=0; i< n; i++ ) {
    	w = cin.next();
    	if( w.equals( "I" ) ) {
         b = i;
    	  f.insert_edge( n, i, 1000000 );
    	}
    	h = cin.nextInt();
    	for( j=0; j< h; j++ ) {
    	  g = cin.nextInt();
    	  f.insert_edge( g, i, 1 );
    	  f.insert_edge( i, g, 1000000 );
    	}
      }

     ans = f.maxflow( n+1, n, m );
     if( ans >= 1000000 )
    	System.out.println( "PANIC ROOM BREACH" );
     else
    	System.out.println( ans );
   }
   return;
  }
}

  1. 可以根据二叉排序树的定义进行严格的排序树创建和后序遍历操作。如果形成的排序树相同,其树的前、中、后序遍历是相同的,但在此处不能使用中序遍历,因为,中序遍历的结果就是排序的结果。经在九度测试,运行时间90ms,比楼主的要快。