zoukankan      html  css  js  c++  java
  • light oj 1057 状压dp TSP

     1 #include <iostream>
     2 #include <cstdlib>
     3 #include <cstring>
     4 #include <queue>
     5 #include <cstdio>
     6 #include <algorithm>
     7 #include <map>
     8 //#include <time.h>
     9 //#include <ext/pb_ds/assoc_container.hpp>
    10 //#include <ext/pb_ds/tree_policy.hpp>
    11 #define LL long long
    12 
    13 using namespace std;
    14 //using namespace __gnu_pbds;
    15 
    16 int dp[1<<16][20];
    17 
    18 int n;
    19 
    20 struct nodes
    21 {
    22     int x, y;
    23     int operator - (const nodes &b)const
    24     {
    25         return max(abs(x - b.x) , abs(y - b.y));
    26     }
    27 }Point[20];
    28 
    29 int dfs(int statu,int cur)
    30 {
    31     if(dp[statu][cur] != -1)
    32         return dp[statu][cur];
    33     if(statu == (1<< (n+1) )-1 && cur == 0)
    34         return dp[statu][cur] = 0;
    35     dp[statu][cur] = 0x3f3f3f;
    36 
    37     for(int i = 0; i <= n; i++)
    38     {
    39         if( ((statu >> i)&1) == 1 ) continue;
    40         dp[statu][cur] = min(dp[statu][cur], dfs( statu|(1<<i),i ) + (Point[cur]-Point[i]) );
    41     }
    42 
    43     return dp[statu][cur];
    44 }
    45 
    46 void solve()
    47 {
    48     memset(dp,-1,sizeof(dp));
    49     int row,col;
    50     scanf("%d %d",&row,&col);
    51     char ss[25];
    52     n = 0;
    53     for(int i = 1; i <= row; i++)
    54     {
    55         scanf("%s",ss+1);
    56         for(int j = 1; j <= col; j++)
    57             if(ss[j] == 'x')
    58         {
    59             Point[0].x = i;
    60             Point[0].y = j;
    61         }
    62         else if(ss[j] == 'g')
    63         {
    64             Point[++n].x = i;
    65             Point[n].y = j;
    66         }
    67     }
    68     int ans = dfs(0,0);
    69     printf("%d
    ",ans);
    70 }
    71 
    72 
    73 int main(void)
    74 {
    75     int t,cnt = 0;
    76     scanf("%d",&t);
    77     while(t--)
    78     {
    79         printf("Case %d: ",++cnt);
    80         solve();
    81     }
    82     return 0;
    83 }
  • 相关阅读:
    代码规范
    svn的牛逼操作反向merge
    QT 半透明遮罩(弹窗)
    ACE库 ACE_Handle_Set类解析
    linux系统如何启用ftp服务
    vim color
    Linux动态库应用
    自建工程makefile文件
    Makefile工程文件
    linux杂记
  • 原文地址:https://www.cnblogs.com/henserlinda/p/5743885.html
Copyright © 2011-2022 走看看