hdu3681之状态压缩dp

Prison Break

Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2845    Accepted Submission(s): 710


Problem Description
Rompire is a robot kingdom and a lot of robots live there peacefully. But one day, the king of Rompire was captured by human beings. His thinking circuit was changed by human and thus became a tyrant. All those who are against him were put into jail, including our clever Micheal#1. Now it’s time to escape, but Micheal#1 needs an optimal plan and he contacts you, one of his human friends, for help.
The jail area is a rectangle contains n×m little grids, each grid might be one of the following: 
1) Empty area, represented by a capital letter ‘S’. 
2) The starting position of Micheal#1, represented by a capital letter ‘F’. 
3) Energy pool, represented by a capital letter ‘G’. When entering an energy pool, Micheal#1 can use it to charge his battery ONLY ONCE. After the charging, Micheal#1’s battery will become FULL and the energy pool will become an empty area. Of course, passing an energy pool without using it is allowed.
4) Laser sensor, represented by a capital letter ‘D’. Since it is extremely sensitive, Micheal#1 cannot step into a grid with a laser sensor. 
5) Power switch, represented by a capital letter ‘Y’. Once Micheal#1 steps into a grid with a Power switch, he will certainly turn it off. 

In order to escape from the jail, Micheal#1 need to turn off all the power switches to stop the electric web on the roof—then he can just fly away. Moving to an adjacent grid (directly up, down, left or right) will cost 1 unit of energy and only moving operation costs energy. Of course, Micheal#1 cannot move when his battery contains no energy. 

The larger the battery is, the more energy it can save. But larger battery means more weight and higher probability of being found by the weight sensor. So Micheal#1 needs to make his battery as small as possible, and still large enough to hold all energy he need. Assuming that the size of the battery equals to maximum units of energy that can be saved in the battery, and Micheal#1 is fully charged at the beginning, Please tell him the minimum size of the battery needed for his Prison break.
 

Input
Input contains multiple test cases, ended by 0 0. For each test case, the first line contains two integer numbers n and m showing the size of the jail. Next n lines consist of m capital letters each, which stands for the description of the jail.You can assume that 1<=n,m<=15, and the sum of energy pools and power switches is less than 15.
 

Output
For each test case, output one integer in a line, representing the minimum size of the battery Micheal#1 needs. If Micheal#1 can’t escape, output -1.
 

Sample Input
   
   
   
   
5 5 GDDSS SSSFS SYGYS SGSYS SSYSS 0 0
 

Sample Output
   
   
   
   
4
 

Source
2010 Asia Hangzhou Regional Contest

分析:对于所给的图,把y,g,f看成点,然后对这些点重建图求最短距离,由于这些点的总和是<=15,所以可以转化为TSP问题,对于答案进行二分,然后进行状压dp判断是否能走完所有的y

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
#include <map>
#include <cmath>
#include <iomanip>
#define INF 99999999
typedef long long LL;
using namespace std;

const int MAX=10+6;
int n,m,size,temp;
int dp[1<<MAX][MAX],dist[MAX][MAX];
int dir[4][2]={0,1,0,-1,1,0,-1,0};
int mp[MAX][MAX],d[MAX][MAX];
char s[MAX][MAX];
bool mark[MAX][MAX],w[MAX];

int BFS(int ii,int jj,int a,int b){//求最短路 
	queue<int>xx,yy;
	if(ii == a && jj == b)return 0;
	xx.push(ii);
	yy.push(jj);
	int x,y,nx,ny;
	memset(mark,false,sizeof mark);
	mark[ii][jj]=true;
	d[ii][jj]=0;
	while(!xx.empty()){
		x=xx.front();
		y=yy.front();
		xx.pop();
		yy.pop();
		for(int i=0;i<4;++i){
			nx=x+dir[i][0];
			ny=y+dir[i][1];
			if(nx<0 || ny<0 || nx>=n || ny>=m || s[nx][ny] == 'D')continue;
			if(mark[nx][ny])continue;
			mark[nx][ny]=true;
			d[nx][ny]=d[x][y]+1;
			if(nx == a && ny == b)return d[nx][ny];
			xx.push(nx);
			yy.push(ny);
		} 
	}
	return INF; 
}

void Init(){//重建图求最短路 
	memset(mp,-1,sizeof mp);
	memset(w,false,sizeof w);
	size=temp=1;
	for(int i=0;i<n;++i){
		for(int j=0;j<m;++j){
			if(s[i][j] == 'D' || s[i][j] == 'S')continue;
			for(int a=i;a<n;++a){
				for(int b=(a == i?j:0);b<m;++b){
					if(s[a][b] == 'D' || s[a][b] == 'S')continue;
					if(mp[a][b] == -1){
						if(s[a][b] == 'F')mp[a][b]=0;
						else mp[a][b]=size++;
					}
					if(s[a][b] == 'G')w[mp[a][b]]=true;
					else temp=(temp|(1<<mp[a][b]));
					dist[mp[a][b]][mp[i][j]]=dist[mp[i][j]][mp[a][b]]=BFS(i,j,a,b);
				}
			}
		}
	}
}

bool DP(int mid){//TSP
	int bit=1<<size;
	memset(dp,-1,sizeof dp);
	dp[1][0]=mid;
	for(int i=1;i<bit;++i){
		for(int j=0;j<size;++j){
			if(dp[i][j] == -1)continue;
			if((i&temp) == temp)return true;
			for(int k=0;k<size;++k){
				int p=1<<k;
				if(i&p)continue;
				dp[i|p][k]=max(dp[i|p][k],dp[i][j]-dist[j][k]);
				if(dp[i|p][k]>=0 && w[k])dp[i|p][k]=mid;
			}
		}
	}
	return false;
}

int main(){
	while(~scanf("%d%d",&n,&m),n+m){
		for(int i=0;i<n;++i)scanf("%s",s[i]);
		Init();//初始化最短路
		int l=0,r=0;
		for(int i=0;i<size;++i){
			for(int j=i+1;j<size;++j){
				if(dist[i][j] == INF)continue;
				r=max(r,dist[i][j]*(size-1));
			}
		}
		int minn=INF;
		while(l<=r){
			int mid=l+r>>1;
			if(DP(mid))r=mid-1,minn=min(minn,mid);
			else l=mid+1;
		}
		if(minn == INF)printf("-1\n");
		else printf("%d\n",minn);
	}
	return 0;
}

你可能感兴趣的:(hdu3681之状态压缩dp)