HDU 5067 Harry And Dig Machine

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   for   strong   

題目連結~~>

做題感悟:在比賽的時候SB了,明明是個很裸的TSP但是第二組範例一直不對,很是無語,最後發現數組開倒了 。

解題思路:

                 這題應該屬於TSP入門題。狀態方程: dp [ S|( 1 << j) ] [ i ]    = min ( dp [ S ] [ i ] + d [ i ] [ j ] ) ;代表達到狀態 S|(1<<j) ,到達 j 點所形成的最優狀態,等於達到狀態 S ,到達 i 點,然後再從 i 點到 j 點的距離。不斷更新就好。

代碼:

#include<iostream>#include<sstream>#include<map>#include<cmath>#include<fstream>#include<queue>#include<vector>#include<sstream>#include<cstring>#include<cstdio>#include<stack>#include<bitset>#include<ctime>#include<string>#include<cctype>#include<iomanip>#include<algorithm>using namespace std  ;#define INT __int64#define L(x)  (x * 2)#define R(x)  (x * 2 + 1)const int INF = 0x3f3f3f3f ;const double esp = 0.0000000001 ;const double PI = acos(-1.0) ;const INT mod =  10000007 ;const int MY = 1400 + 5 ;const int MX = (1<<12) + 5 ;int n ,m ,num ;int dp[MX][25] ,d[25][25] ;struct node{    int x ,y ;}P[50] ;void DP_SC(){    memset(dp ,INF ,sizeof(dp)) ;    dp[1][0] = 0 ;    for(int S = 0 ;S < (1<<num) ; ++S)      for(int i = 0 ;i < num ; ++i)        if(dp[S][i] != INF)        {            for(int j = 0 ;j < num ; ++j)            {                if(S&(1<<j))  continue ;                dp[S|(1<<j)][j] = min(dp[S|(1<<j)][j] ,dp[S][i] + d[i][j]) ;            }        }    int S = (1<<num)-1 ,ans = INF ;    for(int i = 0 ;i < num ; ++i)          ans = min(ans ,dp[S][i] + d[i][0]) ;    cout<<ans<<endl ;}int main(){    //freopen("input.txt" ,"r" ,stdin) ;    while(~scanf("%d%d" ,&n ,&m))    {        num = 1 ;        int x ;        for(int i = 0 ;i < n ; ++i)          for(int j = 0 ;j < m ; ++j)          {              scanf("%d" ,&x) ;              if(x&&(i+j))              {                  P[num].x = i ;                  P[num++].y = j ;              }          }       P[0].x = 0 ;       P[0].y = 0 ;       for(int i = 0 ;i < num ; ++i)         for(int j = 0 ;j < num ; ++j)           d[i][j] = abs(P[i].x-P[j].x) + abs(P[i].y-P[j].y) ;       DP_SC() ;    }    return 0 ;}


HDU 5067 Harry And Dig Machine

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.