hdu 5067 Harry And Dig Machine(状态压缩dp)

来源:互联网 发布:授权回调域名校验出错 编辑:程序博客网 时间:2024/05/22 15:29

Harry And Dig Machine

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 729    Accepted Submission(s): 279


Problem Description
  As we all know, Harry Porter learns magic at Hogwarts School. However, learning magical knowledge alone is insufficient to become a great magician. Sometimes, Harry also has to gain knowledge from other certain subjects, such as language, mathematics, English, and even algorithm.
  Dumbledore, the headmaster of Hogwarts, is planning to construct a new teaching building in his school. The area he selects can be considered as an n*m grid, some (but no more than ten) cells of which might contain stones. We should remove the stones there in order to save place for the teaching building. However, the stones might be useful, so we just move them to the top-left cell. Taking it into account that Harry learned how to operate dig machine in Lanxiang School several years ago, Dumbledore decides to let him do this job and wants it done as quickly as possible. Harry needs one unit time to move his dig machine from one cell to the adjacent one. Yet skilled as he is, it takes no time for him to move stones into or out of the dig machine, which is big enough to carry infinite stones. Given Harry and his dig machine at the top-left cell in the beginning, if he wants to optimize his work, what is the minimal time Harry needs to finish it?
 

Input
They are sever test cases, you should process to the end of file.
For each test case, there are two integers n and m.(1n,m50).
The next n line, each line contains m integer. The j-th number of ith line a[i][j] means there are a[i][j] stones on the jth cell of the ith line.( 0a[i][j]100 , and no more than 10 of a[i][j] will be positive integer).
 

Output
For each test case, just output one line that contains an integer indicate the minimal time that Harry can finish his job.
 

Sample Input
3 30 0 00 100 00 0 02 21 11 1
 

Sample Output
44
 

Source
BestCoder Round #14
题意:到达所有正值点回到起点花费的最小时间
做法:直接找出正值点,二进制状态压缩,表示点是否到达
dp[state][j]表示到达过的点为state最后一个到达的点为j的最小时间
详细转移见代码,很水的状态压缩,注意边界值的特判即可
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <vector>#define MAX 51using namespace std;typedef pair<int,int> PII;int mp[MAX][MAX];int dis[MAX][MAX];vector<PII> v;int dp[1<<12][12];int n,m;int main ( ){    while ( ~scanf ( "%d%d" , &n , &m ) )     {        v.clear();        for ( int i = 1 ; i <= n ; i++ )            for ( int j = 1 ; j <= m ; j++ )            {                scanf ( "%d" , &mp[i][j] );                if ( i == 1 && j == 1 ) continue;                if ( mp[i][j] )                     v.push_back ( make_pair ( i , j ) );            }        int len = v.size();        for ( int i = 0 ; i < len ; i++ )            for ( int j = 0 ; j < len ; j++ )                dis[i+1][j+1] = abs ( v[i].first - v[j].first ) + abs ( v[i].second - v[j].second );        for ( int i = 0 ; i < len ; i++ )           dis[0][i+1] = dis[i+1][0] = v[i].first + v[i].second - 2;        len++;        if ( len == 1 )        {            printf ( "0\n" );            continue;        }        int total = 1<<len;        memset ( dp , 0x3f , sizeof ( dp ) );        for ( int i = 1 ; i < len ; i++ )            dp[1<<i][i] = dis[0][i];        for ( int i = 1 ; i < total ; i++ )            for ( int j = 0 ; j < len ; j++ )               if ( (1<<j)&i )                  for ( int k = 0 ; k < len ; k++ )                  {                      if ( k == j ) continue;                      if ( (1<<k)&i )                        dp[i][j] = min ( dp[i-(1<<j)][k] + dis[k][j] , dp[i][j] );                  }        printf ( "%d\n" , dp[total-1][0] );                   }}



 
0 0