標籤: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