hdu 5067

H - Harry And Dig Machine
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Appoint description: 

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 3 0 0 0 0 100 0 0 0 0 2 2 1 1 1 1
 

Sample Output

4 4
 
分析:第一道旅行商問題,從起始點出發遍歷一遍所有被標記點,最後回到原點,求最小消耗時間
狀態壓縮搞就行~~
#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <cmath>
using namespace std;
const int INF=99999999;
const int maxn=1<<10;


int n,m;
int dis[15][15],ct;
int dp[maxn+10][14];


struct node
{
  int x,y;
}st[16];

void solve()
{

    int tot=1<<ct;
    for(int i=0;i<=maxn;i++)
        for(int j=0;j<12;j++)
        dp[i][j]=INF;

    for(int i=0;i<ct;i++)
        {
            dp[1<<i][i]=abs(st[i].x-1)+abs(st[i].y-1);
           // cout<<dp[1<<i][i]<<endl;
        }
    for(int i=1;i<tot;i++) //枚舉狀態集合
    {
        for(int j=0;j<ct;j++) //枚舉位置
        {
           if((i&(1<<j))==0)continue;
           for(int k=0;k<ct;k++) //狀態轉移
            {
                if((i&(1<<k))==0)
                    {
                        dp[i|(1<<k)][k]=min(dp[i|(1<<k)][k],dp[i][j]+dis[j][k]);
                    }
            }
        }
    }

    int ans=INF;
    for(int i=0;i<ct;i++)
      {
        ans=min(ans,dp[tot-1][i]+abs(st[i].x-1)+abs(st[i].y-1));
      }

    cout<<ans<<endl;
}

int main()
{
   while( cin>>n>>m )
   {
       ct=0;
       for(int i=1;i<=n;i++)
          for(int j=1;j<=m;j++)
            {
               int tmp;
               scanf("%d",&tmp);
               if(tmp==0)continue;
               if( i==1&&j==1 )continue;
               st[ct].x=i;
               st[ct++].y=j;
             }

       if(ct==0)
       {
          cout<<0<<endl;
          continue;
       }

       for(int i=0;i<ct;i++)
            for(int j=i+1;j<ct;j++)
            {
                dis[i][j]=abs(st[i].x-st[j].x)+abs(st[i].y-st[j].y);
                dis[j][i]=dis[i][j];
            }


      
       solve();
   }
   return 0;
}


發佈了244 篇原創文章 · 獲贊 17 · 訪問量 17萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章