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

来源:互联网 发布:百分百综合采集软件 编辑:程序博客网 时间:2024/06/10 19:33

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.$(1 \leq n, m \leq 50)$. 
The next n line, each line contains m integer. The j-th number of $i_{th}$ line a[i][j] means there are a[i][j] stones on the $j_{th}$ cell of the $i_{th}$ line.( $0 \leq a[i][j] \leq 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

题意:

旅行商问题,给出n个位置,求从起点(0,0)出发遍历所有位置再回到原点得最短路。


解题思路:

1.DP+状态压缩,以二进制保存当前遍历状态,如果此状态下某个点未被经过,则用此状态转移到经过该店的状态,更新其值。

2.注意到n很小,DFS搜一遍~。


代码1:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>using namespace std;const int N=55;const int INF=0x3f3f3f3f;int dp[(1<<11)+15][N];int dist[N][N];struct Node{    int x,y;}p[N];int main(){    int n,m,t;    int cnt;    while(cin>>n>>m)    {        cnt=0;        for(int i=0; i<n; i++)            for(int j=0; j<n; j++)            {                cin>>t;                if(t||(i==0&&j==0))                {                    p[cnt].x=i;                    p[cnt++].y=j;                }            }        for(int i=0; i<cnt; i++)        {            for(int j=i+1; j<cnt; j++)                dist[i][j]=dist[j][i]=abs(p[i].x-p[j].x)+abs(p[i].y-p[j].y);                dist[i][i]=0;        }        memset(dp,INF,sizeof(dp));        dp[0][0]=0;        for(int i=0; i<(1<<cnt); i++)        {            for(int j=0; j<cnt; j++)            {                if(dp[i][j]==INF||(i&(1<<j))==0)                        continue;                for(int k=0; k<cnt; k++)                {                    if(i&(1<<k))                        continue;                    dp[i|(1<<k)][k]=min(dp[i|(1<<k)][k],dp[i][j]+dist[j][k]);                }            }        }        cout<<dp[(1<<cnt)-1][0]<<endl;    }}

代码2:
#include <iostream>  #include <cstdio>  #include <string>  #include <cstring>  #include <vector>  #include <deque>  #include <list>  #include <cctype>  #include <algorithm>  #include <climits>  #include <queue>  #include <stack>  #include <cmath>  #include <set>  #include <iomanip>  #include <cstdlib>  #include <ctime>  #pragma comment(linker, "/STACK:1024000000,1024000000")  using namespace std;    struct node  {      int x;      int y;  };    struct node p[15];  int ans;  bool vis[15];  int path[15];  bool flag;    void dfs ( int be, int en, int sum )  {      if ( en == sum )      {          ans = min ( ans, path[be] + p[be].x + p[be].y );          return;      }      for ( int i = 0; i <= en; i ++ )      {          if ( ! vis[i] )          {              int dis = abs ( p[be].x - p[i].x ) + abs ( p[be].y - p[i].y );              if ( path[i] > path[be] + dis )              {                  int tmp = path[i];                  path[i] = path[be] + dis;                  vis[i] = 1;                  if ( path[i] < ans )                      dfs ( i, en, sum + 1 );                  vis[i] = 0;                  path[i] = tmp;              }          }      }  }    int main()  {      int m, n;      while ( cin >> m >> n )      {          int cnt = 0;          ans = 0x3f3f3f3f;          flag = 0;          int tmp;          memset ( vis, 0, sizeof ( vis ) );          memset ( path, 0x3f3f3f3f, sizeof ( path ) );          for ( int i = 0; i < m; i ++ )          {              for ( int j = 0; j < n; j ++ )              {                  cin >> tmp;                  if ( tmp != 0 )                  {                      p[++cnt].x = i;                      p[cnt].y = j;                  }              }          }          p[0].x = 0;          p[0].y = 0;          path[0] = 0;          dfs ( 0, cnt, 0 );          cout << ans << endl;      }      return 0;  }  



0 0